SOURCE CODE FILES FOR Classes2 EXAMPLES =========================================================== /** * Project Pair3 * Class Main * * Demonstrate the add and equals methods * in the Pair class. */ public class Main { public static void main(String[] args) { // Declare and create Pair objects. Pair s = new Pair(); Pair t = new Pair(3); Pair u = new Pair(8, 5); Pair v, w; System.out.println(s + " " + t + " " + u); v = s.add(t); // To create w, the anonymous object // (2, 5) is added to t, which is (3, 3). w = t.add(new Pair(2, 5)); System.out.println(v + " " + w); System.out.println(); // a.equals(b) if and only if x and y // components of object a equal the x and y // components of b. // a == b if and only if address in a equals // address in b. System.out.print(u.equals(u) + " "); System.out.println(u == u); System.out.print(u.equals(w) + " "); System.out.println(u == w); } } // Output: (0,0) (3,3) (8,5) (3,3) (8,5) true true true false ----------------------------------------------------------- /** * Project Pair3 * Class Pair * * The Pair class with add and equals methods. */ public class Pair { // Instance variables are x and y coordinates. private int x, y; // Noarg constructor. public Pair() { x = 0; y = 0; } // Constructor with one parameter. public Pair(int value) { x = value; y = value; } // Constructor with two arguments. public Pair(int xValue, int yValue) { x = xValue; y = yValue; } // Accessor method for x. public int getX() { return x; } // Accessor method for y. public int getY() { return y; } // toString method. public String toString() { return "(" + x + "," + y + ")"; } // Method for adding two Pair objects. // Addition is defined componentwise. public Pair add(Pair other) { Pair returnValue; returnValue = new Pair(x + other.x, y + other.y); return returnValue; } // Two pair objects are equal if both of the // components are equal. public boolean equals(Pair other) { return (x == other.x && y == other.y); } } =========================================================== /** * Project Kid4 * Class Main * * Test a Kid class with an equals method. * Two Kid objects are equal if their ages are * the same. (Alternatively, two kids are equal * if their names are the same.) */ public class Main { public static void main(String[] args) { Kid w = new Kid("Jane", 'F', 11); Kid x = new Kid("Terry", 'M', 12); Kid y = new Kid("Terry", 'F', 9); Kid z = new Kid("Scott", 'M', 11); System.out.println(x + " " + y + " " + z); System.out.println(); System.out.print(w.equals(y) + " "); System.out.print(x.equals(y) + " "); System.out.println(w.equals(z) + " "); } } // Output: Terry M 12 Terry F 9 Scott M 11 false false true ----------------------------------------------------------- /** * Project Kid4 * Class Kid * * A Kid class with an equals method. */ public class Kid { // Private instance variables. private String name; private char gender; private int age; // Noarg constructor; public Kid() { name = "Unknown"; gender = 'U'; age = -1; } // Parameterized constructor, which // performs data validation. public Kid(String aName, char aGender, int anAge) { // Don't allow empty string as a name. if (aName.equals("")) name = "Unknown"; else name = aName; // Gender must be 'F' or 'M'. if (aGender == 'F' || aGender == 'M') gender = aGender; else gender = 'U'; // Age must be nonnegative. if (anAge < 0) age = -1; else age = anAge; } // Accessor method for name. public String getName() { return name; } // Accessor method for gender. public char getGender() { return gender; } // Accessor method for name. public int getAge() { return age; } // Mutator method for gender. public void setGender(char aGender) { // Gender must be 'F' or 'M'. if (aGender == 'F' || aGender == 'M') gender = aGender; else gender = 'U'; } // hasBirthday method adds 1 to age. public void hasBirthday() { age++; } // toString method returns object // information as a string. public String toString() { return name + " " + gender + " " + age; } // Two Kid objects are "equal" if their ages // are equal public boolean equals(Kid other) { return age == other.age; // If you want to declare two Kid objects // "equal" if their names are the same, use // the following return statement: // return name.equal(other.name); } } ===========================================================