Object-Oriented Programming in Javatm
Total Page:16
File Type:pdf, Size:1020Kb
OBJECT-ORIENTED PROGRAMMING IN JAVATM Richard L. Halterman 2 March 2007 Draft © 2007 Richard L. Halterman. CC Some rights reserved. All the content of this document (including text, figures, and any other original works), unless otherwise noted, is licensed under a Creative Commons License. Attribution-NonCommercial-NoDerivs This document is free for non-commericial use with attribution and no derivatives. You are free to copy, distribute, and display this text under the following conditions: • BY: Attribution. You must attribute the work in the manner specified by the author or licensor. • $n Noncommercial. You may not use this work for commercial purposes. • = No Derivative Works. You may not alter, transform, or build upon this work. • For any reuse or distribution, you must make clear to others the license terms of this work. • Any of these conditions can be waived if you get permission from the copyright holder. Your fair use and other rights are in no way affected by the above. See http://www.creativecommons.org for more information. Java is a registered trademark of Sun Microsystems, Incorporated. DrJava Copyright © 2001-2003 was developed by the JavaPLT group at Rice University ([email protected]). Eclipse is an open-source project managed by the Eclipse Foundation (http://www.eclipse.org). 2 March 2007 Draft i Contents 1 The Context of Software Development 1 1.1 Software . .1 1.2 Development Tools . .2 1.3 The Java Development Environment . .5 1.4 The DrJava Development Environment . .9 1.5 Summary . 11 1.6 Exercises . 12 2 Values, Variables, and Types 14 2.1 Java Values in DrJava’s Interaction Pane . 14 2.2 Variables and Assignment . 17 2.3 Identifiers . 22 2.4 Summary . 24 2.5 Exercises . 24 3 Arithmetic Expressions 26 3.1 Expressions and Mathematical Operators . 26 3.2 String Concatenation . 29 3.3 Arithmetic Abbreviations . 30 3.4 Summary . 31 3.5 Exercises . 31 4 Objects: Packaging Computation 33 4.1 Classes . 33 4.2 Comments . 39 4.3 Local Variables . 40 4.4 Method Parameters . 42 2 March 2007 Draft © 2007 Richard L. Halterman CONTENTS ii 4.5 Summary . 43 4.6 Exercises . 44 5 Boolean Expressions and Conditional Execution 46 5.1 Relational Operators . 46 5.2 The if Statement . 49 5.3 The if/else Statement . 53 5.4 Summary . 54 5.5 Exercises . 55 6 More Complex Conditionals 56 6.1 Nested Conditionals . 56 6.2 Multi-way if/else Statements . 60 6.3 Errors in Conditional Expressions . 64 6.4 Summary . 64 6.5 Exercises . 65 7 Modeling Real Objects 66 7.1 Objects with State . 67 7.2 Traffic Light Example . 70 7.3 RationalNumber Example . 74 7.4 Object Aliasing . 77 7.5 Summary . 79 7.6 Exercises . 80 8 Living with Java 82 8.1 Standard Output . 82 8.2 Formatted Output . 83 8.3 Keyboard Input . 84 8.4 Source Code Formatting . 85 8.5 Errors . 87 8.6 Constants Revisited . 91 8.7 Encapsulation . 93 8.8 Summary . 95 8.9 Exercises . 96 2 March 2007 Draft © 2007 Richard L. Halterman CONTENTS iii 9 Class Members 97 9.1 Class Variables . 97 9.2 Class Methods . 100 9.3 Updated Rational Number Class . 100 9.4 An Example of Class Variables . 104 9.5 The main() Method . 105 9.6 Summary . 106 9.7 Exercises . 106 10 Composing Objects 109 10.1 Modeling Traffic Light Logic . 109 10.2 Textual Visualization of a Traffic Light . 111 10.3 Intersection Example . 115 10.4 Summary . 119 10.5 Exercises . 119 11 Inheritance and Polymorphism 121 11.1 Inheritance . 121 11.2 Protected Access . 129 11.3 Visualizing Inheritance . 130 11.4 Polymorphism . 132 11.5 Extended Rational Number Class . 133 11.6 Multiple Superclasses . 135 11.7 Summary . 135 11.8 Exercises . 136 12 Simple Graphics Programming 138 12.1 2D Graphics Concepts . 138 12.2 The Viewport Class . 139 12.3 Event Model . ..