import java.awt.*; public class Lab6 { /** * Notice the use of polymorphism with the method call. We can't be sure what * type of objects will be passed in, but we don't have to care. Every object * in Java has an equals() and toString() method, which are all that this * method requires. With dynamic binding, the equals() method of MyRectangle, * MyColoredRectangle1, or MyColoredRectangle2 will be executed depending on * the actual type of the lhs object. * * @param lhs * left hand side (lhs) of "equals sign" * @param rhs * right hand side (rhs) of "equals sign" */ public static void testEquality(Object lhs, Object rhs) { if(lhs.equals(rhs)) System.out.println(lhs + " EQUALS " + rhs); else System.out.println(lhs + " is NOT EQUAL to " + rhs); } public static String forColor(Color c) { if(c == Color.black) return "black"; else if(c == Color.blue) return "blue"; else if(c == Color.cyan) return "cyan"; else if(c == Color.white) return "white"; else if(c == Color.gray) return "gray"; else if(c == Color.green) return "green"; else if(c == Color.orange) return "orange"; else if(c == Color.yellow) return "yellow"; else if(c == Color.red) return "red"; else if(c == Color.magenta) return "magenta"; return "unknown"; } public static void main(String[] args) { MyRectangle R0; MyColoredRectangle1 R1; MyColoredRectangle2 R2; R0 = new MyRectangle(50, 50, 70, 46); R1 = new MyColoredRectangle1(80, 20, 70, 46, Color.yellow); R2 = new MyColoredRectangle2(10, 10, 70, 46, Color.yellow); System.out.println("R0: " + R0); System.out.println("R1: " + R1); System.out.println("R2: " + R2); testEquality(R0, R1); testEquality(R1, R0); System.out.println(); testEquality(R0, R2); testEquality(R2, R0); System.out.println(); testEquality(R1, R2); testEquality(new MyColoredRectangle1(10, 11, 70, 46, Color.yellow), R1); testEquality(R2, new MyColoredRectangle2(10, 10, 70, 46, Color.green)); R1.setSize(80, 100); System.out.println(); testEquality(R0, R1); testEquality(R1, R0); } }