COMP200 - Object Oriented Programming: Test One Duration - 60 minutes Study the following class and answer the questions that follow: package shapes3d; public class Circular3DShape { private double radius; public Circular3DShape(double radius) { this.radius = radius; public double getradius() { return radius; public void setradius(double radius) { this.radius = radius; public double diameter(){ return radius * 2; public double circumference(){ return 2 * Math.PI * radius; public double area(){ return Math.PI * radius * radius; 1. Write a sub-class of Circular3DShape called Cylinder. The class has an additional attribute (private double height) to represent the height of the cylinder. Besides the usual constructor/s and getters and setters, you should have the following additional methods: Override the area() method to return the surface area of the cylinder (surface area of a cylinder is given by (2 π r 2 ) + (2 π r height)). Write an additional method in the class volume() that returns the volume of the cylinder. The volume of a cylinder is calculated as follows: π r 2 height. Override the Object class s equals method to return true if the object input is equal to this cylinder. Two cylinders are equal iff they have equal heights and equal radii. (15) 1
public class Cylinder extends Circular3DShape { private double height; * @param radius * @param height public Cylinder(double radius, double height) { super(radius); this.height = height; * @return the height public double getheight() { return height; * @param height the height to set public void setheight(double height) { this.height = height; public double area(){ return (2 * Math.PI * super.getradius() * super.getradius()) + (2 * Math.PI * super.getradius() * height); public double volume(){ return Math.PI * super.getradius() * super.getradius() * height; public boolean equals(object o){ if(o instanceof Cylinder){ return ((Cylinder)o).getRadius() == super.getradius() && ((Cylinder)o).height == height; return false; COMP200 2 2012
2. Assume that the above mentioned classes Circular3DShape and Cylinder both compile without error and have the methods described above. A class Shapes3DApp with a main method is written within the package: For each of the following state whether the sets of statements will compile without error or not if they are added independently to the main method. If there are errors, explain the error briefly. 2.1.) Circular3DShape shape1 = new Cylinder(2,2); shape1.getradius(); 2.2.) Circular3DShape shape1 = new Cylinder(2,2); Cylinder shape2 = shape1; 2.3.) Circular3DShape shape1 = new Cylinder(2,2); double vol = shape1.volume(); 2.4.) Circular3DShape shape1 = new Cylinder(2,2); if (shape1 instanceof Cylinder ) double vol = shape1.volume(); 2.5.) Object o = new Cylinder(2,2); o.getradius(); 1. no error 2. error: shape1 is of superclass type. Cannot store a reference of this type in a subclass variable. 3. The method volume() is undefined for the type Circular3DShape 4. The method volume() is undefined for the type Circular3DShape. Need a typecast. 5. The method getradius() is undefined for the type Object. 3. Give the output of each of the if statements. If there will be no output, due to an error, state why that code snippet will result in an error. (NB. This section will be negatively marked - you will lose half a mark for each incorrect answer - SO no guessing!) (5) package shapes3d; public class Shapes3DApp { public static void main(string[] args) { Circular3DShape shape0 = new Circular3DShape(2); Circular3DShape shape1 = new Cylinder(2,2); Cylinder shape2 = new Cylinder(2,2); Object o = new Circular3DShape(2); if (shape1.equals(shape0) ) System.out.println( 1. equal ) ; System.out.println( 1. not equal ) ; if (shape1.equals(shape2) ) System.out.println( 2. equal ) ; System.out.println( 2. not equal ) ; COMP200 3 2012
if (shape1 == shape2 ) System.out.println( 3. equal ) ; System.out.println( 3. not equal ) ; if(shape0.equals(o)) System.out.println( 4. equal ) ; System.out.println( 4. not equal ) ; shape0 = shape2; if (shape1.equals(shape0) ) System.out.println( 5. equal ) ; System.out.println( 5. not equal ) ; 1. not equal 2. equal 3. not equal 4. not equal 5. equal 4. We want to create a subclass of Circular3DShape called Cylinder. The class Sphere has not additional attributes. Given the definition of the class Circular3DShape above: State whether each of the following code snippets can be added to the class Sphere without causing a compiletime or a run-time error. If it cannot be added give reasons why it will result in an error. (10) 4.1.) public Sphere(double radius) { super(); super.setradius(radius); 4.2.) public Sphere(double radius) { super.setradius(radius); 4.3.) public int circumference(){ return (int) (super.circumference()); 4.4.) public double area (double x){ return super.area() * x; 4.5.) double area(){ return super.area(); COMP200 4 2012
1. super() is undefined in the superclass. 2. implicit call to super() fails because super() is undefined in the superclass 3. override so cannot change the return type. 4. legal overload 5. Cannot reduce the visibility of the inherited method from Circular3DShape COMP200 5 2012