Question

Write a program in Java to have a Car class which is comparable. Make the Car...

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?

Homework Answers

Answer #1

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.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write code for a JAVA class for a "Car". The Car class has 2 properties: modelType...
Write code for a JAVA class for a "Car". The Car class has 2 properties: modelType and currentSpeed. Make sure to write code for instance variables (the properties), getters/setters and a default constructor, overloaded constructor. ALSO, include one method called "accelerate" that increases the speed by "1.0" miles per hour
write a c++ program A class called car (as shown in the class diagram) contains: o...
write a c++ program A class called car (as shown in the class diagram) contains: o Four private variables: carId (int), carType (String), carSpeed (int) and numOfCars (int). numOfCars is a static counter that should be  Incremented whenever a new Car object is created.  Decremented whenever a Car object is destructed. o Two constructors (parametrized and copy constructor) and one destructor. o Getters and setters for the Car type, ID, and speed. And static getter function for numOfCars....
Exercise 2 Write a class named Vehicle Holds make and model (strings) and year (int) Contains...
Exercise 2 Write a class named Vehicle Holds make and model (strings) and year (int) Contains this method: Vehicle(String make, String model, int year) { this.make = make; this.model = model; this.year = year; } Add getters and setters for each variable Write a class called FordMustang that extends the Vehicle class               Create instance of object               Pass variables               Print
JAVA Learning Objectives: To be able to code a class structure with appropriate attributes and methods....
JAVA Learning Objectives: To be able to code a class structure with appropriate attributes and methods. To demonstrate the concept of inheritance. To be able to create different objects and use both default and overloaded constructors. Practice using encapsulation (setters and getters) and the toString method. Create a set of classes for various types of video content (TvShows, Movies, MiniSeries). Write a super or parent class that contains common attributes and subclasses with unique attributes for each class. Make sure...
(JAVA) Create a class that represents a Customer. Use good OO programming technique. A Customer has...
(JAVA) Create a class that represents a Customer. Use good OO programming technique. A Customer has a firstName, lastName, customerNumber, emailAddress. You can add some more fields if you want to, but those 4 are a minimum. Override the equals method, and the toString method from the Object class. Implement the Comparable interface. In the main method, create some instances of the Customer class, and demonstrate the use of the accessor and mutator methods, as well as the compareTo method.
Write the following problem in Java Create a class Dog, add name, breed, age, color as...
Write the following problem in Java Create a class Dog, add name, breed, age, color as the attributes. You need to choose the right types for those attributes. Create a constructor that requires no arguments. In this constructor, initialize name, breed, age, and color as you wish. Define a getter and a setter for each attribute. Define a method toString to return a String type, the returned string should have this information: “Hi, my name is Lucky. I am a...
Write the program in java Implement a class Product. Create instance variables to store product name...
Write the program in java Implement a class Product. Create instance variables to store product name and price and supply the values through constructor. For example new Product(“Toaster’, 29.95). Create methods, getName, getPrice. Write a method productPrinter that prints the product name and its price after reducing it by $5. Create a main class and necessary constructs in the main class to run the Product class.
Write a Java program that prompts the user to input a word (String). The program must...
Write a Java program that prompts the user to input a word (String). The program must print the reversed word with all consecutive duplicate characters removed. The program must contain the following classes: - The StackX class (you can use the Java Stack class). - The Reverse class which must contain a private data field called “word” of type string, a constructor, and a void method called revNoDup(). The revNoDup() method must reverse the word and remove the consecutive duplicate...
IN JAVA Inheritance Using super in the constructor Using super in the method Method overriding Write...
IN JAVA Inheritance Using super in the constructor Using super in the method Method overriding Write an application with five classes Vehicle, Car, Bus, Truck, and Tester class with main method in it. The following characteristics should be used: make, weight, height, length, maxSpeed, numberDoors, maxPassenges, isConvertable, numberSeats, maxWeightLoad, and numberAxels. Characteristics that are applicable to all vehicles should be instance variables in the Vehicle class. The others should be in the class(s) where they belong. Class vehicle should have...
Write the Game class, Java lanuage. A Game instance is described by three instance variables: gameName...
Write the Game class, Java lanuage. A Game instance is described by three instance variables: gameName (a String), numSold (an integer that represents the number of that type of game sold), and priceEach (a double that is the price of each of that type of Game). I only want three instance variables!! The class should have the following methods: A constructor that has two parameter – a String containing the name of the Game and a double containing its price....
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT