0% found this document useful (0 votes)
62 views5 pages

Patel Arth Lab4

The document describes creating an abstract Vehicle superclass with color and dateMade fields and get/set methods, along with an abstract steer() method. It then defines a Drivable interface with an abstract howToDrive() method. Finally, it creates a Car subclass that extends Vehicle, implements Drivable and Comparable, and overrides methods like steer() and howToDrive() while adding speed comparison.

Uploaded by

Arth Patel
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)
62 views5 pages

Patel Arth Lab4

The document describes creating an abstract Vehicle superclass with color and dateMade fields and get/set methods, along with an abstract steer() method. It then defines a Drivable interface with an abstract howToDrive() method. Finally, it creates a Car subclass that extends Vehicle, implements Drivable and Comparable, and overrides methods like steer() and howToDrive() while adding speed comparison.

Uploaded by

Arth Patel
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/ 5

Lab 4

Course: COSC 1047 Introduction to Computer Science II


Name: Arth Patel
Section: COSC 1047 J
Student number: 239548040

Q1
create a abstract supper class with private data fields (color and datemade). Provides
Get, set method for this class and also include abstract void method steer().

2- Define interface name drivable with abstract method name howtodrive() with
return value type string().
3- Create concrete subclass named Car that extends the Vehicle class and
implements the Drivable and Comparable interfaces. This class contains String
data field.
- In object class override the equal method.
- Implement steer() method
- Implement howToDrive() method that returns the “step on gas pedal”
- Implement the compareTo method that compare two cars on basis on ca speed.

Answer:

Methodology:
Firstly I created abstract superclass that name is "vehicle". after that I add two private dat fields
color and datemade. Implement get and set method for color and datemade. after that Declare
abstract method steer() in this class. Next i create Drivable interface and declare abstract method
in this class.
In the next step Creates subclass named Car that extends Vehicle and implements Drivable and
Comparable<Car>. creates constructor and add private data fields and implement the methods that
creates.
At last creates a test program for test classes and interface.
Code:

import java.util.Date;

abstract class Vehicle


{
private String color;
private Date dateMade;

public Vehicle(String color, Date dateMade)


{
this.color = color;
this.dateMade = dateMade;
}

public String getColor()


{
return color;
}

public void setColor(String color)


{
this.color = color;
}

public Date getDateMade()


{
return dateMade;
}

public void setDateMade(Date dateMade)


{
this.dateMade = dateMade;
}
public abstract void steer();
}

interface Drivable
{
String howToDrive();
}

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 int getSpeed()
{
return speed;
}

public String getModel()


{
return model;
}

public void setModel(String model)


{
this.model = model;
}

public void setSpeed(int speed)


{
this.speed = speed;
}

@Override
public boolean equals(Object obj)
{
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false;

Car otherCar = (Car) obj;


return model.equals(otherCar.model);
}

@Override
public void steer()
{
System.out.println("Please Turn the steering wheel.");
}
@Override
public String howToDrive()
{
return "Please Push gas pedal.";
}

@Override
public int compareTo(Car otherCar)
{
return Integer.compare(this.speed, otherCar.speed);
}
}

public class CarTest


{
public static void main(String[] args)
{
Car car_1 = new Car("Black", new Date(), "SUV", 140);
Car car_2 = new Car("Gr", new Date(), "Sports", 210);

if (car_1.equals(car_2))
{
System.out.println("car_1 and car_2 are similar.");
}
else
{
System.out.println("car_1 and car_2 are not similar.");
}

car_1.steer();

System.out.println(car_2.howToDrive());

if (car_1.compareTo(car_2) < 0)
{
System.out.println("car_1 is moving slow than car_2.");
}
else if (car_1.compareTo(car_2) > 0)
{
System.out.println("car_1 is moving fast than car_2.");
}
else
{
System.out.println("car_1 and car_2 both car have the equal speed.");
}
}
}

You might also like