December 1999
JP219 : JAVA PROGRAMMING

QUESTION 5

Total Marks: 15 Marks

Click here to access other questions

SUGGESTED SOLUTIONS
Solutions and allocated marks are indicated in green.
Return to
Question 5

 

A vehicular transportation device is a facility used by people to travel around Singapore. These devices have specific properties,namely the number of people they can carry,called People ,and a maximum speed,called Speed .Consider the following definition of a vehicular transportation device class,called VTD :

public class VTD {
  protected int People;
protected int Speed;
VTD(int p,int s){

 

}

People=p;
Speed=s;
}  

 

 
(a)

Define a class,called Car ,which inherits the properties of the VTD class,and also has the additional property of a Manufacturer ,which is a String .Your class should contain a constructor method which takes all the member attributes as parameters, correctly initialises all attributes and instantiates the base class with the appropriate parameters.
A sample definition of Car follows:
public class Car extends VTD {

protected String Manufacturer;

Car(int Capacity,int Speed,String M){

Manufacturer=M;
super(Capacity,Speed);

}

}

And the following marking scheme should be used:
•Correct use of the extends clause;(1 mark)
•Declaring the local member Manufacturer ;(1 mark)
•A correct constructor signature with parameter list;(1 mark)
•Initialising the Manufacturer member;(1 mark)
•Instantiating and initialising the super class with the correct parameter list. (1 mark)

 

[5]
(b)

A Skip is a large metal container,used for the storage of large obsolete industrial components.Define an interface ,called Skip which contains a method declaration called AddJunk ,which takes no parameters.
A sample definition of Skip follows:

interface Skip {
public boolean AddJunk();
}

And the following marking scheme should be used:
•Correctly defining the Skip as an interface;(1 mark)
•Correctly declaring the interface method AddJunk ;(1 mark)

 

[2]

(c)

A Ute is a very special type of vehicular transportation device,in that it can perform the function of a Car ,and can also be used as a Skip ,for the storage and disposal of junk. Implement a class,called Ute ,which inherits the properties of your Car class and your interface Skip .In addition to this,your class should include the following:

•The protected member NumberJunkItems ,of type int ;

•A constructor method,which takes all the member attributes as parameters,and instantiates and initialises them as appropriate.

•The implementation of the interface method AddJunk ,which simply increments the value of JunkCapacity .The implementation should ensure that no subsequent derived classes can overload this implementation.
A sample definition of Ute follows:
public class Ute extends Car implements Skip {

protected int NumberJunkItems;

Ute(int People,int Speed,String Manufacturer,

int initialJunk) {
super(People,Speed,Manufacturer);
NumberJunkItems=InitialJunk;

}

public final void AddJunk(){

NumberJunkItems++;

}

}

And the following marking scheme should be used:
•Declaring the class as extends Car ;(1 mark)
•Declaring the class as implements Skip ;(1 mark)
•Including the member NumberJunkItems ;(1 mark)
•Declaring the constructor signature with the correct parameter list;(1 mark)
•Instantiating the base class with the the correct parameters;(1 mark)
•Initialising the value of NumberJunkItems ;(1 mark)
•Declaring the signature of AddJunk to be final ;(1 mark)
•Incrementing the value of NumberJunkItems in the method AddJunk .(1 mark)

[8]