0% found this document useful (0 votes)
98 views4 pages

Lab #4 - COSC 1047 Section K - 24W

The document describes a lab assignment to create classes for vehicles. It involves: 1. Creating an abstract Vehicle superclass with color and dateMade fields and associated getter/setter methods. It also contains an abstract steer() method. 2. Defining a Drivable interface with an abstract howToDrive() method returning a String. 3. Creating a concrete Car class that extends Vehicle and implements Drivable. It adds model and speed fields along with getter/setters. It overrides equals() and implements steer(), howToDrive(), and compareTo() based on speed.

Uploaded by

Rajath KP
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)
98 views4 pages

Lab #4 - COSC 1047 Section K - 24W

The document describes a lab assignment to create classes for vehicles. It involves: 1. Creating an abstract Vehicle superclass with color and dateMade fields and associated getter/setter methods. It also contains an abstract steer() method. 2. Defining a Drivable interface with an abstract howToDrive() method returning a String. 3. Creating a concrete Car class that extends Vehicle and implements Drivable. It adds model and speed fields along with getter/setters. It overrides equals() and implements steer(), howToDrive(), and compareTo() based on speed.

Uploaded by

Rajath KP
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/ 4

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;

public class Array {


public static void main(String[] args)
{
Vehicle v = new Vehicle ("red",new Date());
v.steer();
}

public static class Vehicle {


private String color;
private Date dateMade;
public Vehicle ( String color, Date dateMade)
{
dateMade = new java.util.Date();
this.color = color;
this.dateMade = dateMade;
}
public String getColor(){
return color;
}
public void setColor(final String color){
this.color = color;
}
public Date getDateMade(){

return dateMade;
}
public void setDateMade(final Date dateMade) {
this.dateMade = dateMade;
}

public void steer() {


}
}
}

2. Define an interface named Drivable that contains an abstract method named


howToDrive() with return value type String.

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:

public class Car extends Vehicle implements Drivable, Comparable<Car> {


private String model;
private int speed;

public Car(String color, Date dateMade, String model, int speed) {


super(color, dateMade);
this.model = model;
this.speed = speed;
}

public String getModel() {


return model;
}

public void setModel(String model) {


this.model = model;
}

public int getSpeed() {


return speed;
}

public void setSpeed(int speed) {


this.speed = speed;
}

@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();
}
}

You might also like