Write a program in Java to have a Car class which is comparable. Make the Car class comparable and
use speed to compare cars. Write a method in Main class that finds the minimum of 4 things
(generically). Create 4 cars and pass the cars to the minimum method and find the smallest car.
Have a toString method for the Car class. The main program should be able to find out how
many cars are created so far by calling a static method called countCars.
Create another class called LuxCar that extends the class Car. You may have an additional
member variable called airCondition of type intger (with setters and getters). Make the
variables in Car class protected. Create 4 LuxCar objects and find the smallest one. Does LuxCar
objects work with minimum method?
1.
//Car.java : Java class Car that implements Comparable
public class Car implements Comparable<Car>{
private String model;
private String make;
private int speed;
private static int numCars = 0;
// constructors
public Car()
{
model = "";
make="";
speed= 0;
numCars++;
}
public Car(String make, String model, int speed)
{
this.model = model;
this.make = make;
this.speed = speed;
numCars++;
}
// setters
public void setModel(String model)
{
this.model = model;
}
public void setMake(String make)
{
this.make = make;
}
public void setSpeed(int speed)
{
this.speed = speed;
}
// getters
public String getModel()
{
return model;
}
public String getMake()
{
return make;
}
public int getSpeed()
{
return speed;
}
public int compareTo(Car car)
{
// Cars are compared based on their speed
return(speed-car.getSpeed());
}
public static int countCars()
{
return numCars;
}
public String toString()
{
return("Make : "+make+" Model : "+model+" Speed : "+speed);
}
}
//end of Car.java
//Main.java
public class Main {
// Generic method to return the smallest of the 4 objects
public static <T extends Comparable<T>> T minimum(T obj1, T obj2, T obj3, T obj4)
{
T min = obj1;
if(min.compareTo(obj2) > 0)
min = obj2;
if(min.compareTo(obj3) > 0)
min = obj3;
if(min.compareTo(obj4) > 0)
min = obj4;
return min;
}
public static void main(String [] args)
{
Car car1 = new Car("Make1", "Model1",120);
Car car2 = new Car("Make2", "Model2",150);
Car car3 = new Car("Make3", "Model3",50);
Car car4 = new Car("Make4", "Model4",100);
System.out.println("Minimum Car : "+minimum(car1,car2,car3,car4));
System.out.println("Number of cars created : "+Car.countCars());
}
}
//end of Main.java
Output:
2.
//Car.java : Java class Car that implements Comparable
public class Car implements Comparable<Car>{
protected String model;
protected String make;
protected int speed;
protected static int numCars = 0;
// constructors
public Car()
{
model = "";
make="";
speed= 0;
numCars++;
}
public Car(String make, String model, int speed)
{
this.model = model;
this.make = make;
this.speed = speed;
numCars++;
}
// setters
public void setModel(String model)
{
this.model = model;
}
public void setMake(String make)
{
this.make = make;
}
public void setSpeed(int speed)
{
this.speed = speed;
}
// getters
public String getModel()
{
return model;
}
public String getMake()
{
return make;
}
public int getSpeed()
{
return speed;
}
public int compareTo(Car car)
{
// Cars are compared based on their speed
return(speed-car.getSpeed());
}
public static int countCars()
{
return numCars;
}
public String toString()
{
return("Make : "+make+" Model : "+model+" Speed : "+speed);
}
}
//end of Car.java
//LuxCar.java : Java class containing LuxCar
public class LuxCar extends Car{
private int airCondition ;
public LuxCar()
{
super();
airCondition = 0;
}
public LuxCar(String make, String model, int speed, int airCondition)
{
super(make,model,speed);
this.airCondition = airCondition;
}
public void setAirCondition(int airCondition)
{
this.airCondition = airCondition;
}
public int getAirCondition()
{
return airCondition;
}
public String toString()
{
return(super.toString()+" Air Condition : "+airCondition);
}
}
//end of LuxCar.java
//Main.java
public class Main {
// Generic method to return the smallest of the 4 objects
public static <T extends Comparable<T>> T minimum(T obj1, T obj2, T obj3, T obj4)
{
T min = obj1;
if(min.compareTo(obj2) > 0)
min = obj2;
if(min.compareTo(obj3) > 0)
min = obj3;
if(min.compareTo(obj4) > 0)
min = obj4;
return min;
}
public static void main(String [] args)
{
LuxCar car1 = new LuxCar("Make1", "Model1",120,1);
LuxCar car2 = new LuxCar("Make2", "Model2",150,2);
LuxCar car3 = new LuxCar("Make3", "Model3",50,3);
LuxCar car4 = new LuxCar("Make4", "Model4",100,4);
System.out.println(minimum(car1,car2,car3,car4));
System.out.println("Number of cars created : "+Car.countCars());
}
}
//end of Main.java
Output:
minimum method works for LuxCar also because its super class Car is Comparable<Car> and hence this property of Car is inherited by LuxCar which makes LuxCar also comparable (since LuxCar is a type of Car) and the minimum method uses the compareTo method of the Car object since LuxCar doesn't override the compareTo method.
Get Answers For Free
Most questions answered within 1 hours.