





Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
A part of the teaching materials for a university course on object-oriented programming (oop) at wellesley college. It includes information on the anatomy of a java class, access modifiers (public, protected, private), inheritance, and the use of interfaces. The document also includes examples of java code and problem sets for students.
Typology: Assignments
1 / 9
This page cannot be seen from the preview
Don't miss anything!






06- 1
Reading: Downey: Chapter 13; Carrano: p. 126- Problem Set: Assignment #2 due Friday, Feburary 23 Wellesley College CS Lecture 06 Thursday, February 15 Handout # 06- 2
06- 3 The Anatomy of a Java Class
06- 4 Class Example: A Counter Class import java.util.*; // imports Vector public class Counter { // Instance variables: protected int count; private int id; // Class variable: private static Vector
06- 7 Inheritance: A Resettable Counter
public Object(); public boolean equals (Object x); // this = x public String toString(); // string rep. of pointer public Counter (); public void inc (); // increments count public String toString(); // Overrides Object’s definition public ResettableCounter(); public void reset (); // resets count to 0 superclasses subclasses 06- 8 Inheritance: Resettable Counter Code public class ResettableCounter extends Counter { public void reset() { count = 0; // Won’t work if count is declared private in Counter! displayCounters(); } public static void main (String [] args) { ResettableCounter a = new ResettableCounter(); // [1 of 1:0]; a.inc(); // [1 of 1:1]; Counter b = new Counter(); // [1 of 2:1]; [2 of 2:0]; a.inc(); // [1 of 2:2]; [2 of 2:0]; b.inc(); // [1 of 2:2]; [2 of 2:1]; a.reset(); // [1 of 2:0]; [2 of 2:1]; a.inc(); // [1 of 2:1]; [2 of 2:1]; } } // The following is implicitly declared: public ResettableCounter() { super(); }
06- 9 Inheritance: An Inittable Counter
public Object(); public boolean equals (Object x); // this = x public String toString(); // string rep. of pointer public Counter (); public void inc (); // increments count public String toString(); // overrides Object’s definition public ResettableCounter(); public void reset (); // resets count to 0 superclasses subclasses
public InittableCounter(); public InittableCounter(int init); // initializes count to init 06- 10 Inheritance: Inittable Counter Code public class InittableCounter extends ResettableCounter { public InittableCounter (int init) { super(); // Call nullary Counter constructor; count = init; displayCounters(); } public InittableCounter () { this(0); } public static void main (String [] args) { InittableCounter a = new InittableCounter(17); // [1 of 1:0]; // [1 of 1:17]; a.inc(); // [1 of 1:18]; InittableCounter b = new InittableCounter(); // [1 of 2:18]; [2 of 2:0]; // [1 of 2:18]; [2 of 2:0]; a.inc(); // [1 of 2:19]; [2 of 2:0]; b.inc(); // [1 of 2:19]; [2 of 2:1]; a.reset(); // [1 of 2:0]; [2 of 2:1]; a.inc(); // [1 of 2:1]; [2 of 2:1]; } }
06- 13 Inheritance: An Equatable Counter
public Object(); public boolean equals (Object x); // this = x public String toString(); // string rep. of pointer public Counter (); public void inc (); // increments count public String toString(); // overrides Object’s definition public ResettableCounter(); public void reset (); // resets count to 0 superclasses subclasses
public InittableCounter(); public InittableCounter(int init); // initializes count to init
public EquatableCounter(); public boolean equals (Object x); // this.count = x.count 06- 14 Inheritance: EquatableCounter Code
e.equals(c) → true // asymmetric! e.equals(c2) → true e.equals(c3) → true e.equals(r) → true c.inc(); e.inc(); e.equals(c) → true e.equals(c2) → false e.equals(c3) → true e.equals(r) → false Counter c = new Counter(); Counter c2 = new Counter(); Counter c3 = c; Counter r = new ResettableCounter(); Counter e = new EquatableCounter(); c.equals(c2) → false c.equals(c3) → true c.equals(r) → false c.equals(e) → false
06- 15 Two Implementations of Immutable Points public class IPointVars { private int x, y; // Represent coords with two int variables. public IPointVars (int x, int y) {this.x = x; this.y = y;} public int getX() { return x; } public int getY() { return y; } public String toString() { return "(" + getX() + "," + getY() + ")"; } } public class IPointArray { private int[] ints = new int[2]; // Represent coords with two-slot array. public IPointArray (int x, int y) {ints[0] = x; ints[1] = y;} public int getX() { return ints[0]; } public int getY() { return ints[1]; } public String toString() { return "(" + getX() + "," + getY() + ")"; } } 06- 16 Manipulating Immutable Points public static int sumCoordsVars (IPointVars p) { return p.getX() + p.getY(); } public static int sumCoordsArray (IPointArray p) { return p.getX() + p.getY(); } The sumCoord methods are the same modulo the point type. Can we define a single sumCoords() method that works on instances of both classes? Yes! By using (1) an interface or (2) an abstract class.