Patel Arth Lab4
Patel Arth Lab4
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;
interface Drivable
{
String howToDrive();
}
@Override
public boolean equals(Object obj)
{
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false;
@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);
}
}
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.");
}
}
}