/* TestCylinder.java CIS 160 David Klick 2015-09-19 Demonstrates some object-oriented programming techniques. This code violates standards by placing several classes in the same file when there is no good reason for it. It is done in this case to present all the techniques together in one place. */ class Point { protected double x, y; public Point() { this(0.0, 0.0); } public Point(double x, double y) { setXY(x,y); } public Point(Point p) { this(p.x, p.y); } public void setXY(double x, double y) { setX(x); setY(y); } public void setX(double x) { this.x = x; } public void setY(double y) { this.y = y; } public double getX() { return x; } public double getY() { return y; } public String toString() { return "(" + x + "," + y + ")"; } public boolean equals(Point p) { return p.x==x && p.y==y; } } class Circle extends Point { protected double radius; public Circle() { this(0.0, 0.0, 1.0); } public Circle(double x, double y, double rad) { super(x, y); setRadius(rad); } public Circle(Circle c) { this(c.x, c.y, c.radius); } public void setRadius(double rad) { radius = rad; } public double getRadius() { return radius; } public double getArea() { return Math.PI * radius * radius; } public double getCircumference() { return 2.0 * Math.PI * radius; } public boolean equals(Circle c) { return c.x==x && c.y==y && c.radius==radius; } public String toString() { return super.toString() + ", r=" + radius; } } class Cylinder extends Object { private Circle c = new Circle(); private double height; public Cylinder(Circle cir, double hgt) { setX(cir.getX()); setY(cir.getY()); setRadius(cir.getRadius()); setHeight(hgt); } public void setX(double x) { c.setX(x); } public void setY(double y) { c.setY(y); } public void setRadius(double r) { c.setRadius(r); } public void setHeight(double h) { if (h < 0.0) h *= -1.0; height = h; } public double getX() { return c.getX(); } public double getY() { return c.getY(); } public double getRadius() { return c.getRadius(); } public double getHeight() { return height; } public double getArea() { return 2 * c.getArea() + c.getCircumference() * height; } public double getVolume() { return height * c.getArea(); } public String toString() { return c + ", h=" + height; } } public class TestCylinder { public static void main(String[] args) { System.out.println("Testing the Point class:"); Point p1 = new Point(); if (p1.getX() == 0.0 && p1.getY() == 0.0) { System.out.println("The Point() default constructor works"); } else { System.out.println("The Point() default constructor is broken"); } p1.setX(2.0); if (p1.getX() == 2.0) { System.out.println("Point.getX(), Point.setX() work"); } else { System.out.println("Point.getX() and/or Point.setX() is broken"); } p1.setY(4.0); if (p1.getY() == 4.0) { System.out.println("Point.getY(), Point.setY() work"); } else { System.out.println("Point.getY() and/or Point.setY() is broken"); } Point p2 = new Point(2.0, 3.0); if (p2.getX() == 2.0 && p2.getY() == 3.0) { System.out.println("The Point(double,double) constructor works"); } else { System.out.println("The Point(double,double) constructor is broken"); } if (!p1.equals(p2)) { System.out.println("Point.equals() seems to be working"); } else { System.out.println("Point.equals() is not working"); } p2.setY(4.0); if (p1.equals(p2)) { System.out.println("Point.equals() seems to be working"); } else { System.out.println("Point.equals() is not working"); } Point p3 = new Point(p1); if (p1.equals(p3)) { System.out.println("The Point copy constructor seems to be working"); } else { System.out.println("The Point copy constructor seems to be broken"); } System.out.println("(2.0,4.0) should be printed after this: " + p2); System.out.println(); System.out.println("Testing the Circle class:"); Circle c1 = new Circle(); if (c1.getX() == 0.0 && c1.getY() == 0.0 && c1.getRadius() == 1.0) { System.out.println("The Circle() default constructor works"); } else { System.out.println("The Circle() default constructor is broken"); } Circle c2 = new Circle(1.0, 2.0, 3.0); if (c2.getX() == 1.0 && c2.getY() == 2.0 && c2.getRadius() == 3.0) { System.out.println("The Circle(double, double, double) constructor works"); } else { System.out.println("The Circle(double, double, double) constructor is broken"); } Circle c3 = new Circle(c2); if (!c1.equals(c2) && c2.equals(c3)) { System.out.println("Circle.equals() seems to be working"); System.out.println("Circle's copy constructor seems to be working"); } else { System.out.println("Circle.equals() or the copy constructor seems to be broken"); } c3.setRadius(4.0); if (c3.getRadius() == 4.0) { System.out.println("Circle.getRadius() and Circle.setRadius() seem to be working"); } else { System.out.println("Circle.getRadius() and/or Circle.setRadius() seem to be broken"); } System.out.println("(1.0,2.0), r=4.0 should be printed here: " + c3); System.out.println("Test of Circle getArea() [50.265...]: " + c3.getArea()); System.out.println("Test of Circle getCircumference() [25.13...]: " + c3.getCircumference()); System.out.println(); System.out.println("Testing the Cylinder class:"); Cylinder cyl = new Cylinder(c3, -8.0); if (cyl.getX() == 1.0 && cyl.getY() == 2.0 && cyl.getRadius() == 4.0 && cyl.getHeight() == 8.0) { System.out.println("The Cylinder constructor works"); System.out.println("The Cylinder accessor methods for x, y, radius, and height work"); } else { System.out.println("The Cylinder constructor or some accessor method(s) is/are broken"); } cyl.setX(2.0); cyl.setY(3.0); cyl.setRadius(5.0); cyl.setHeight(10.0); if (cyl.getX() == 2.0 && cyl.getY() == 3.0 && cyl.getRadius() == 5.0 && cyl.getHeight() == 10.0) { System.out.println("The main Cylinder mutator and accessor methods work"); } else { System.out.println("Some Cylinder accessor/mutator method(s) is/are broken"); } System.out.println("(2.0,3.0), r=5.0, h=10.0 should be printed here: " + cyl); System.out.println("Test of Cylinder getArea() [471.238...]: " + cyl.getArea()); System.out.println("Test of Cylinder getVolume() [785.39...]: " + cyl.getVolume()); System.out.println(); } } /* Sample run: Testing the Point class: The Point() default constructor works Point.getX(), Point.setX() work Point.getY(), Point.setY() work The Point(double,double) constructor works Point.equals() seems to be working Point.equals() seems to be working The Point copy constructor seems to be working (2.0,4.0) should be printed after this: (2.0,4.0) Testing the Circle class: The Circle() default constructor works The Circle(double, double, double) constructor works Circle.equals() seems to be working Circle's copy constructor seems to be working Circle.getRadius() and Circle.setRadius() seem to be working (1.0,2.0), r=4.0 should be printed here: (1.0,2.0), r=4.0 Test of Circle getArea() [50.265...]: 50.26548245743669 Test of Circle getCircumference() [25.13...]: 25.132741228718345 Testing the Cylinder class: The Cylinder constructor works The Cylinder accessor methods for x, y, radius, and height work The main Cylinder mutator and accessor methods work (2.0,3.0), r=5.0, h=10.0 should be printed here: (2.0,3.0), r=5.0, h=10.0 Test of Cylinder getArea() [471.238...]: 471.23889803846896 Test of Cylinder getVolume() [785.39...]: 785.3981633974483 */