java objects first

593
Java Objects First: An Introduction to Computer Programming using Java and BlueJ Copyright 2006-12 Rick Gee

Upload: geronimo3

Post on 06-Nov-2015

5 views

Category:

Documents


0 download

DESCRIPTION

PROGRAMACION ORIENTADA A OBJETOS CON JAVA USANDO BLUEJ

TRANSCRIPT

  • Java Objects First:

    An Introduction to Computer Programming

    using Java and BlueJ

    Copyright 2006-12

    Rick Gee

  • i

    Table of Contents

    Table of Contents ................................................................................................................................... i

    Preface to the reader.............................................................................................................................. 1 To students ........................................................................................................................................ 1 To teachers ........................................................................................................................................ 4 To everyone ....................................................................................................................................... 6

    Acknowledgements ............................................................................................................................... 8

    Notes ..................................................................................................................................................... 9

    Chapter 0 Introduction ..................................................................................................................... 11 Learning objectives ......................................................................................................................... 11 What is computer science?.............................................................................................................. 11 Hardware ......................................................................................................................................... 11 Software .......................................................................................................................................... 13 Programming languages.................................................................................................................. 13 Summary ......................................................................................................................................... 14

    Chapter 1 Classes and objects: identity, state, and behaviour .......................................................... 15 Learning objectives ......................................................................................................................... 15 Introduction ..................................................................................................................................... 15 Definitions....................................................................................................................................... 15 Classes............................................................................................................................................. 17 The Student class ............................................................................................................................ 19

    Class diagrams ............................................................................................................................ 19 Datatypes..................................................................................................................................... 21

    The Professor class ......................................................................................................................... 22 Tradeoffs of storing the name as one field versus several .............................................................. 23 Behaviours ...................................................................................................................................... 25 A first look at Java .......................................................................................................................... 26

    Documentation ............................................................................................................................ 27 Programming Style documentation.......................................................................................... 28 Class declaration ......................................................................................................................... 28 Programming Style class names .............................................................................................. 28 Instance variables ........................................................................................................................ 29 Programming Style instance variables ..................................................................................... 30 Constructor(s) ............................................................................................................................. 30 Programming Style constructors .............................................................................................. 33 Getters and setters ....................................................................................................................... 34 Programming Style getters and setters ..................................................................................... 35

  • ii

    toString ........................................................................................................................................ 35 Programming Style toString .................................................................................................... 37

    Creating another class, the College ................................................................................................. 37 Summary ......................................................................................................................................... 39 Exercises ......................................................................................................................................... 40

    Chapter 2 Introducing BlueJ ............................................................................................................ 41 Learning objectives ......................................................................................................................... 41 Introduction ..................................................................................................................................... 41 BlueJ ............................................................................................................................................... 41

    Creating a new project ................................................................................................................ 42 Virtual machines and bytecode ................................................................................................... 47 Testing the Student class ............................................................................................................. 47 Inspecting an object .................................................................................................................... 52 Unit testing - definition ............................................................................................................... 54 Unit testing with BlueJ................................................................................................................ 55 Unit testing the results ............................................................................................................. 63

    Smoke testing .................................................................................................................................. 66 The Professor class ......................................................................................................................... 67 The College class ............................................................................................................................ 67 BlueJ minutiae ................................................................................................................................ 67

    Unit testing tools ......................................................................................................................... 68 Font size ...................................................................................................................................... 68 Line numbers .............................................................................................................................. 68 Colours ........................................................................................................................................ 68 Default code ................................................................................................................................ 70

    Summary ......................................................................................................................................... 71 Exercises ......................................................................................................................................... 72

    Chapter 3 Making decisions............................................................................................................. 75 Learning objectives ......................................................................................................................... 75 The if statement............................................................................................................................... 75

    Boolean algebra .......................................................................................................................... 75 Boolean algebra an example .................................................................................................... 77 A revised toString method .......................................................................................................... 78

    Using the Java documentation ................................................................................................ 79 Programming style if statements .............................................................................................. 81

    Checkstyle ............................................................................................................................... 81 Simpler tests .................................................................................................................................... 82 More complicated tests ................................................................................................................... 83 Summary ......................................................................................................................................... 86 Exercises ......................................................................................................................................... 87

    Chapter 4 Inheritance ....................................................................................................................... 91 Learning objectives ......................................................................................................................... 91 Abstract classes ............................................................................................................................... 91

    The Person class .......................................................................................................................... 92 toString in an abstract class..................................................................................................... 95

  • iii

    The Person class, continued ........................................................................................................ 97 The Student class a derived class ............................................................................................. 97 The Professor class ..................................................................................................................... 98

    Garbage in, garbage out .................................................................................................................. 98 In Summary ..................................................................................................................................... 98 Exercises ....................................................................................................................................... 100

    Chapter 5 Exceptions ...................................................................................................................... 103 Learning objectives ....................................................................................................................... 103 Definition ...................................................................................................................................... 103 Examples ....................................................................................................................................... 104 Runtime and nonruntime exceptions ............................................................................................ 104 Creating and throwing exceptions ................................................................................................ 105

    DataFormatException ............................................................................................................... 106 A better solution ........................................................................................................................ 106 ShortIdentifierException and LongIdentifierException ........................................................... 107 Validation using exceptions ...................................................................................................... 107 The body of validateIdentifier .................................................................................................. 108

    while loops ............................................................................................................................ 109 Testing the exceptions ............................................................................................................... 110 ShortIdentifierExceptionTest .................................................................................................... 111 Test good identifier ................................................................................................................ 111 Try and catch blocks ................................................................................................................. 111 Test short identifier ................................................................................................................ 112 Test short identifier, part 2 ..................................................................................................... 113 LongIdentifierExceptionTest .................................................................................................... 114 NonnumericIdentifierException ............................................................................................... 114

    Using a loop .......................................................................................................................... 114 Using an exception to throw an exception ............................................................................ 116

    Finally ........................................................................................................................................... 116 Summary ....................................................................................................................................... 117 Exercises ....................................................................................................................................... 118

    Chapter 6 An Address class............................................................................................................ 119 Learning objectives ....................................................................................................................... 119 Introduction ................................................................................................................................... 119 Adding an address ......................................................................................................................... 119

    The Address class ...................................................................................................................... 120 The Address class the code .................................................................................................... 121 The Address class the code in detail ................................................................................... 122 The Address class getters and setters ..................................................................................... 124 Person uses Address .................................................................................................................. 125 Making copies (cloning) ........................................................................................................... 126 The Address class unit testing ................................................................................................ 129 Testing Address cloning ............................................................................................................ 129 The reserved word null ............................................................................................................. 130

    The College class and addresses ................................................................................................... 131 Summary ....................................................................................................................................... 131

  • iv

    Exercises ....................................................................................................................................... 132

    Chapter 7 Dates .............................................................................................................................. 135 Learning objectives ....................................................................................................................... 135 Introduction ................................................................................................................................... 135 How old is a person? ..................................................................................................................... 135

    Dates ......................................................................................................................................... 136 Details about calendars ............................................................................................................. 136 The MyDate class a simplification ........................................................................................ 137

    Programming style a skeleton for a class ........................................................................... 138 Primitive types ...................................................................................................................... 138 Digression ............................................................................................................................. 139

    The MyDate class the code .................................................................................................... 139 The MyDate class unit tests ................................................................................................... 141 Using MyDate objects............................................................................................................... 143 Simplifying the class diagram ................................................................................................... 144 Retirement of a professor .......................................................................................................... 145 Retirement of a professor unit tests ....................................................................................... 146

    Programming style common errors ............................................................................................ 148 Bad dates - exceptions .................................................................................................................. 149 Summary ....................................................................................................................................... 153 Exercises ....................................................................................................................................... 154

    Chapter 8 Collections, part 1 ......................................................................................................... 155 Learning objectives ....................................................................................................................... 155 Life is complicated ........................................................................................................................ 155 Collections the college model contains ......................................................................................... 156 The Java Collections Framework.................................................................................................. 158 A set .............................................................................................................................................. 159 A collection of professors ............................................................................................................. 160 HashSet ......................................................................................................................................... 161 Generic collections........................................................................................................................ 162

    Programming style .................................................................................................................... 164 HashSet ...................................................................................................................... 164

    The equals method .................................................................................................................... 165 The hashCode method............................................................................................................... 166

    How many elements are in a set? .................................................................................................. 167 What are the elements in the set? .................................................................................................. 167

    Traversing a collection with a for loop ..................................................................................... 167 Adding a professor ........................................................................................................................ 168

    Cloning a professor ................................................................................................................... 170 Removing a professor ................................................................................................................... 171

    Traversing a collection with an iterator and a while loop ......................................................... 171 Exceptions ................................................................................................................................. 173

    Another collection of professors the department ....................................................................... 174 Listing all the professors ........................................................................................................... 175 The Singleton pattern ................................................................................................................ 175 Listing all the professors, continued ......................................................................................... 176

  • v

    Collection of departments ............................................................................................................. 178 Collection of students ................................................................................................................... 178 Summary ....................................................................................................................................... 179 Exercises ....................................................................................................................................... 180

    Chapter 9 Collections, part 2 ......................................................................................................... 183 Learning objectives ....................................................................................................................... 183 Introduction ................................................................................................................................... 183 A collection of courses .................................................................................................................. 183 The Course class ........................................................................................................................... 183

    Course unit tests ..................................................................................................................... 184 Formatting the output ................................................................................................................ 184

    printf .............................................................................................................................................. 186 A collection of courses, continued ................................................................................................ 186 The Section class........................................................................................................................... 186

    Set? List? ............................................................................................................................... 186 What is a section? ..................................................................................................................... 188

    The Singleton pattern revisited ..................................................................................................... 190 Listing a collection in order .......................................................................................................... 191

    Comparable ............................................................................................................................... 192 Comparators .............................................................................................................................. 192 Unit testing the comparator ....................................................................................................... 194 Alternative versions of the comparator ..................................................................................... 195

    Definitions class and object - revisited ...................................................................................... 196 Producing an alphabetical list of professors ................................................................................. 196

    The for-each statement, revisited .............................................................................................. 197 BlueJ revisited ........................................................................................................................... 198

    Producing a professor list in numeric order .................................................................................. 199 Passing a Comparator to a method ............................................................................................ 199

    Registering students in sections .................................................................................................... 201 Producing an alphabetic class list ................................................................................................. 202

    The for-each statement, revisited .............................................................................................. 204 Producing a class list in numeric order ......................................................................................... 204 Students enrol in sections.............................................................................................................. 205 SortedSet ....................................................................................................................................... 205 Summary ....................................................................................................................................... 205 Exercises ....................................................................................................................................... 206

    Chapter 10 Collections, part 3 ....................................................................................................... 209 Learning objectives ....................................................................................................................... 209 Introduction ................................................................................................................................... 209 The Meeting class ......................................................................................................................... 209

    Instance variables ...................................................................................................................... 210 Behaviours ................................................................................................................................ 210 Time arithmetic ......................................................................................................................... 210 Exceptions ................................................................................................................................. 212

    Section uses Meeting .................................................................................................................... 212 The if statement, revisited ..................................................................................................... 215

  • vi

    Creating the collection of Meeting objects ............................................................................... 216 Section uses Meeting displaying the times ................................................................................ 217

    Arrays ........................................................................................................................................ 218 StringBuffer .............................................................................................................................. 220 Adjusting to different colleges .................................................................................................. 221 Another for loop ........................................................................................................................ 222

    Processing all the meetings ........................................................................................................... 223 Manipulating StringBuffers .......................................................................................................... 223

    Placing data in StringBuffers .................................................................................................... 223 Converting numbers to day of the week ................................................................................... 225 Processing second and subsequent meetings ............................................................................ 226 A lot of code .............................................................................................................................. 227

    Programming Style ....................................................................................................................... 229 Course contains Section ................................................................................................................ 230 Mark, and Student contains Mark ................................................................................................. 231 Professor contains Section ............................................................................................................ 232 Summary ....................................................................................................................................... 233 Exercises ....................................................................................................................................... 234

    Chapter 11 Collections, part 4 ........................................................................................................ 235 Learning objectives ....................................................................................................................... 235 Introduction ................................................................................................................................... 235 Grade Average ............................................................................................................................... 235

    When to calculate an average ................................................................................................... 235 Weighted average ...................................................................................................................... 236 The transcript method original version .................................................................................. 236 Knowing when a semester changes .......................................................................................... 237 Transcript a skeleton for changes ........................................................................................... 237 When the semester changes ...................................................................................................... 238

    Integer division ..................................................................................................................... 239 When the semester remains the same ....................................................................................... 240 The declarations ........................................................................................................................ 240 The overall average ................................................................................................................... 240 The transcript String problems and deficiencies .................................................................... 241 The String class and its methods ............................................................................................... 241

    Details of split ....................................................................................................................... 242 Details of indexOf and lastIndexOf ...................................................................................... 244

    Reversing a String ..................................................................................................................... 244 Palindromes............................................................................................................................... 245 The transcript String reordering ............................................................................................. 246 The transcript String problems and deficiencies, continued .................................................. 248

    Summary ....................................................................................................................................... 250 Exercises ....................................................................................................................................... 251

    Chapter 12 Exceptions revisited .................................................................................................... 253 Learning objectives ....................................................................................................................... 253 Definition ...................................................................................................................................... 253 Examples ....................................................................................................................................... 254

  • vii

    Runtime and nonruntime exceptions ............................................................................................ 255 Creating and throwing exceptions ................................................................................................ 256 Catching exceptions ...................................................................................................................... 261

    CloneNotSupportedException .................................................................................................. 262 Bad input data ........................................................................................................................... 262

    Finally ........................................................................................................................................... 263 Summary ....................................................................................................................................... 263 Exercises ....................................................................................................................................... 264

    Chapter 13 Persistence, part 1 ........................................................................................................ 265 Learning objectives ....................................................................................................................... 265 Warning to the reader .................................................................................................................... 265 Why persistence? .......................................................................................................................... 265 What does saving an object mean? ............................................................................................... 266 External devices ............................................................................................................................ 266 Streams .......................................................................................................................................... 266 Persistence via object serialization ............................................................................................... 267

    Object serialization Address .................................................................................................. 268 Object serialization MyDate .................................................................................................. 271 Object serialization Meeting and Mark.................................................................................. 271 Object serialization Person, Student, Professor ..................................................................... 271 Object serialization some subtleties ....................................................................................... 271 Object serialization creating writeObject and readObject methods ....................................... 272 Object serialization Section and Student ............................................................................... 274 Object deserialization Section and Student............................................................................ 276 Programming style .................................................................................................................... 278 Object serialization Section and Student, revisited ................................................................ 278 Object serialization Professor ................................................................................................ 278 Object serialization College ................................................................................................... 278

    Summary ....................................................................................................................................... 279 Exercises ....................................................................................................................................... 280

    Chapter 14 Persistence, part 2 ........................................................................................................ 281 Learning objectives ....................................................................................................................... 281 Persistence via XML ..................................................................................................................... 281 A Digression Scalable Vector Graphics ..................................................................................... 282 A Second Digression PDF ......................................................................................................... 282 Using an XMLEncoder no collections ....................................................................................... 282 Using an XMLDecoder no collections ...................................................................................... 286 Using an XMLEncoder with a collection .................................................................................. 287 Using an XMLDecoder with a collection .................................................................................. 293 Programming Style ....................................................................................................................... 294 Using XML summary ................................................................................................................ 294 Persistence via traditional files ..................................................................................................... 295

    Input .......................................................................................................................................... 295 Output ....................................................................................................................................... 299 CSV ........................................................................................................................................... 300

    Convert a Student object to CSV format .............................................................................. 301

  • viii

    Writing a CSV file ................................................................................................................ 305 Reading a CSV file into a spreadsheet .................................................................................. 305 Reading a CSV file into a Java program ............................................................................... 306 Scanner .................................................................................................................................. 308 Decoupling ............................................................................................................................ 309

    Further study ............................................................................................................................. 311 Persistence via databases .............................................................................................................. 312 Summary ....................................................................................................................................... 312 Exercises ....................................................................................................................................... 313

    Chapter 15 Creating a GUI: data entry screens, part 1 .................................................................. 315 Learning objectives ....................................................................................................................... 315 Warning to the reader .................................................................................................................... 315 Introduction ................................................................................................................................... 315 GUI ............................................................................................................................................... 315 Some HCI considerations ............................................................................................................. 317 Layout managers ........................................................................................................................... 318 Installing the RelativeLayout layout manager .............................................................................. 320 Creating a data entry screen .......................................................................................................... 320

    How the data entry screen should appear ................................................................................. 320 Import statements ...................................................................................................................... 321 Creating the JFrame .................................................................................................................. 321 Making the JFrame visible ........................................................................................................ 324 Explaining the code................................................................................................................... 324

    The JFrame............................................................................................................................ 325 The layout manager............................................................................................................... 325 BindingFactory ..................................................................................................................... 325 The labels .............................................................................................................................. 326 The label Bindings ................................................................................................................ 327 The other labels ..................................................................................................................... 328 JTextFields ............................................................................................................................ 329 Other text fields..................................................................................................................... 330 Done!..................................................................................................................................... 330

    Summarizing building a data entry screen ................................................................................ 330 Displaying the frame ................................................................................................................. 330

    Threads definition ...................................................................................................................... 331 Threads and Swing ........................................................................................................................ 331

    Runnable ............................................................................................................................... 331 Create the frame .................................................................................................................... 332 Position and size the frame ................................................................................................... 332 box.pack(); ............................................................................................................................ 333 Make it visible....................................................................................................................... 333

    Displaying the frame ................................................................................................................. 333 Answering some questions ....................................................................................................... 333

    JButtons......................................................................................................................................... 337 Cancel button ............................................................................................................................ 337

    Create a JButton .................................................................................................................... 337

  • ix

    Create a JButtons bindings .................................................................................................. 337 Add a JButton and its bindings to the frame ......................................................................... 338 Create a mnemonic ............................................................................................................... 338 The default button ................................................................................................................. 338 Button action ......................................................................................................................... 338 The OK JButton .................................................................................................................... 338

    ActionEvents and ActionListeners ................................................................................................ 339 Creating an ActionListener ....................................................................................................... 339 Linking an ActionListener to a JButton .................................................................................... 340 What does an ActionListener do? ............................................................................................. 340 Behind the Cancel Button ......................................................................................................... 342

    Editing data ................................................................................................................................... 342 Verifying year and month text fields ......................................................................................... 343 Verifying the day text field ....................................................................................................... 345

    Verification behind the Okay button ............................................................................................. 347 Improvements ............................................................................................................................... 347

    Remove intrusive dialogs .......................................................................................................... 347 Creating the message ............................................................................................................ 347 Using the message................................................................................................................. 348

    Disable the OK button until needed .......................................................................................... 348 Data entry for other classes ........................................................................................................... 351 Testing a GUI ................................................................................................................................ 351 Summary ....................................................................................................................................... 351 Exercises ....................................................................................................................................... 352

    Chapter 16 Creating a GUI: data entry screens, part 2 .................................................................. 355 Learning objectives ....................................................................................................................... 355 Introduction ................................................................................................................................... 355 Building the Section data entry screen.......................................................................................... 355

    Create the frame ........................................................................................................................ 357 Create a column of panels ......................................................................................................... 357 Create the three panels .............................................................................................................. 357 Populate the top panel ............................................................................................................... 358

    Place the boxes in the panel .................................................................................................. 359 Decorate the panel..................................................................................................................... 360 Create the middle panel ............................................................................................................ 360

    Labels .................................................................................................................................... 361 The meetings ......................................................................................................................... 362

    Arrays ........................................................................................................................................ 362 Create the middle panel, continued ............................................................................................... 364

    Radio buttons ............................................................................................................................ 364 ButtonGroup ......................................................................................................................... 364 An array of radio buttons ...................................................................................................... 365 An array of text fields ........................................................................................................... 365 Checkboxes ........................................................................................................................... 365 Placing the arrays in the frame .............................................................................................. 366

    InputVerifiers ............................................................................................................................ 368

  • x

    ActionListeners for radio buttons ............................................................................................. 370 ActionListeners for checkboxes ................................................................................................ 371 Decorate the middle panel ........................................................................................................ 371

    The bottom panel .......................................................................................................................... 372 Which layout manager should we select for the bottom panel? ............................................... 372 Button ActionListeners ............................................................................................................. 372

    Summarizing building a data entry screen .................................................................................... 374 Some general comments on data entry ......................................................................................... 375 Summary ....................................................................................................................................... 376 Exercises ....................................................................................................................................... 377

    Chapter 17 Creating a GUI: menus ................................................................................................ 379 Learning objectives ....................................................................................................................... 379 Introduction ................................................................................................................................... 379 Creating the menus ....................................................................................................................... 379

    Definitions................................................................................................................................. 379 Designing the menu system ...................................................................................................... 380 Implementing the menu ............................................................................................................ 380 Accessibility .............................................................................................................................. 384

    Completing the menu .................................................................................................................... 384 Performing actions ........................................................................................................................ 385

    ActionListeners for each menu option ...................................................................................... 385 One ActionListener for all menu items ..................................................................................... 386

    Toolbars ......................................................................................................................................... 388 Help ............................................................................................................................................... 388 Internationalization ....................................................................................................................... 388

    Definition .................................................................................................................................. 388 Steps to follow .......................................................................................................................... 389 The Java statements to make the magic happen ....................................................................... 391 Who did the translation? ........................................................................................................... 392 Conclusion ................................................................................................................................ 392

    Summary ....................................................................................................................................... 392 Exercises ....................................................................................................................................... 393

    Chapter 18 Applets ......................................................................................................................... 395 Learning objectives ....................................................................................................................... 395 Introduction ................................................................................................................................... 395 Creating an applet ......................................................................................................................... 396 Running an applet ......................................................................................................................... 397 An applet (as created by BlueJ) under the microscope ................................................................. 399

    init ............................................................................................................................................. 401 start ............................................................................................................................................ 402 stop ............................................................................................................................................ 402 paint........................................................................................................................................... 403 destroy ....................................................................................................................................... 404 getAppletInfo ............................................................................................................................ 404 getParameterInfo ....................................................................................................................... 405

    Hello world ................................................................................................................................... 406

  • xi

    A problem.................................................................................................................................. 407 Squares .......................................................................................................................................... 408 Canadian flag ................................................................................................................................ 408 Creating HTML to run an applet ................................................................................................... 409

    A quick introduction to HTML ................................................................................................. 411 Summary ....................................................................................................................................... 412 Exercises ....................................................................................................................................... 413

    Chapter 19 Design Patterns ............................................................................................................ 423 Learning objectives ....................................................................................................................... 423 Introduction ................................................................................................................................... 423 Interface ........................................................................................................................................ 425 Marker interface ............................................................................................................................ 426 Creator........................................................................................................................................... 426 High Cohesion .............................................................................................................................. 426 Low Coupling ............................................................................................................................... 427 Observer or Event listener ............................................................................................................ 427 Delegation ..................................................................................................................................... 428 Factory .......................................................................................................................................... 428 Lazy Initialization ......................................................................................................................... 428 Singleton ....................................................................................................................................... 429 Model-view-controller (MVC) ..................................................................................................... 429 Summary ....................................................................................................................................... 430 Exercises ....................................................................................................................................... 431

    Chapter 20 Mathematical examples ............................................................................................... 433 Learning objectives ....................................................................................................................... 433 Introduction ................................................................................................................................... 433 BigInteger ..................................................................................................................................... 433

    The java.lang.Math package ..................................................................................................... 434 BigInteger, continued .................................................................................................................... 435 Definitions class and object - revisited ...................................................................................... 436 Rational numbers .......................................................................................................................... 436

    Adding rational numbers........................................................................................................... 438 Subtracting, multiplying, and dividing rational numbers ......................................................... 439 Inverting a rational number ....................................................................................................... 440 Converting a rational number to a double................................................................................. 440 What if ints are not big enough? ............................................................................................... 440

    Complex numbers ......................................................................................................................... 440 Arithmetic operations and complex numbers ........................................................................... 440 Implementing ComplexNumber ............................................................................................... 441

    Summary ....................................................................................................................................... 441 Exercises ....................................................................................................................................... 442

    Chapter 21 Mathematical methods ................................................................................................ 443 Learning objectives ....................................................................................................................... 443 Introduction ................................................................................................................................... 443 Greatest common divisor (gcd) ..................................................................................................... 444

  • xii

    A recursive greatest common divisor method ........................................................................... 444 Sample greatest common divisor calculations .......................................................................... 445

    Fibonacci numbers ........................................................................................................................ 446 Iterative Fibonacci method ....................................................................................................... 447 Recursive Fibonacci method ..................................................................................................... 448

    The factorial function .................................................................................................................... 449 The choose function ...................................................................................................................... 450

    A Formula! ................................................................................................................................ 450 Recursive choose method ......................................................................................................... 450

    The Ackermann function............................................................................................................... 451 Definition of the Ackermann function ...................................................................................... 451 Recursive implementation of the Ackermann function ............................................................ 451

    Dynamic programming ................................................................................................................. 452 Storing the data Fibonacci ..................................................................................................... 453 Saving the data Fibonacci ...................................................................................................... 454 Restoring the data Fibonacci .................................................................................................. 455 Saving the data Ackermann function ..................................................................................... 456 Restoring the data Ackermann function ................................................................................. 457

    Summary ....................................................................................................................................... 457 Exercises ....................................................................................................................................... 458

    Chapter 22 Loose ends ................................................................................................................... 459 Learning objectives ....................................................................................................................... 459 Introduction ................................................................................................................................... 459 public static void main(String[] args) ........................................................................................... 459 Summary ....................................................................................................................................... 460 Exercises ....................................................................................................................................... 461

    In Conclusion .................................................................................................................................... 463

    References ......................................................................................................................................... 465 Printed Reference materials .......................................................................................................... 465 Online references .......................................................................................................................... 466

    Lab Assignments ............................................................................................................................... 467

    Lab Assignment 0 Getting to know you ........................................................................................ 469 Objectives: .................................................................................................................................... 469 Procedure: ..................................................................................................................................... 469 Hand-in: ........................................................................................................................................ 471

    Lab Assignment 1 Getting to know the environment .................................................................... 473 Objectives: .................................................................................................................................... 473 Procedure: ..................................................................................................................................... 473 Hand-in: ........................................................................................................................................ 478

    Lab Assignment 2 Birds ................................................................................................................. 479 Objectives: .................................................................................................................................... 479 Introduction: .................................................................................................................................. 479 Procedure: ..................................................................................................................................... 480

  • xiii

    Hand-in: ........................................................................................................................................ 492 Notes: ............................................................................................................................................ 493

    Lab Assignment 3 People............................................................................................................... 495 Objectives: .................................................................................................................................... 495 Introduction: .................................................................................................................................. 495 Procedure: ..................................................................................................................................... 495 Hand-in: ........................................................................................................................................ 496

    Lab Assignment 4 Sightings and Lists ........................................................................................... 497 Objectives: .................................................................................................................................... 497 Introduction: .................................................................................................................................. 497 Procedure: ..................................................................................................................................... 499 Hand-in: ........................................................................................................................................ 500

    Lab Assignment 5 Sophistication................................................................................................... 501 Objectives: .................................................................................................................................... 501 Introduction: .................................................................................................................................. 501 Procedure: ..................................................................................................................................... 501 Hand-in: ........................................................................................................................................ 502

    Lab Assignment 6 Sophistication (continued) ............................................................................... 503 Objectives: .................................................................................................................................... 503 Introduction: .................................................................................................................................. 503 Procedure: ..................................................................................................................................... 503 Hand-in: ........................................................................................................................................ 505

    Lab Assignment 7 (Part 1) Even More Sophistication................................................................... 507 Objectives: .................................................................................................................................... 507 Background: .................................................................................................................................. 507 Procedure: ..................................................................................................................................... 507 Hand-in: ........................................................................................................................................ 508

    Lab Assignment 7 (Part 2) Even More Sophistication................................................................... 509 Objectives: .................................................................................................................................... 509 Background: .................................................................................................................................. 509 Procedure: ..................................................................................................................................... 509 Hand-in: ........................................................................................................................................ 509

    Lab Assignment 8 Reporting ......................................................................................................... 511 Objectives: .................................................................................................................................... 511 Introduction: .................................................................................................................................. 511 Procedure: ..................................................................................................................................... 511 Hand-in: ........................................................................................................................................ 512

    Lab Assignment 9 Querying .......................................................................................................... 513 Objectives: .................................................................................................................................... 513 Background: .................................................................................................................................. 513 Procedure: ..................................................................................................................................... 513 Hand-in: ........................................................................................................................................ 514

    Lab Assignment 10 Code cleaning ................................................................................................ 515

  • xiv

    Objectives: .................................................................................................................................... 515 Introduction: .................................................................................................................................. 515 Procedure: ..................................................................................................................................... 516 Hand-in: ........................................................................................................................................ 518

    Lab Assignment 0 A flock of birders ............................................................................................. 519 Objectives: .................................................................................................................................... 519 Background: .................................................................................................................................. 519 Procedure: ..................................................................................................................................... 520 Hand-in: ........................................................................................................................................ 521

    Lab Assignment 1 A flock of birders (continued) .......................................................................... 523 Objectives: .................................................................................................................................... 523 Procedure: ..................................................................................................................................... 523 Hand-in: ........................................................................................................................................ 524

    Lab Assignment 2 File processing and remembering .................................................................... 525 Objectives: .................................................................................................................................... 525 Introduction: .................................................................................................................................. 525 Procedure: ..................................................................................................................................... 525

    Part 1: ........................................................................................................................................ 525 Part 2: ........................................................................................................................................ 528

    Hand-in: ........................................................................................................................................ 528

    Lab Assignment 3 File processing and remembering (continued) ................................................. 529 Objectives: .................................................................................................................................... 529 Introduction: .................................................................................................................................. 529 Procedure: ..................................................................................................................................... 529

    Part 1: ........................................................................................................................................ 529 Part 2: ........................................................................................................................................ 530

    Hand-in: ........................................................................................................................................ 530

    Lab Assignment 4 Planning a GUI: a simple data entry screen ..................................................... 531 Objectives: .................................................................................................................................... 531 Introduction: .................................................................................................................................. 531 Procedure: ..................................................................................................................................... 531 Hand-in: ........................................................................................................................................ 532

    Lab Assignment 5 Building a GUI: a simple data entry screen ..................................................... 533 Objectives: .................................................................................................................................... 533 Introduction: .................................................................................................................................. 533 Procedure: ..................................................................................................................................... 533 Hand-in: ........................................................................................................................................ 533

    Lab Assignment 6 Building a GUI: menus .................................................................................... 535 Objectives: .................................................................................................................................... 535 Introduction: .................................................................................................................................. 535 Procedure: ..................................................................................................................................... 535 Hand-in: ........................................................................................................................................ 535

    Lab Assignment 7 And now for something completely different .................................................. 537

  • xv

    Objectives: .................................................................................................................................... 537 Background: .................................................................................................................................. 537 Procedure: ..................................................................................................................................... 537

    Part 1- Hello, world................................................................................................................... 537 Part 2 Simple flags ................................................................................................................. 538 Part 3 Seychelles or Macedonia .........................