



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
Material Type: Quiz; Class: Object-Oriented Programming and Design; Subject: COMPUTER SCIENCE; University: University of Arizona; Term: Unknown 1989;
Typology: Quizzes
1 / 5
This page cannot be seen from the preview
Don't miss anything!




Given class Point and a test driver for class Circle and class Rectangle, establish a properly designed inheritance hierarchy. Let Shape be the abstract class. Implement all three classes to the right of the non- inheritance version below. Completely implement all constructors and all methods. Include all instance variables. The Circle and Rectangle classes must behave exactly the same with the inheritance hierarchy as without. The assertions must pass. (22pts) @Test public void test getArea() { // 10 pixels over, 10 pixels down, radius 2. Shape c = new Circle(10, 10, 2.0); asssertEquals(3.14159, c.getArea(), 0.001); // width = 3.25, height = 5. Shape r = new Rectangle(40, 10, 3.25, 5.75); sssertEquals(18.6875, r.getArea(), 0.001); // width = 3.25, height = 5. Shape r2 = new Rectangle(40, 60, 2, 3); sssertEquals(6.0, r.getArea(), 0.1); } public class Point { // Use this Point class private int xPos; private int yPos; public Point(int x, int y) { xPos = x; yPos = y; } public int getX() { return xPos; } public int getY() { return yPos; } } public class Rectangle { private Point upperLeft; private double width; private double height; public Rectangle(int x, int y, double height, double width) { upperLeft = new Point(x, y); width = width; height = height; } public int getX() { return upperLeft.getX(); } public int getY() { return upperLeft.getY(); } public double getArea() { return width * height; } } public class Circle { private Point upperLeft; private double radius; public Circle(int x, int y, double diameter) { upperLeft = new Point(x,y); radius = diameter / 2; } public int getX() { return upperLeft.getX(); } public int getY() { return upperLeft.getY(); } public double getArea() { return Math.PI*Math.pow(radius, 2); } }
public abstract class Shape { private Point p; Shape(int x, int y) { p = new Point(x, y); } public int getX() { return p.getX(); } public int getY() { return p.getY(); } abstract double getArea(); } public class Rectangle extends Shape { private double my_width; private double my_height; public Rectangle(int x, int y, double height, double width) { super(x, y); my_width = width; my_height = height; } public double getArea() { return my_width * my_height; } } public class Circle extends Shape { private double my_radius; public Circle(int x, int y, double diameter) { super(x, y); my_radius = diameter / 2; } public double getArea() { return Math.PI * Math.pow(my_radius, 2); } }