// Torture test program #6 (try this one last!) // // GeoTester.txt - a JFlat sample program. // // Things to keep in mind about our language: // // Methods: // - can be overriden (replace a superclass's method with a specialized one). // - cannot be overloaded(several methods of the same name in a single class). // // Constructors: // - must call super() explicitly. // - can only have one constructor per class. // // Declarations: // - All classes must be declared public. // - no assignment at declaration: int foo = 5; // - Cannot do: Circle myCircle = new Circle(10); // Must break into: // Circle myCircle; // myCircle = new Circle(10); // // MethodInvocation: // foo.bar(); should work. // foo[1].bar(); will work. // (foo).bar(); will work. // public class GeoTester { public GeoTester() {super();} public static void main() { int transparentcount, magic; JFSystem sys; Shape s1; Shape s2; Shape s3; Square sq1; Square sq2; sys = new JFSystem(); s1 = new Shape(); s2 = new Circle(5); s3 = new Sphere(5); sq1 = new Square(5); sq2 = new Cube(5); transparentcount = 0; // How many of the shapes are transparent? if (s1.amItransparent()) { transparentcount = transparentcount +1; } if (s2.amItransparent()) { transparentcount = transparentcount +1; } if (s3.amItransparent()) { transparentcount = transparentcount +1; } if (sq1.amItransparent()) { transparentcount = transparentcount +1; } if (sq2.amItransparent()) { transparentcount = transparentcount +1; } sys.put(transparentcount); // The answer should be 3. // Calculate areas s2.calculateArea(); s3.calculateArea(); sq1.calculateArea(); sq2.calculateArea(); sys.put(((Circle)s2).area); // The answer should be 78 sys.put(((Circle)s3).area); // The answer should be 78 sys.put(sq1.area); // The answer should be 25 sys.put(sq2.area); // The answer should be 25 // Calculate volumes sys.put(s3.calculateVolume()); // The answer should be 523 sys.put(sq2.calculateVolume()); // The answer should be 125 // Print some magic numbers magic = ((Sphere)s3).magicNumber(); sys.put(magic); // The answer should be -2 for a sphere. magic = ((Cube)sq2).magicNumber(); sys.put(magic); // The answer should be -33 for a sphere. sys.put(((Circle)s2).secretnumber); // The answer should be 777, if secret got called. } } public class Shape { public boolean isTransparent; public Shape() { super(); isTransparent = true; } public boolean amItransparent() { return isTransparent; } public int calculateArea() {return 0;} public int calculateVolume() {return 0;} // public int magicNumber() {return 0;} } public class Circle extends Shape { public int radius; public int area; public int secretnumber; public Circle(int radius){ super(); this.radius = radius; area = 0; secretnumber =0; } public int calculateArea() { area = 22 * radius * radius / 7; this.secret(); return area; } private void secret() { secretnumber = 777; } public int magicNumber() { return 0-120; } } public class Square extends Shape { public int side_length; public int area; public Square(int side_length){ super(); this.side_length = side_length; area = 0; } public int calculateArea() { int dummy_side_length; dummy_side_length = side_length -1; area = side_length + side_length * dummy_side_length; return area; } } public class Sphere extends Circle { public int volume; public int specialValue; public Sphere(int radius){ super(radius); volume = 0; specialValue = 0; this.isTransparent = false; // 3-D shapes are not transparent. } public int calculateVolume() { volume = 4 * 22 * radius * radius * radius / 3 / 7; return (4 * 22 * radius * radius * radius / 3 / 7); } public int calculateBogus(boolean newTransparency, int returnval) { isTransparent = newTransparency; return returnval+1*5; } public int magicNumber() { return 0-1-1; } } public class Cube extends Square { public int volume; public Cube(int side_length){ super(side_length); this.isTransparent = false; // 3-D shapes are not transparent. volume = 0; } public int calculateVolume() { volume = side_length * side_length * side_length; return ((volume)); } public int magicNumber() { return 0-33; } }