COMP200 - Object Oriented Programming: Test One
COMP200 - Object Oriented Programming: Test One
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;
Solution:
public class Square extends Polygon {
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;
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;
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);
}
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();
COMP200 4 2019
COMP200 Test One 2019
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