0% found this document useful (0 votes)
19K views5 pages

COMP200 - Object Oriented Programming: Test One

This document contains a test for an object-oriented programming course. It includes two questions. The first asks to write a Square subclass of the Polygon class. The second concerns accessibility of fields and methods in classes and subclasses across different packages. It asks to identify which fields and methods are directly accessible in different scenarios.

Uploaded by

Mhlengi Wiseman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19K views5 pages

COMP200 - Object Oriented Programming: Test One

This document contains a test for an object-oriented programming course. It includes two questions. The first asks to write a Square subclass of the Polygon class. The second concerns accessibility of fields and methods in classes and subclasses across different packages. It asks to identify which fields and methods are directly accessible in different scenarios.

Uploaded by

Mhlengi Wiseman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

COMP200 - Object Oriented Programming:

Test One

1. Study the following class Polygon represents a regular polygon (an n-sided figure with
all sides equal to each other). Write a complete subclass of Polygon called Square that
represents a square. (7)
package polygons;

public class Polygon {

private int numSides = 0;


private int side = 0; //length of a side

public Polygon (int numSides, int side){


this.numSides = numSides;
this.side = side;
}

public double getArea(){ return 0; }

public int getSide() { return side; }

public int getNumSides() {return numSides;}

public double getPerimeter(){


return numSides * side;
}

public String toString(){


return numSides + ” sided polygon”;
}

Solution:
public class Square extends Polygon {

public Square (int side){


super(4,side);
}

public double getArea(){


return super.getSide() * super.getSide();
}

1
COMP200 Test One 2019

2. Consider the class One in the package one with the following fields and a single construc-
tor.
package one;

public class One {


private int x;
int y;
public int z;
public final int V = 5;
protected String s;

public One(int x, int y, int z, String s) {


this.x = x;
this.y = y;
this.z = z;
this.s = s;
}

2.1.) When we say that a field is directly accessible we mean that one can write code such
as:
One o = new One(2,4,6,”yes”);
o.x = 12; //access the field directly
int q = o.y;
List the fields of One that are directly accessible from within the classes below :
i. List the fields of One that are directly accessible from within another class in the
package One (assuming that it correctly creates an object of One first).

Solution: y, z, V, s

ii. List the fields of One that are directly accessible from within any class in another
package (assuming that it correctly creates an object of One first).

Solution: z, V

iii. List the fields of One that are directly accessible from within any subclass of One
in the package One (assuming that it correctly creates an object of One first).

Solution: y, z, V, s

iv. List the fields of One that are directly accessible from within any subclass of One
in another package (assuming that it correctly creates an object of One first).

Solution: z, V, s

(8)

COMP200 2 2019
COMP200 Test One 2019

3. 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;
}

A subclass of Circular3DShape is shown below:


public class Cylinder extends Circular3DShape {

private double height;

public Cylinder(double radius, double height) {


super(radius);
this.height = height;
}

public double getHeight() {return height; }

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;
}

COMP200 3 2019
COMP200 Test One 2019

3.1.) State whether each of the following code snippets can be added to the class Cylinder
without causing a compile-time or a run-time error. If it cannot be added give reasons
why it will result in an error. (10)
i. public Cylinder(double radius) {
super();
super.setRadius(radius);
}

ii. public Cylinder(double radius) {


super.setRadius(radius);
}

iii. public int circumference(){


return (int) (super.circumference());
}

iv. public double area (double x){


return super.area() * x;
}

v. double area(){
return super.area();
}

Solution:
1. The call to the super class constructor, super(), will fail because the super
class does not contain a no-argument constructor. ie. super() is undefined in
the superclass.
2. The compiler will insert an implicit call to super() as the first line in the con-
structor because the programmer does not call a super class constructor. This
will fail because super() is undefined in the superclass
3. This is an override as the arguments have not changes. But you cannot change
the return type of an overridden method.
4. legal overload
5. Cannot reduce the visibility (i.e. cannot make the access more restrictive) of
the inherited method from Circular3DShape

3.2.) 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. (10)
i. Circular3DShape shape1 = new Cylinder(2,2);
shape1.getRadius();

ii. Circular3DShape shape1 = new Cylinder(2,2);


Cylinder shape2 = shape1;

iii. Circular3DShape shape1 = new Cylinder(2,2);


double vol = shape1.volume();

COMP200 4 2019
COMP200 Test One 2019

iv. Circular3DShape shape1 = new Cylinder(2,2);

if (shape1 instanceof Cylinder )


double vol = shape1.volume();
}

v. Object o = new Cylinder(2,2);


o.getRadius();

Solution:
• 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.

COMP200 5 2019

You might also like