Programming in Java John M. Morrison March 8, 2013 2 Contents 0 Getting Started 11 0.0 Introduction . 11 0.1 Getting a Java Development Kit . 11 0.2 Getting DrJava . 12 0.3 What the heck is tools.jar?.................... 14 1 Introducing Java 17 1.0 How does Java Work on a Mechanical Level? . 17 1.0.1 Customizing DrJava . 18 1.1 Python Classes and Objects . 19 1.2 Java Classes and Objects . 22 1.3 Java's Integer Types . 27 1.3.1 Some Interaction Pane Nuances . 28 1.3.2 Using Java integer types in Java Code . 29 1.4 The Rest of Java's Primitive Types . 34 1.4.1 The boolean Type . 35 1.4.2 Floating{Point Types . 36 1.4.3 The char type . 36 1.5 More Java Class Examples . 37 2 Java Objects 41 2.0 Java Object Types . 41 2.1 Java Strings as a Model for Java Objects . 41 3 4 CONTENTS 2.1.1 But is there More? . 43 2.2 Primitive vs. Object: A Case of equals Rights . 46 2.2.1 Aliasing . 48 2.3 More Java String Methods . 49 2.4 Java Classes Know Things: State . 50 2.4.1 Quick! Call the OBGYN! . 52 2.4.2 Method and Constructor Overloading . 53 2.4.3 Get a load of this ...................... 54 2.4.4 Now Let Us Make this Class DO Something . 55 2.4.5 Who am I? . 57 2.4.6 Mutator Methods . 58 2.5 The Scope of Java Variables . 60 2.6 The Object-Oriented Weltanschauung . 64 2.6.1 Procedural Programming . 64 2.6.2 Object{Oriented Programming . 65 3 Translating Python to Java 69 3.0 Introduction . 69 3.1 Java Data Structures . 69 3.1.1 Goodies inside of java.util.Arrays . 71 3.1.2 Fixed Size? I'm finding this very confining! . 71 3.1.3 A Brief but Necessary Diversion: What is this Object object? . 72 3.1.4 And Now Back to the Matter at Hand . 74 3.2 Conditional Execution in Java . 77 3.3 Extended{Precision Integer Arithmetic in Java . 79 3.4 Recursion in Java . 82 3.5 Looping in Java . 84 3.6 Static and final ........................... 85 3.6.1 Etiquette Between Static and Non-Static Members . 87 3.6.2 How do I Make my Class Executable? . 88 CONTENTS 5 3.7 The Wrapper Classes . 89 3.7.1 Autoboxing and Autounboxing . 90 3.8 A Caveat . 91 3.9 Case Study: An Extended-Precision Fraction Class . 92 3.9.1 Making a Proper Constructor and toString() Method . 93 3.10 Overloading the Constructor . 96 3.11 Creating an equals Method . 99 3.12 Hello Mrs. Wormwood! Adding Arithmetic . 100 3.13 The Role of static and final . 103 4 The Big Fraction Case Study 109 4.0 Case Study: An Extended-Precision Fraction Class . 109 4.0.1 A Brief Weltanschauung . 109 4.1 Start your Engines! . 110 4.2 Making a Proper Constructor and toString() Method . 111 4.3 Overloading the Constructor . 114 4.4 Creating an equals Method . 116 4.5 Hello Mrs. Wormwood! Adding Arithmetic . 118 4.6 The Role of static and final . 120 4.7 Using Javadoc . 125 4.7.1 Triggering Javadoc . 126 4.7.2 Documenting toString() and equals() . 127 4.7.3 Putting in a Preamble and Documenting the Static Con- stants . 128 4.7.4 Documenting Arithmetic . 129 4.7.5 The Complete Code . 131 5 Interfaces, Inheritance and Java GUIs 137 5.0 What is ahead? . 137 5.1 A Short GUI Program . 137 5.2 Inheritance . 140 5.2.1 Polymorphism and Delegation . 144 6 CONTENTS 5.2.2 Understanding More of the API Guide . 145 5.2.3 Deprecated Can't be Good . 146 5.2.4 Why Not Have Multiple Inheritance? . 146 5.2.5 A C++ Interlude . 146 5.3 Examining Final . 147 5.4 Back to the '70's with Cheesy Paneling, or I Can't Stand it, Call the Cops! . 148 5.4.1 Recursion is our Friend . 151 5.5 A Framework for our GUI Programs . 152 5.6 Creating a Complex View . 154 5.7 Interfaces . 160 5.7.1 The API Guide, Again . 162 5.8 Making a JButton live with ActionListener . 163 5.9 Inheritance and Graphics . 164 5.10 Abstract Classes . 166 6 The Tricolor Case Study 171 6.0 Introduction . 171 6.1 Building the View for Tricolor . 171 6.2 Our Panels Need to Know their Colors . 172 6.3 Inserting ColorPanels into the Tricolor App . 173 6.4 Le Carte . 174 6.5 It's time to build the controller! . 176 6.6 The Color Menu and the Controller . 177 6.7 The Position Menu and Its Controller . 182 6.8 All Code Shown . 184 6.8.1 Tricolor.java . 184 6.8.2 ColorPanel.java . 186 6.8.3 QuitListener.java . 187 6.8.4 ColorMenuItem.java . 187 6.8.5 ColorMenuItemListener.java . 188 CONTENTS 7 6.8.6 PositionMenuItem.java . 188 6.8.7 PositionMenuItemListener.java . 189 7 Inner Classes, Anonymous Classes and Java GUIs 191 7.0 What is ahead? . 191 7.1 Improving Tricolor . 191 7.2 Deconstructing this Arabesque . 192 7.3 Hammertime . 193 7.4 Using Inner Classes to Improve our Design . 194 7.5 The Position Menu . 199 7.6 Cruft Patrol! . 201 7.7 The Product . 201 7.8 Inner Classes in General . 204 7.9 Adding and Deleting Components from a JFrame . 206 8 Exception Handling 213 8.0 Introduction . 213 8.1 The Throwable Subtree . 213 8.2 Checked and Run-Time Exceptions . 215 8.2.1 Catching It . 216 8.3 A Simple Case Study . 216 8.4 All Code Shown . 222 8.5 Exception Handling, In General . 224 8.5.1 Can you have several catch blocks? . 224 8.5.2 The Bucket Principle . 225 8.6 Mr. Truman, We Must Pass the Buck! . 226 8.6.1 Must I? . 227 8.7 Can I Throw an Exception? . 228 8.7.1 Can I make my own exceptions? . 229 8.8 Summary . 230 9 Text File IO 231 8 CONTENTS 9.0 Introduction . 231 9.1 The File Class and Paths . 231 9.2 Constructors and Methods . 233 9.3 A Simple Case Study: Copying a File . 234 9.3.1 A Programming Idiom . 236 9.3.2 Buffered FileIO . 238 9.4 Opening a File in a GUI Window . 241 9.4.1 Designing the Application . 242 9.5 Swing's ImageIO Class . 245 10 The NitPad Case Study 249 10.0 Case Study: NitPad: A Text Editor . 249.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages339 Page
-
File Size-