You are previewing Programming with Java.
O'Reilly logo
Programming with Java

Book Description

Programming with Java is designed to help the reader understand the concepts of Java programming language. It includes an exhaustive coverage of additional appendices on keywords, operators and supplementary programs; additional chapters on Collect

Table of Contents

  1. Cover
  2. Title Page
  3. Contents
  4. Dedication
  5. List of Programs
  6. List of Boxes
  7. List of Figures
  8. List of Tables
  9. Preface
  10. Chapter 1: Introduction
    1. 1.1 A Brief History of Java
    2. 1.2 Why Is Java Popular for the Internet?
    3. 1.3 Features of Java
      1. 1.3.1 Simple
      2. 1.3.2 Secure
      3. 1.3.3 Portable
      4. 1.3.4 Object oriented
      5. 1.3.5 Robust
      6. 1.3.6 Multi-threaded
      7. 1.3.7 Interpreted
      8. 1.3.8 Architecture neutral
      9. 1.3.9 Distributed
    4. 1.4 How Java Programs Run
    5. 1.5 Advantages and Disadvantages of an Interpreted Language
    6. 1.6 Java Applets and Applications
    7. 1.7 Advantages of Java Technology
    8. 1.8 Java Platform
    9. 1.9 Java Development Kit
    10. 1.10 How to Run Simple Java Programs
      1. 1.10.1 How to organize our computer for command prompt operation
      2. 1.10.2 How to run a simple program in command prompt environment
      3. 1.10.3 How to run programs in integrated development environments
    11. Keywords
    12. RuleBook
    13. Review Questions
  11. Chapter 2: Object Orientation an Introduction
    1. 2.1 Programming Languages Before Object Orientation Became Popular
    2. 2.2 Features of Object-Oriented Language
      1. 2.2.1 Encapsulation
      2. 2.2.2 Data hiding
      3. 2.2.3 Inheritance
      4. 2.2.4 Polymorphism
      5. 2.2.5 Message passing
    3. 2.3 The Benefits of OOP
      1. 2.3.1 Software reusability
      2. 2.3.2 Code sharing
      3. 2.3.3 Rapid prototyping
      4. 2.3.4 Information hiding
    4. Keywords
    5. Review Questions
  12. Chapter 3: Java Language Basics
    1. 3.1 Introduction
    2. 3.2 Java Program Structure
    3. 3.3 Basic Building Blocks
      1. 3.3.1 Comments
      2. 3.3.2 Character set
      3. 3.3.3 Constants
    4. 3.4 Variables
    5. 3.5 Data Types
      1. 3.5.1 Numeric data types
      2. 3.5.2 Non-numeric data types
      3. 3.5.3 Declarations of variables
    6. 3.6 Operators
      1. 3.6.1 Arithmetic operators
      2. 3.6.2 Relational operators
      3. 3.6.3 Logical operators
      4. 3.6.4 Bit-wise operators
      5. 3.6.5 Shift operators
      6. 3.6.6 Assignment operators
      7. 3.6.7 Other (special) operators
    7. 3.7 Expressions
      1. 3.7.1 Arithmetic expressions
      2. 3.7.2 Arithmetic expressions with single operand
      3. 3.7.3 Arithmetic expressions with two operands
      4. 3.7.4 General arithmetic expressions
      5. 3.7.5 Logical expressions
      6. 3.7.6 Assignment expressions
      7. 3.7.7 Type char in assignment
    8. 3.8 Typecasting
    9. 3.9 Keyword Final
    10. 3.10 Statements
      1. 3.10.1 Expression statements
      2. 3.10.2 Declaration statement
      3. 3.10.3 Flow control statements
      4. 3.10.4 Empty statement
      5. 3.10.5 Labelled statement
    11. 3.11 Description of Very Simple Java Program
    12. 3.12 End of Chapter Programs
      1. 3.12.1 Relational expression
      2. 3.12.2 Study of special characters
      3. 3.12.3 Truncating a real number
      4. 3.12.4 A straight line
      5. 3.12.5 Keyword final
    13. Keywords
    14. RuleBook
    15. C++ Corner
    16. Review Questions
    17. Exercises
  13. Chapter 4: Control Structures
    1. 4.1 Simple If Statement
    2. 4.2 If-Else Statement
    3. 4.3 Switch-Case Statement
    4. Advance Note
    5. Keywords
    6. RuleBook
    7. Review Questions
    8. Exercises
  14. Chapter 5: Loops
    1. 5.1 for Loop
    2. 5.2 while Loop
    3. 5.3 do-while Loop
    4. 5.4 break and continue
      1. 5.4.1 break statement
      2. 5.4.2 continue statement
    5. 5.5 Comparison of all the Three Constructs
    6. 5.6 End of Chapter Programs
      1. 5.6.1 Fibonacci numbers
      2. 5.6.2 Compound interest
      3. 5.6.3 for loop control variable
      4. 5.6.4 Java is great!
      5. 5.6.5 cv can be a character
    7. Keywords
    8. RuleBook
    9. C++ Corner
    10. Review Questions
    11. Exercises
  15. Chapter 6: Functions
    1. 6.1 Functions in Java
      1. 6.1.1 Syntax of methods
    2. 6.2 Recursive Functions
    3. 6.3 Parameter Passing
      1. 6.3.1 Simple data types as parameters
      2. 6.3.2 Objects as parameters
    4. 6.4 Method swap
    5. 6.5 In-built Methods or Library Methods
    6. 6.6 End of Chapter Programs
      1. 6.6.1 Method pow()
      2. 6.6.2 Method Math.atan2()
    7. Keywords
    8. RuleBook
    9. C++ Corner
    10. Review Questions
    11. Exercises
  16. Chapter 7: Arrays
    1. 7.1 Arrays in Java
    2. 7.2 Creating an Array
    3. 7.3 Array Index Checking
    4. 7.4 Multi-Dimensional Arrays
      1. 7.4.1 Matrix addition
      2. 7.4.2 Matrix multiplication
    5. 7.5 Enhanced for Loop
    6. 7.6 End of Chapter Programs
      1. 7.6.1 Array index out of bound
      2. 7.6.2 Binary search
      3. 7.6.3 Linear search
      4. 7.6.4 Checking for duplicate
    7. Keywords
    8. RuleBook
    9. C++ Corner
    10. Review Questions
    11. Exercises
  17. Chapter 8: Introducing Classes
    1. 8.1 Class Fundamentals
      1. 8.1.1 The general form of a class
      2. 8.1.2 A simple class
    2. 8.2 Declaring and Creating Objects
    3. 8.3 Accessing Members
    4. 8.4 Access Control Parameters: Public and Private
    5. 8.5 Introducing Methods
    6. 8.6 Methods with Parameters
    7. 8.7 Methods Returning a Value
    8. 8.8 Constructors
    9. 8.9 System-Supplied Constructor
    10. 8.10 Default Constructor
    11. 8.11 Keyword this
    12. 8.12 Garbage Collection
    13. 8.13 The finalize() Method
    14. 8.14 Overloading Methods and Constructors
    15. 8.15 Using Objects as Parameters
    16. 8.16 Keyword Static
      1. 8.16.1 Static variables
      2. 8.16.2 Static methods
    17. 8.17 Description of Simple Java Program
    18. 8.18 End of Chapter Programs
      1. 8.18.1 Initializing an object is necessary
      2. 8.18.2 A non-static method can access private instance variables
      3. 8.18.3 Private variables are not accessible outside the class
      4. 8.18.4 Use of this keyword
      5. 8.18.5 Keyword static not allowed for local variables
      6. 8.18.6 Class Point
    19. Keywords
    20. RuleBook
    21. C++ Corner
    22. Review Questions
    23. Exercises
  18. Chapter 9: Wrapper Classes
    1. 9.1 Introduction
    2. 9.2 Class Byte
    3. 9.3 Float
    4. 9.4 Boolean
    5. 9.5 Character
    6. 9.6 Autoboxing and Unboxing Principle
    7. 9.7 End of Chapter Programs
      1. 9.7.1 Java identifier start
      2. 9.7.2 Method valueOf()
    8. Keywords
    9. RuleBook
    10. C++ Corner
    11. Review Questions
    12. Exercises
  19. Chapter 10: Strings
    1. 10.1 Introduction
    2. 10.2 Declaring and Constructing a String
    3. 10.3 Reading a String
    4. 10.4 Writing a String
    5. 10.5 Copying One String into Other
    6. 10.6 Comparing Strings
    7. 10.7 Other Operations on Strings
      1. 10.7.1 Testing for contents
      2. 10.7.2 Changing the case
      3. 10.7.3 Methods valueof() and toString()
      4. 10.7.4 Concatenation of strings
    8. 10.8 Sorting Strings
    9. 10.9 StringBuffer
      1. 10.9.1 How to modify strings using StringBuffer
    10. 10.10 Class Vector
    11. 10.11 End of Chapter Programs
      1. 10.11.1 Reading a string from console
      2. 10.11.2 Comparison operators == does not work on strings
    12. Keywords
    13. RuleBook
    14. C++ Corner
    15. Review Questions
    16. Exercises
  20. Chapter 11: Simple Input/Output
    1. 11.1 Concepts of Streams, Stream Classes
    2. 11.2 Character Stream Classes
    3. 11.3 Writing to the Screen
    4. 11.4 Simple Formatting
    5. 11.5 Formatting with printf
    6. 11.6 Reading the Data
    7. 11.7 Reading Strings
    8. 11.8 Reading Data
    9. 11.9 Class Scanner
    10. 11.10 StreamTokenizer
    11. 11.11 Hierarchies of Character-Based i/o Classes
    12. 11.12 Hierarchies of Byte-Oriented i/o Classes
    13. 11.13 End of Chapter Programs
      1. 11.13.1 Using class InputStreamReader
      2. 11.13.2 Reading integer
      3. 11.13.3 Reading into a character array
      4. 11.13.4 Formatted output
    14. RuleBook
    15. C++ Corner
    16. Keywords
    17. Review Questions
    18. Exercises
  21. Chapter 12: Inheritance
    1. 12.1 Inheritance Basics
    2. 12.2 Member Access and Inheritance
    3. 12.3 Keyword super
      1. 12.3.1 Use of super in sub-class constructor
      2. 12.3.2 Use of super in referencing a super-class variable
    4. 12.4 Use of this in Sub-Class Constructor
    5. 12.5 Creating a Multi-Level Hierarchy
      1. 12.5.1 Keyword protected
    6. 12.6 Method overriding
    7. 12.7 Using final in Inheritance
      1. 12.7.1 Using final with a class
      2. 12.7.2 Using final with a method of a super-class
    8. 12.8 Introducing Interface
      1. 12.8.1 Defining an interface
      2. 12.8.2 Implementing interfaces
      3. 12.8.3 Variables in interfaces
    9. 12.9 Abstract Methods and Classes
    10. 12.10 Collection of Programs
      1. 12.10.1 private of super not accessible
      2. 12.10.2 This program proves that default acts as public
      3. 12.10.3 Is super always needed in sub-class constructor?
      4. 12.10.4 A final class cannot be extended
    11. Keywords
    12. RuleBook
    13. C++ Corner
    14. Review Questions
    15. Exercises
  22. Chapter 13: Graphics
    1. 13.1 Introduction
    2. 13.2 Frame
    3. 13.3 Drawing a Line
    4. 13.4 Drawing a Rectangle
    5. 13.5 Drawing a Point
    6. 13.6 Drawing Circles
    7. 13.7 Introducing Colours
    8. 13.8 Designing a Fabric
    9. 13.9 Simple Animation
    10. 13.10 End of Chapter Programs
      1. 13.10.1 Thick point by other methods
    11. Keywords
    12. C++ Corner
    13. Review Questions
    14. Exercises
  23. Chapter 14: Collection of Academic Programs
    1. 14.1 Prime Numbers
      1. 14.1.1 Sieve of Eratosthenes
      2. 14.1.2 Prime numbers: A more efficient method
    2. 14.2 Date, Time, and Calendar
      1. 14.2.1 Class Date
      2. 14.2.2 Method getTime() from class Date
      3. 14.2.3 Class GregorianCalendar
      4. 14.2.4 Incrementing a date
    3. 14.3 Matrix
      1. 14.3.1 Matrix addition
      2. 14.3.2 Matrix multiplication
    4. 14.4 Handling Complex Numbers
    5. 14.5 Astrology and Numerology
      1. 14.5.1 Astrology
      2. 14.5.2 Numerology
    6. 14.6 Playing Cards
      1. 14.6.1 Shuffling of cards I
      2. 14.6.2 Generate a fresh deck of cards
    7. 14.7 Searching and Sorting
      1. 14.7.1 Selection sort
      2. 14.7.2 Quick sort
      3. 14.7.3 Bubble sort
    8. Exercises
  24. Chapter 15: Miscellaneous Topics
    1. 15.1 Packages
      1. 15.1.1 Def ining and using packages
      2. 15.1.2 Static import
    2. 15.2 Command Line Arguments
    3. 15.3 Inner and Outer Classes
    4. 15.4 BigInteger
    5. 15.5 Enumeration
    6. 15.6 Labelled break and Labelled Continue
      1. 15.6.1 Labelled break
      2. 15.6.2 Labelled continue
    7. 15.7 Operators Revisited
      1. 15.7.1 Conditional operator (? :)
      2. 15.7.2 Operator instanceof
      3. 15.7.3 Operator >>>
    8. 15.8 Interface Cloneable
      1. 15.8.1 Method clone()
    9. 15.9 Generics
      1. 15.9.1 Generic types
      2. 15.9.2 Generic methods
    10. 15.10 Guidelines for Forming Identifiers
    11. 15.11 Parameter Passing Re-visited
    12. 15.12 Dynamic Data Structures
      1. 15.12.1 Linked list
      2. 15.12.2 Binary search tree
    13. 15.13 Package Java.util
      1. 15.13.1 Arrays
      2. 15.13.2 Class BitSet
    14. 15.14 Class Runtime
      1. 15.14.1 Methods freeMemory() and totalMemory()
    15. 15.15 Class System: Time Delay Routine
    16. 15.16 Assertion
    17. 15.17 Description of Advanced Java Program
    18. 15.18 The Untold Story: Java Criticized
    19. Keywords
    20. RuleBook
    21. C++ Corner
    22. Review Questions
    23. Exercises
  25. Chapter 16: Inheritance Revisited
    1. 16.1 Class Object
    2. 16.2 Multiple Inheritance
    3. 16.3 Polymorphism
      1. 16.3.1 A super-class variable can reference a sub-class object
      2. 16.3.2 Polymorphism in action
    4. 16.4 Dynamic Method Dispatch
    5. 16.5 Collection of Programs
      1. 16.5.1 Sub-class reference cannot be assigned a super-class value
      2. 16.5.2 Arrays of dissimilar elements
    6. RuleBook
    7. C++ Corner
    8. Review Questions
  26. Chapter 17: Files
    1. 17.1 Why Files?
    2. 17.2 File Classification
    3. 17.3 Class File
    4. 17.4 Text Files
    5. 17.5 Formatted File Input
    6. 17.6 Reading File with Format-Free Data
    7. 17.7 Binary Files
    8. 17.8 Files for Objects
    9. 17.9 Random Access File
    10. 17.10 Object Serialization
    11. 17.11 End of Chapter Programs
      1. 17.11.1 Large binary file
      2. 17.11.2 Large text file
      3. 17.11.3 List of files
      4. 17.11.4 Reading matrices from binary files
      5. 17.11.5 Listing all the files including those in the sub-folders
    12. Keywords
    13. Review Questions
  27. Chapter 18: Multi-threaded Programming
    1. 18.1 What are Threads?
    2. 18.2 Why Use Threads?
    3. 18.3 Creating and Running a Thread
    4. 18.4 Creating Multiple Threads
    5. 18.5 Life Cycle of a Thread
    6. 18.6 Implementing Runnable Interface
    7. 18.7 Thread Priority and Thread Scheduling Policy
    8. 18.8 Synchronization
      1. 18.8.1 Synchronized methods
      2. 18.8.2 Synchronized statements
      3. 18.8.3 Methods wait(), notify(), and notifyAll()
      4. 18.8.4 Method join()
    9. 18.9 Timer and TimerTask
    10. Keywords
    11. RuleBook
    12. Review Questions
    13. Exercises
  28. Chapter 19: Exception Handling
    1. 19.1 Why Use Exception Handling
    2. 19.2 Types of Exceptions
    3. 19.3 Checked and Unchecked Exceptions
    4. 19.4 Exception Handling Constructs
    5. 19.5 Keyword finally
    6. 19.6 Throw Statement
    7. 19.7 Multiple Catch Blocks
    8. 19.8 Collection of End of Chapter Programs
      1. 19.8.1 Clause finally
      2. 19.8.2 Number format exception
    9. Keywords
    10. RuleBook
    11. C++ Corner
    12. Review Questions
    13. Exercises
  29. Chapter 20: Java Applets
    1. 20.1 Applet Basics
    2. 20.2 The Applet Class
    3. 20.3 Applet Architecture
    4. 20.4 Applet Life Cycle
    5. 20.5 Comparison of Applet and Application
    6. 20.6 Setting Font and Changing Style, Size, Type, Foreground and Background Colour
    7. 20.7 Using Graphics Methods in Applets
    8. 20.8 Colours and Applets
    9. 20.9 Images in Applet
    10. 20.10 Sound in Applets
    11. 20.11 Applet Tag Attributes
    12. 20.12 JAR Files
    13. 20.13 Introduction to HTML
    14. Keywords
    15. RuleBook
    16. C++ Corner
    17. Review Questions
    18. Exercises
  30. Chapter 21: Event Handling And AWT
    1. 21.1 Abstract Window Toolkit
      1. 21.1.1 Using Button
      2. 21.1.2 Using TextField
      3. 21.1.3 Using checkbox
      4. 21.1.4 Using radio button
      5. 21.1.5 Using canvas
    2. 21.2 Event Handling
      1. 21.2.1 Delegation event model
      2. 21.2.2 Event classes and event listener interfaces
      3. 21.2.3 KeyEvent
      4. 21.2.4 Mouse events
      5. 21.2.5 Adapter classes
      6. 21.2.6 Virtual key code constants
    3. Keywords
    4. RuleBook
    5. Review Questions
    6. Exercises
  31. Chapter 22: Introduction to Swing
    1. 22.1 JFrame
    2. 22.2 JOptionPane for (Output) Message
    3. 22.3 Input Dialog JOptionPane
    4. 22.4 Class JButton
    5. 22.5 Class JLabel
    6. 22.6 JCheckBox
    7. 22.7 Introduction to JRadioButtons
    8. 22.8 Introduction to Class Canvas
    9. 22.9 Class JTextField
      1. 22.9.1 Simple demo
      2. 22.9.2 Finding a square of a number
    10. 22.10 JTextArea
    11. 22.11 JMenu
    12. 22.12 JFileChooser
    13. 22.13 JComboBox
    14. 22.14 Layout Manager
      1. 22.14.1 BorderLayout
      2. 22.14.2 BoxLayout
      3. 22.14.3 GridLayout
    15. 22.15 Using Borders
    16. 22.16 Java Foundation Classes
    17. Keywords
    18. RuleBook
    19. Review Questions
    20. Exercises
  32. Chapter 23: Collection Framework
    1. 23.1 Collection Interface
    2. 23.2 List Interface
    3. 23.3 ArrayList
    4. 23.4 LinkedList
    5. 23.5 Interface Set
      1. 23.5.1 Iterator
    6. 23.6 TreeSet
    7. 23.7 HashSet
    8. 23.8 Interface Map
    9. 23.9 HashMap
    10. 23.10 TreeMap
    11. 23.11 Hashtable
    12. 23.12 Algorithms
      1. 23.12.1 Sorting
      2. 23.12.2 Algorithm shuffle
      3. 23.12.3 Binary search
    13. 23.13 HashMap
    14. 23.14 Enhanced For Loop
    15. Keywords
    16. RuleBook
    17. Review Questions
    18. Exercises
  33. Chapter 24: Multimedia Experience
    1. 24.1 Images
      1. 24.1.1 Introducing image
      2. 24.1.2 Converting image to an array
      3. 24.1.3 Processing image
      4. 24.1.4 Study of transparency
      5. 24.1.5 Subtitles
    2. 24.2 Sound
      1. 24.2.1 Sound in applications
      2. 24.2.2 A musical keyboard
    3. 24.3 Complete Multimedia Experience
    4. Keywords
    5. Review Questions
    6. Exercises
  34. Chapter 25: Moving From C++ to Java
    1. 25.1 Part I: Comparing keywords
      1. 25.1.1 Keywords
    2. 25.2 Part II: Differences as a Programming Language
      1. 25.2.1 Facilities in C++ missing in Java
      2. 25.2.2 Facilities in Java not present in C++
    3. 25.3 C++ to Java Through Programs
      1. 25.3.1 Exercise one
      2. 25.3.2 Exercise two
      3. 25.3.3 Exercise three
      4. 25.3.4 Exercise four
      5. 25.3.5 Exercise five
      6. 25.3.6 Exercise six
    4. 25.4 Rules for C++ to Java Conversion
  35. Chapter 26: Collection of Programs
    1. 26.1 Projectile
    2. 26.2 Wall Clock
    3. 26.3 Drawing Ellipses
    4. 26.4 Area of Circle by Monte Carlo Method
    5. 26.5 Display of Binary Tree
    6. 26.6 Drawing with Data from a File
    7. 26.7 Animation
    8. 26.8 Graphical Display of Bubble Sort
    9. 26.9 Astrology: Strength of a Horoscope
      1. 26.9.1 Strength of planets located in different signs
      2. 26.9.2 Strength of planets depending on the position
    10. 26.10 Freehand Drawing
    11. 26.11 Sudoku I
    12. 26.12 Sudoku II
    13. 26.13 Moon Revolves Around Earth
    14. 26.14 Astronomy: Retrograde Motion
  36. Appendices
    1. Appendix A: RuleBook
    2. Appendix B: Keywords
    3. Appendix C: Operators
    4. Appendix D: List of Important Packages
    5. Appendix E: Mini projects
      1. AE. 1 Chess Clock
      2. AE. 2 Rolling board
      3. AE. 3 Kaun Banega Karodpati
      4. AE. 4 Exam master
      5. AE. 5 Game of Business
      6. AE. 6 Game: Snake II
      7. AE. 7 Game: Snake and ladder
      8. AE. 8 Game of Contract Bridge
      9. AE. 9 Java IDE in Java
      10. AE. 10 Game of chess
  37. Bibliography
  38. Glossary
  39. Acknowledgements
  40. Copyright
  41. Back Cover