Understanding the Benefits of Object-Oriented Programming . ... Using
Formatters in the java.text Package . ... Exercise 2: Formatting Numeric and Date
Data .
Intermediate Java Programming What You Will Learn...................................................................................................................... 1 Understanding Object-Oriented Programming ............................................................................... 2 Working with Classes ............................................................................................................... 3 Understanding the Benefits of Object-Oriented Programming ................................................ 3 Working with Objects ..................................................................................................................... 4 Creating New Objects ............................................................................................................... 4 Using Variables to Reference Objects ...................................................................................... 5 Working with Dates ........................................................................................................................ 7 Creating Date Objects ............................................................................................................. 7 Using the GregorianCalendar Class ............................................................................... 8 Analyzing the Behavior of Reference Variables .................................................................... 15 Applying What You’ve Learned: Printing a Monthly Calendar ............................................. 17 Exercise 1 ..................................................................................................................................... 21 Using Formatters in the java.text Package .................................................................................... 23 Using the NumberFormat Class ......................................................................................... 23 Formatting Dates ..................................................................................................................... 30 Exercise 2: Formatting Numeric and Date Data ........................................................................... 33 Creating New Classes ................................................................................................................... 35 Defining a New Class ............................................................................................................. 35 Organizing the Class Body ..................................................................................................... 36 Declaring Member Variables (Fields) .................................................................................... 37 Writing Constructors ............................................................................................................... 39 Writing Methods ..................................................................................................................... 42 Creating New Objects Based on User-Defined Classes ......................................................... 46 Exercise 3: Writing Classes .......................................................................................................... 49 Expanding the Student Class ........................................................................................................ 52 Resolving Parameter Names in Constructors and Methods .................................................... 52 Declaring Static Members....................................................................................................... 53 Using Programming Logic to Implement Your Methods ....................................................... 56 Overloading Methods.............................................................................................................. 57 Streamlining Your Code ......................................................................................................... 58 Exercise 4: Expanding Your Classes ............................................................................................ 60 Working with Collections ............................................................................................................. 63 Creating a New Array List ...................................................................................................... 63 Intermediate Java Programming www.cetc.umsl.edu
Rev. B
Populating the Array List ........................................................................................................ 64 Retrieving the Number of List Elements ................................................................................ 66 Retrieving Array List Elements .............................................................................................. 66 Using Loops to Work with Collections .................................................................................. 67 Removing Array List Elements .............................................................................................. 68 Comparing Arrays and Array Lists ......................................................................................... 69 Exercise 5: Working with Collections .......................................................................................... 70 Designing Effective Applications ................................................................................................. 72 Identifying the Required Classes ............................................................................................ 72 Achieving the Goal of Encapsulation ..................................................................................... 73 Defining Fields........................................................................................................................ 73 Writing Methods ..................................................................................................................... 73 Writing Consistent Code ......................................................................................................... 74 Modifying Your Application .................................................................................................. 74 Applying What You’ve Learned: Building a Deck of Cards .................................................. 75 Exercise 6: Designing Effective Classes....................................................................................... 83 Understanding Inheritance ............................................................................................................ 84 Using Inheritance in Your Own Classes ................................................................................. 86 Using Polymorphic Variables ................................................................................................. 94 Using the Object Superclass ............................................................................................... 97 Working with Abstract Classes ............................................................................................... 97 Working with Final Classes (Optional) .................................................................................. 98 Exercise 7: Using Inheritance ....................................................................................................... 99 Organizing Your Classes into Packages ..................................................................................... 102 Documenting Your Classes......................................................................................................... 105 Documenting the Media Class ............................................................................................ 107 Using javadoc to Extract Comments ................................................................................ 108 Viewing Your Documentation .............................................................................................. 109 Documenting Subclasses ...................................................................................................... 109 Exercise 8: Using Packages and Documenting Your Classes .................................................... 111 Creating an JAVA Generic User Interface (GUI)....................................................................... 112 Appendix A: Java Programming Reference Books .................................................................... 113 Appendix B: Solutions to Exercises ........................................................................................... 114