Lab #4 - COSC 1047 Section K - 24W
Lab #4 - COSC 1047 Section K - 24W
Lab #4
Rajath Kannadian Puthiyapurayil
239575340
16 February 2024
1.Write an abstract superclass called Vehicle that contains the following private data
fields:
● colour: a string that holds the colour of the vehicle.
● dateMade: the date the vehicle was manufactured.
● Provide appropriate constructors and setter/getter methods for this class.
● Include an abstract void method named steer()
Answer:
import java.awt.*;
import java.util.Date;
return dateMade;
}
public void setDateMade(final Date dateMade) {
this.dateMade = dateMade;
}
Answer:
interface Drivable {
String howToDrive();
}
3. Write a concrete subclass named Car that extends the Vehicle class and
implements the Drivable and Comparable interfaces.
(i) The class should contain a String data field: model and an integer data field:
speed. Provide appropriate constructors and setter/getter methods for this class.
(ii) Override the equals method in the Object class. Two Car objects are equal if their
models are the same.
(iii) Implement the steer() method to display “Turn steering wheel.”
(iv) Implement the howToDrive() method that returns a string “Step on gas pedal.”
Implement the compareTo method to compare two cars on the basis of speed.
Answer:
@Override
public boolean equals(Object obj) {
if (obj instanceof Car) {
return this.model.equals(((Car) obj).getModel());
}
return false;
}
@Override
public void steer() {
System.out.println("Turn steering wheel.");
}
@Override
public String howToDrive() {
return "Step on gas pedal.";
}
@Override
public int compareTo(Car car) {
return this.speed - car.getSpeed();
}
}