| TeachJava! 2003 |
| Corky Cartwright | |
| Dung Nguyen | |
| Stephen Wong | |
| Charlie Reis, James Hsia, Neal Hororwitz, Peter Centgraf |
| From C++ To Java |
| C++ and Java have similar syntax. DonÕt be misled! |
|
| Profoundly different semantics: high-level objects vs. bytes in memory |
|
| Profoundly different programming
models: Java is object-oriented (OO) C++ is object-based (OB) in normal usage |
| Encouraging Note |
| Writing OO programs in Java is remarkably easyÑprovided we recognize that it is fundamentally different than writing OB programs in C++. | |
| There is little common conceptual ground, other than syntactic conventions, beween OO programing (OOP) in Java and OB programming in C++ |
| Guiding Vision |
| Program design in Java is data-directed. Design the data abstractions first; they will determine the structure of the code. In OOP circles, this data design process is often called object-modeling. |
|
| Common data abstractions are codified as design patterns. | |
| Secondary Theme |
| DrJava, our lightweight, reactive environment for Java, facilitates active learning; with DrJava learning Java is a form of exploration. | |
| DrJava is not a toy; DrJava is developed using DrJava. It includes everything that we believe is important and nothing more. |
| What Is an Object? |
| Collection of fields representing the properties of a conceptual or physical object. | |
| Collection of operations called methods for observing and changing the fields of the object. | |
| These fields and methods often called the members of the object. |
| Example: Phone Directory |
| Task: maintain a directory containing the office address and phone number for each person in the Rice Computer Science Dept. | ||
| Each entry in such a directory has a natural representation as an object with three fields containing a personÕs | ||
| name | ||
| address | ||
| phone number | ||
| represented as character strings. | ||
| Summary of Entry format |
| Fields: | ||
| String name | ||
| String address | ||
| String phone | ||
| Methods: | ||
| String getName() | ||
| String getAddress() | ||
| String getPhone() | ||
| Java Method Invocation |
| A Java method m is executed by sending
a method call o.m() |
|
| to an object o, called the receiver. The method m must be a member of o. | |
| The code defining the method m can refer to the receiver using the keyword this. |
| Finger Exercise |
| In the DrJava programming environment, open the program file Entry.java, compile it, and type the following statements in the Interactions pane: | |
| Entry e = new Entry("Corky","DH
3104","x 6042"); e.getName() |
|
| e.getPhone() | |
| Java Expressions |
| Java supports essentially the same expressions over primitive types (int, float, double, boolean) as C++. | ||
| Notable differences: | ||
| boolean is a distinct type from int | ||
| no unsigned version of integer types | ||
| explicit long type | ||
| Finger Exercise |
| Evaluate the following: | ||
| -5 + 3 | ||
| -(5 + 3) | ||
| 5 % 3 | ||
| 5./3. | ||
| 5 / 0 | ||
| 5./0. | ||
| 5 < 6 | ||
| 5. > 6. | ||
| Finger Exercise cont. |
| 72. - 32. * 1.8 | |
| (72. - 32.) * 1.8 | |
| 72. - 30. - 12. | |
| 72. - (30. - 12.) |
| Java Statements |
| Essentially the same form as in C++:
assignment, if, while, for, return, É |
|
| But well-written Java programs consist primarily of assignment, if, and return statements (with smattering of for). | |
| Focus on assignment and if for most of the week. |
| Assignment |
| Restricted form of assignment: variable definition | |
| type var = exp; | |
| Example | |
| int x = 5; |
| Finger Exercise |
| int x = 5; | |
| x*x | |
| double d = .000001; | |
| double dd = d*d; | |
| dd | |
| dd*dd | |
| 1. + dd | |
| 1. + dd*dd |
| Finger Exercise cont. |
| Evaluate: | ||
| int x = 7; | ||
| if (x = 5) y = 0; else y = 10; | ||
| y | ||
| Did you get the behavior that you expected? Repeat the exercise with corrected syntax. | ||
| Evaluate: | ||
| boolean switch1 = (x = 7); | ||
| switch1 | ||
| Repeat the exercise with corrected syntax. | ||
| Classes: Object Templates |
| A Java program is a collection of classes. | |
| A class is an object template consisting of a collection of membersÑfields and methodsÑ plus a constructor, which is a special method used to initialize objects when created. |
| Example: Class Entry |
| class Entry { | |
| /* fields */ | |
| String name; | |
| String address; | |
| String phone; | |
| /* constructor */ | |
| Entry(String n, String a, String p) { | |
| this.name = n; | |
| this.address = a; | |
| this.phone = p; | |
| } | |
| /* getters */ | |
| String getName() { return this.name; } | |
| String getAddress() { return this.address; } | |
| String getPhone() { return this.phone; } | |
| } |
| Finger Exercise |
| In the Definitions pane of DrJava, enter the Java program defining the Entry class. In the Interactions pane, evaluate the following program text: | ||
| Entry e = new Entry("Corky", "DH 3104", "x 6042"); | ||
| e.getName() | ||
| e.getAddress() | ||
| e.getPhone() | ||
| Save your program for future use in a file named Entry.java. | ||
| Terminology |
| The methods that return the fields of a class are called getters. | |
| The special method with the same name as the class is called the constructor. The constructor is executed when a new instance (object) of the class is created using new. |
| Java Data Types |
| Two fundamental categories | ||
| Primitive types: int, boolean, double, float, char, long, short, byte (first three are most common) | ||
| Object types: all class instances (objects) belong to object types, which are disjoint from the primitive types | ||
| Values of primitive type (e.g., true, 0) are not objects | ||
| Object Types |
| Organized in a strict hierarchy with the universal type Object at the top. | |
| Every class C except Object has an immediate superclass, which is the parent of C in the hierarchy. In a class definition (like our Entry example), the default superclass is Object. | |
| A descendant in the class hierarchy is called a subclass. B is a subclass of A iff A is a superclass of B. Entry is a subclass of Object; Object is a superclass of Entry. |
| null |
| There is a special value null of object type on which method invocation utterly fails. | |
| null is a reference to nothing; the method invocation | |
| null.m() | |
| always generates a NullPointerException aborting execution. |
| Example: the String class |
| The String class is built-in to Java, just like Object. | ||
| Finger Exercise: evaluate | ||
| String s = "Corky"; | ||
| Object o = s; | ||
| o | ||
| o == s | ||
| String t = "Cork" + "yÓ; | ||
| t == s | ||
| s.length() | ||
| o = null; | ||
| o.length() | ||
| Morals: | ||
| multiple copies of the same String may exist; do not use == to test String equality. | ||
| Do not use null to represent legitimate data values. | ||
| Object Types cont. |
| Each subclass C inherits (includes) all of the members of its superclass. | |
| The declared members of C augment the inherited members with one exception: if C declares a method m with exactly the same name and types as an inherited method, then the new definition of m overrides (replaces) the inherited definition. |
| Inheritance from Object |
| The Object class has several members that its children inherit. They include the methods | ||
| public String toString() which gives a String representation for the object. | ||
| public boolean equals(Object o) which compares this to o | ||
| Example of Overriding |
| class Entry { | |
| /* fields */ | |
| String name; | |
| String address; | |
| String phone; | |
| /* constructor */ | |
| Entry(String n, String a, String p) { | |
| this.name = n; | |
| this.address = a; | |
| this.phone = p; | |
| } | |
| /* accessors */ | |
| String getName() { return this.name; } | |
| String getAddress() { return this.address; } | |
| String getPhone() { return this.phone; } | |
| /* overridden methods */ | |
| public String toString() { | |
| return "[" + name + address + phone + "]"; | |
| } | |
| } | |
| Finger Exercise |
| Open your Entry class into the DrJava Interactions pane. | |
| Compile your program and evaluate: | |
| Entry e = new Entry("Corky", "DH 3104", "x 6042"); e |
|
| Add the definition of toString() from the previous slide to your Entry class. | |
| Compile your program and evaluate: | |
| Entry e = new Entry("Corky", "DH
3104", "x 6042"); e |
| Adding a Method Definition |
| class Entry { | |
| É | |
| /** return this if its name matches keyName; otherwise return null indicating failure */ | |
| Entry match(String keyName) { | |
| if (keyName.equals(this.name)) return this; else return null |
|
| } | |
| } | |
| Finger Exercise: Add this method definition to your Entry class, compile it, and evaluate: | |
| Entry e = new Entry("Corky", "DH
3104", "x 6042"); e.match("Corky") |
|
| e.match("Matthias") | |
| The Wrapper Classes |
| How do we treat primitive values as objects? Java includes a built-in ÒwrapperÓ class for each primitive type. | ||
| Examples: | ||
| Integer is the wrapper class for int | ||
| Boolean is the wrapper class for boolean | ||
| Double is the wrapper class for double | ||
| Finger Exercise |
| In the DrJava Interactions evaluate the following: | ||
| Integer i = new Integer(5); | ||
| i | ||
| Integer j = i; | ||
| i == j | ||
| Integer k = new Integer(5); | ||
| i == k | ||
| Constructors |
| Given a class definition, Java provides a mechanism called new for creating new instances of the class. | |
| To exploit the new, the class must provide a special method called a constructor that specifies how the fields of the created object are initialized. | |
| A constructor method has the same name as the class and does not contain the return type in the heading. | |
| Example: | |
| Entry(String n, String a, String p) { | |
| this.name = n; | |
| this.address = a; | |
| this.phone = p; | |
| } | |
| If a class does not include a constructor, Java provides a default constructor (of no arguments) that does nothing. | |
| Slide 34 |
| Union Pattern |
| The union pattern is used to represent different forms of related data with some common behavior. | |
| The pattern consists of an abstract class A together with a collection of variant subclasses B1, ..., BN extending A. An abstract class cannot be instantiated using new. | |
| The collection of classes A, B1, ..., BN is called a union hierarchy and A classes is called the root class of the hierarchy. | |
| The common behavior is codified by a set of methods in A, which may be abstract Each such method m has an associated contract that that the implementation in each variant class must obey. |
| Class Diagram of Union Pattern |
| Defining a Method on a Union |
| City Directory Example |
| Assume that we want to design the data for an online city phone book. In contrast to our DeptDirectory example, such a directory will contain several different kinds of listings: businesses, residences, and government agencies. | |
| The entry data for such a directory is represented by using the union pattern to identify the common behavior among the various kinds of listings. | |
| In this case, the common behavior is the existence of a String name for the listing and the String text of the listing (given by toString()). |
| Definition of CityEntry |
| A CityEntry is either: | ||
| a ResidentialEntry(name,address,phone) | ||
| a BusinessEntry(name,address,phone,city,state) | ||
| a GovernmentEntry(name,address,phone,city, state, government) |
||
| Examples: | ||
| ResidentialEntry("Corky Cartwright","3310 Underwood", "713-660-6967") | ||
| BusinessEntry("ToysRUs","2101
Old Spanish Trail", "713-664-1234","Houston", "TX") |
||
| GovernmentEntry("Federal Drug
Administration", "800-666-9000", "Washington", "DC", "Federal") |
||
| Class Diagram of CityEntry Union |
| Defining Methods on Unions |
| Assume that we want to define a method on a union. The method will typically require a separate implementation for each variant (subclass) of the union. But each implementation will satisfy the same "contract" (description of behavior). | |
| In Java, the method must not only be defined in each variant of the union, it must be declared as abstract in the root class of the union hierarchy. Otherwise, Java will not allow the method to be invoked on objects of the union type. |
| Methods for CityEntry |
| To process entries in a city directory, we need two methods: | |
/** Returns the name of this CityEntry */ |
|
| String getName(); | |
| /** Returns all information in this CityEntry */ | |
| String toString() | |
| The toString() method is already declared in Object (the superclass of CityEntry) but the provided default behavior does not satisfy the contract given above, so it must be overridden in each variant. | |
| The getName must be declared as abstract in CityEntry. | |
| CityEntry |
| abstract class CityEntry { | |
| /** Returns the name for the entry */ | |
| abstract String getName(); | |
| /** Returns all of the information in the listing in String form*/ | |
| /* String toString() */ | |
| } | |
| class BusinessEntry extends CityEntry { | |
| String name, address, phone, city, state; | |
| BusinessEntry(String n, String a, String p, String c, String s) { | |
| this.name = n; | |
| this.address = a; | |
| this.phone = p; | |
| this.city = c; | |
| this.state = s; | |
| } | |
| String getName() { return this.name; } | |
| public String toString() { | |
| return "Business[" + this.name + "," + this.address + "," + | |
| this.phone + "," + this.city + "," + this.state + "]"; | |
| } | |
| } |
| CityEntry cont. |
| class GovernmentEntry extends CityEntry { | |
| String name, address, phone, city, state, government; | |
| GovernmentEntry(String n, String a, String p, String c, String s, String g) { | |
| this.name = n; | |
| this.address = a; | |
| this.phone = p; | |
| this.city = c; | |
| this.state = s; | |
| this.government = g; | |
| } | |
| String getName() { return this.name; } | |
| public String toString() { | |
| return "Government[" + this.name + "," + this.address + "," + this.phone + | |
| "," + this.city + "," + this.state + "," + this.government + "]"; | |
| } | |
| } | |
| class Residential Entry extends CityEntry { | |
| String name, address, phone, city; | |
| ResidentialEntry(String n, String a, String p) { | |
| this.name = n; | |
| this.address = a; | |
| this.phone = p; | |
| } | |
| String getName() { return this.name; } | |
| public String toString() { | |
| return "Residential[" + this.name + "," + this.address + "," + this.phone + "]"; | |
| } | |
| } | |
| Member Hoisting |
| In a union hierarchy, the same code may be repeated in every variant. | |
| A cardinal rule of software engineering is never duplicate code. We can eliminate code duplication in a union hierarchy by hoisting duplicated code (code that is invariant within the union) into the abstract class at the route of the hierarchy. |
| CityEntry Example |
| We can hoist the fields name, address, and phone and the getters getAddress and getPhone. To perform this transformation we must introduce a constructor in the abstract class CityEntry to initialize the fields declared in the class. This constructor can be called from subclass constructors by using the special method name super. | |
| This hoisting process does not affect the form of the class diagram. |
| CityEntry II |
| abstract class CityEntry { | |
| String name, address, phone; | |
| CityEntry(String n, String a, String p) { | |
| name = n; | |
| address = a; | |
| phone = p; | |
| } | |
| String getName() { return name; } | |
| String getAddress() { return address; } | |
| String getPhone() { return phone; } | |
| /** Returns all of the information in the listing */ | |
| /* String toString() */ | |
| } | |
| class BusinessEntry extends CityEntry { | |
| String city, state; | |
| BusinessEntry(String n, String a, String p, String c, String s) { | |
| super(n,a,p); | |
| this.city = c; | |
| this.state = s; | |
| } | |
| String getCity() { return this.city; } | |
| String getState() { return this.state; } | |
| public String toString() { | |
| return "Business[" + this.getName() + "," + this.getAddress() + "," + | |
| this.getPhone() + "," + this.city + "," + this.state + "]"; | |
| } | |
| } |
| CityEntry II cont. |
| class GovernmentEntry extends CityEntry { | |
| String city, state, government; | |
| GovernmentEntry(String n, String a, String p, String c, String s, String g) { | |
| super(n,a,p); | |
| this.city = c; | |
| this.state = s; | |
| this.government = g; | |
| } | |
| String getCity() { return this.city; } | |
| String getState() { return this.state; } | |
| String getGovernment() { return this.government; } | |
| public String toString() { | |
| return "Government[" + this.getName() + "," + this.getAddress() + "," + | |
| this.getPhone() + "," + this.city + "," + this.state + "," + | |
| this.government + "]"; | |
| } | |
| } | |
| class ResidentialEntry extends CityEntry { | |
| ResidentialEntry(String n, String a, String p) { super(n,a,p); } | |
| public String toString() { | |
| return "Residential[" + this.name + "," + this.address + "," + this.phone + | |
| "]"; | |
| } | |
| } | |
| Partial Hoisting |
| In a union hierarchy, the same code may be repeated in some proper subset of the variants. | |
| We can eliminate this code duplication by introducing a new abstract class that is a superclass only of the variants that repeat the same code. | |
| Partial hoisting modifies the form of the class diagram because it introduces a new abstract class below the root abstract class of the union. |
| Revised Class Hierarchy |
| CityEntry III |
| abstract class CityEntry { | |
| String name, address, phone; | |
| CityEntry(String n, String a, String p) { | |
| name = n; | |
| address = a; | |
| phone = p; | |
| } | |
| String getName() { return name; } | |
| String getAddress() { return address; } | |
| String getPhone() { return phone; } | |
| /** Returns all of the information in the listing */ | |
| /* String toString() */ | |
| } | |
| abstract class NonResidentialEntry { | |
| String city, state; | |
| NonResidentialEntry(String n, String a, String p, String c, String s) { | |
| super(n, a, p); | |
| city = c; | |
| state = s; | |
| } | |
| String getCity() { return this.city; } | |
| String getState() { return this.state; } | |
| } | |
| CityEntry III cont. |
| class BusinessEntry extends NonResidentialEntry { | |
| BusinessEntry(String n, String a, String p, String c, String s) { super(n,a,p,c,s); } | |
| public String toString() { | |
| return "Business[" + this.getName() + "," + this.getAddress() + "," + | |
| this.getPhone() + "," + this.getCity() + "," + this.getState() + "]"; | |
| } | |
| } | |
| class GovernmentEntry extends CityEntry { | |
| String government; | |
| GovernmentEntry(String n, String a, String p, String c, String s, String g) { | |
| super(n,a,p,c,s); | |
| this.government = g; | |
| } | |
| String getGovernment() { return this.government; } | |
| public String toString() { | |
| return "Government[" + this.getName() + "," + this.getAddress() + "," + this.getPhone() + | |
| "," + this.getCity() + "," + this.getState() + "," + this.getGovernment() + "]"; | |
| } | |
| } | |
| class Residential Entry extends CityEntry { | |
| ResidentialEntry(String n, String a, String p) { super(n,a,p); } | |
| public String toString() { | |
| return "Residential[" + this.name + "," + this.address + "," + this.phone + "]"; | |
| } | |
| } | |
| Defining DeptDirectory Data |
| A DeptDirectory is either: | ||
| Empty(), the empty DeptDirectory , or | ||
| NonEmpty(first,rest), a non-empty DeptDirectory, where first is an Entry and rest is a DeptDirectory | ||
| Examples: | ||
| Empty() NonEmpty(Entry("Stephen","DH 3103","x 3846"), Empty()) |
||
| Class Diagram for DeptDirectory |
| DeptDirectory |
| The Composite Pattern |
| The DeptDirectory class hierarchy is a special form of union called a composite. A union is composite when one or more variants contain fields of the root class type. In other words, variants in the union contain references to objects of union type. In the DeptDirectory example, the NonEmpty variant contains a field rest of type DeptDirectory. | |
| Searching a DeptDirectory |
| Given the name of a person, we want to find that person's phone number in our DeptDirectory. In other words, we want to define a method on the DeptDirectory class | ||
| findAddress(String keyName) | ||
| that searches "this" DeptDirectory for an Entry that matches the argument keyName. | ||
| The Interpreter Pattern |
| To define a method m on a composite class like DeptDirectory, we follow the same process as we would in defining a method on a union class, with one new wrinkle. In the variants that refer to the composite class (have fields of composite class type), computing m for this will usually involve delegating the task of computing m for the embedded references to the composite class. | |
| In our DeptDirectory example, the only embedded reference to DeptDirectory in variant subclasses is the rest field in NonEmpty. | |
| Template for coding findPhone |
| abstract class DeptDirectory { | |
| abstract String findPhone(String key); | |
| F | |
| class Empty extends DeptDirectory { | |
| String findPhone(String key) { É } | |
| public String toString() { return "Empty()"; } | |
| } | |
| class Cons extends DeptDirectory { | |
| Entry first; | |
| DeptDirectory rest; | |
| Cons(Entry f, DeptDirectory r) { | |
| this.first = f; | |
| this.rest = r; | |
| } | |
| Entry getFirst() { return this.first; } | |
| DeptDirectory getRest() { return this.rest; } | |
| String findPhone(String key) { | |
| É rest.findPhone(key) É | |
| } | |
| public String toString() { return "NonEmpty(" + first + ", " + rest + ")"; } | |
| } |
| findPhone |
| abstract class DeptDirectory { | |
| abstract String findPhone(String key); | |
| } | |
| class Empty extends DeptDirectory { | |
| String findPhone(String key) { | |
| return null; | |
| } | |
| public String toString() { return "Empty()"; } | |
| } | |
| class NonEmpty extends DeptDirectory { | |
| Entry first; | |
| DeptDirectory rest; | |
| NonEmpty(Entry f, DeptDirectory r) { | |
| this.first = f; | |
| this.rest = r; | |
| } | |
| Entry getFirst() { return this.first; } | |
| DeptDirectory getRest() { return this.rest; } | |
| String findPhone(String key) { | |
| if (key.equals(first.getName())) | |
| return first.getPhone(); | |
| else return rest.findPhone(key); | |
| } | |
| public String toString() { return "NonEmpty(" + first + ", " + rest + ")"; } | |
| } | |