COMP200 - Object Oriented Programming: Test One

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

COMP200 - Object Oriented Programming:

Test One

1. Study the following classes and answer the questions that follow: MyPoint represents a single point in 2-
dimensional space and My3DPoint, a subclass of MyPoint, that represents a point in 3-dimensional space.
Both classes are in the same package.
package questionOne;

public class MyPoint {

int x = 0;
int y = 0;

public MyPoint(int x, int y) {


this.x = x;
this.y = y;
}

public int getX() {


return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}

public boolean isWeird(){


return this.x == this.y;
}
}

package questionOne;

public class My3DPoint extends MyPoint {

int z = 0;

public int getZ() {


return z;
}
public void setZ(int z) {
this.z = z;
}
}

1
COMP200 Test One 2018

1.1.) Will the class My3DPoint compile without error? Explain clearly why it will or will not compile. (2)

Solution: Call to implicit constructor super() will fail as there is no no-arg constructor in the
super-class.
It is not sufficient to say that there is no constructor - because the compiler inserts a no-arg default
constructor. Also it is not sufficient to say that there is no call to super - because the compiler also
inserts a call to super(). So the reason why this does not compile is that there is not no-argument
constructor in the super class, so the call to super() fails.

1.2.) For each of the following constructors, state whether the constructor can be added to the class My3DPoint
without causing a compile error. Give a reason if it will result in a compile error. (10)

i. public My3DPoint (int z){


super();
this.z = z;
}

Solution: Call to superclass constructor will fail as there is no no-arg constrcutor in the super
class.

ii. public My3DPoint(int x, int y){


super(x,y);
}

Solution: No error.
This is fine, even though the constructor does not initialize the field z. It is not required that
constructors initialize any or all of the fields. It is a good idea to do so, but it is not a compile
error.

iii. public My3DPoint(int z){


super(0,0);
this.z = z;
}

Solution: No error.

iv. public My3DPoint(int x, int y, int z){


this.z = z;
super(x,y);
}

Solution: Error. Call to super constructor must be first statement in the constructor.

v. public My3DPoint() {
this.z = 0;
}

Solution: Error, Call to superclass constructor will fail. In this case, the compiler inserts a
call to the no-arg super constructor. This fails since there is no no-arg constructor in the super
class.

COMP200 2 2018
COMP200 Test One 2018

1.3.) Assume that a correct constructor has been added to the class My3DPoint and the classes compile without
error.
State whether each of the following methods can be added to the class My3DPoint without resulting in a
compile error. Give a reason if it will result in a compile error. If it compiles without errors then state
whether the method is an override or an overload. (8)

i. public boolean isWeird () {


return false;
}

Solution: Can be added. is an override.

ii. public boolean isWeird (int q) {


if(q > z)
return true;
else
return false;
}

Solution: Can be added. is an overload.

iii. public int isWeird (int q) {


if(q > z)
return q;
else
return z;
}

Solution: Can be added. overload.

iv. public int isWeird () {


return 0;
}

Solution: Cannot be added. This is an override (we are not changing the argument list–so
cannot be an overload.) and overrides are not allowed to change the return type.

v. private boolean isWeird () {


return false;
}

Solution: Error. Not override because we cannot make visibility more restrictive. Not over-
load - arguments are the same.

COMP200 3 2018
COMP200 Test One 2018

2. Study the classes below and answer the questions that follow. Ques. 2 Total: 15
public class Vehicle {
private int numberOfWheels;

public Vehicle(int numWheels){


numberOfWheels = numWheels;
}
public int getNumberOfWheels(){
return numberOfWheels;
}
}

public class MotorisedVehicle extends Vehicle {


private int engineCapacity;

public MotorisedVehicle(int numWheels, int engineCapacity){


super(numWheels);
this.engineCapacity = engineCapacity;
}
public int getEngineCapacity(){
return engineCapacity;
}
}

public class TestVehicles {


public static void main(String[] args){}
}

2.1.) Write a subclass of MotorisedVehicle called Car that additionally has a field doors that stores the num-
ber of doors the car has. (5)

Solution:
public class Car extends MotorisedVehicle {

private int numDoors;

public Car(int numWheels, int engineCapacity, int numDoors){


super(numWheels, engineCapacity);
this.numDoors = numDoors;
}

public int getNumDoors() {


return numDoors;
}

COMP200 4 2018
COMP200 Test One 2018

2.2.) For each of the following state whether the statement/s may be added to the indicated classes without
causing compilation problems. If it will result in compilation problems then explain why.
i. The statement ArrayList<Vehicle> v = new ArrayList<Vehicle>(); may be added to the main
method without causing compilation problems.

Solution: No problems

ii. The statements:


Vehicle v = new MotorisedVehicle(4,2000);
System.out.println(v.getEngineCapacity());

may be added to the main method without causing compilation problems.

Solution: Compile error. Method not defined in type Vehicle. Note that the compiler checks
if the variable v is used properly. Since v is of type Vehicle, the compiler checks if the method
call is correct.

iii. The statements:


MotorisedVehicle v = new Vehicle(4);
System.out.println(v.getNumberOfWheels());

may be added to the main method without causing compilation problems.

Solution: Error. Type incompatibility. Cannot store super class reference in sub class type.

iv. The statements:


Vehicle v = new MotorisedVehicle(4,2000);
System.out.println(v.getNumberOfWheels());

may be added to the main method without causing compilation problems.

Solution: No error.

v. The statements:
Object o = new MotorisedVehicle(4,2000);
System.out.println(o.getNumberOfWheels());

may be added to the main method without causing compilation problems.

Solution: compile error. Method not defined in type Object.

(10)

COMP200 5 2018

You might also like