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 program that defines a class called circle. Circle should have a member variable...
java Create a program that defines a class called circle. Circle should have a member variable called radius that is used to store the radius of the circle. Circle should also have a member method called calcArea that calculates the area of the circle using the formula area = pi*r^2. Area should NOT be stored in a member variable of circle to avoid stale data. Use the value 3.14 for PI. For now, make radius public and access it directly...
(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...
Write Java program. 3x^4^ + 2x^2^ + 3x + 7 added to 2x^3^ + 4x +...
Write Java program. 3x^4^ + 2x^2^ + 3x + 7 added to 2x^3^ + 4x + 5 is equal to 3x^4^ + 2x^3^ + 2x^2^ + 7x + 12. Create an implementation of the ADT described above to create and add polynomials. Begin by defining a class Term that contains the exponent and coefficient. This class should implement the Comparable interface by comparing the values of the exponents. You should build a Polynomial class with a LinkedList<Term> instance variable, which...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT