Question

In: Computer Science

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

Write a program 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?

Solutions

Expert Solution

//program to have a Car class which is comparable.
public class Car implements Comparable {
//   Make the variables in Car class protected
protected int speed;
protected static int count;
public Car(int s) {
speed = s;
count++;
}
// Make the Car class comparable and use speed to compare cars
public int compareTo(Object o) {
Car other = (Car) o;
return this.speed - other.speed;
}
// how many cars are created so far by calling a static method called countCars.
public static int countCars(){
   return count;
}
@Override
   public String toString() {
       return "Car [speed=" + speed + "]";
   }
// a method in Main class that finds the minimum of 4 things (generically)
   public static Car minCar(Car cars[])
   {
       Car resultCar = cars[0];
       int min = resultCar.speed;
       for(int i=1;i<cars.length;i++)
       {
           if(cars[i].speed<min)
           {
               resultCar = cars[i];
               min = cars[i].speed;
           }
       }
       return resultCar;
   }
public static void main(String[] args) {
//     Create 4 cars and pass the cars to the minimum method and find the smallest car.
   Car cars[] = new Car[4];
  
       cars[0] = new Car(30);
   cars[1] = new Car(20);
   cars[2] = new Car(10);
   cars[3] = new Car(50);
  
   System.out.println(minCar(cars));
   }
}
//class called LuxCar that extends the class Car
class LuxCar extends Car{
//   dditional member variable called airCondition of type intger
   int airCondition;
  
//   with setters and getters
   public int getAirCondition() {
       return airCondition;
   }

   public void setAirCondition(int airCondition) {
       this.airCondition = airCondition;
   }

   public LuxCar(int speed,int airCondition) {
       super(speed);
       this.airCondition = airCondition;
   }
  
   public static void main(String[] args)
   {
//       Create 4 LuxCar objects and find the smallest on
       LuxCar luxCars[] = new LuxCar[4];
      
           luxCars[0] = new LuxCar(30,10);
       luxCars[1] = new LuxCar(20,20);
       luxCars[2] = new LuxCar(10,30);
       luxCars[3] = new LuxCar(50,40);
      
       System.out.println(minCar(luxCars));
   }
  
}


Related Solutions

Using the Note class and the Quadratic classes you have already developed, make each one Comparable....
Using the Note class and the Quadratic classes you have already developed, make each one Comparable. A Note is larger than another note if it is higher in frequency. A Quadratic is bigger than another Quadratic if it opens faster. 2. Write a driver for each (Note and for Quadratic). In the driver program create a few objects and compare them . then create a list of those objects and sort them. 3. Rewrite the Note class so that a...
In this class add Comparable interface. In the driver program create a few objects and In...
In this class add Comparable interface. In the driver program create a few objects and In the driver program create a few objects and compare them . then create a list of those objects and sort them .A Quadratic is bigger than another Quadratic if it opens faster package pack2; /** * This is a program for defining a quadratic equation * @author sonik */ public class Quadratic { public int coeffX2 = 0; public int coeffX = 0; public...
Task: Write a program that creates a class Apple and a tester to make sure the...
Task: Write a program that creates a class Apple and a tester to make sure the Apple class is crisp and delicious. Instructions: First create a class called Apple  The class Apple DOES NOT HAVE a main method  Some of the attributes of Apple are o Type: A string that describes the apple. It may only be of the following types:  Red Delicious  Golden Delicious  Gala  Granny Smith o Weight: A decimal value representing...
Write an inventory program in java for a used car lot. You should have one Car...
Write an inventory program in java for a used car lot. You should have one Car parent class and child classes for 3 vehicle types (convertible, SUV, Classic, etc). The parent class should have 3 variables and 3 functions, each child class should have 1 variable unique to it and 1 function. Your program should allow the user to store up to 100 cars in the inventory and write the cars out to a file when done.
Write a java program using the information given Design a class named Pet, which should have...
Write a java program using the information given Design a class named Pet, which should have the following fields (i.e. instance variables):  name - The name field holds the name of a pet (a String type)  type - The type field holds the type of animal that a pet is (a String type). Example values are “Dog”, “Cat”, and “Bird”.  age - The age field holds the pet’s age (an int type) Include accessor methods (i.e. get...
Car Class Write a class named Car that has the following member variables: • year. An...
Car Class Write a class named Car that has the following member variables: • year. An int that holds the car’s model year. • make. A string object that holds the make of the car. • speed. An int that holds the car’s current speed. In addition, the class should have the following member functions. • Constructor. The constructor should accept the car’s year and make as arguments and assign these values to the object’s year and make member variables....
Write a class Car that contains the following attributes: The name of car The direction of...
Write a class Car that contains the following attributes: The name of car The direction of car (E, W, N, S) The position of car (from imaginary zero point) The class has the following member functions: A constructor to initialize the attributes. Turn function to change the direction of car to one step right side (e.g. if the direction is to E,it should be changed to S and so on.) Overload the Turn function to change the direction to any...
Write a C program of car sale: The Visual Studio project itself must make its output...
Write a C program of car sale: The Visual Studio project itself must make its output to the Console (i.e. the Command Prompt using printf) and it must exhibit the following features as a minimum: 10%: Looping Menu with 2 main actions: Sell Car, View Sales Note: A Car is defined only by its price 10% Must contain at least one array containing sales figures (each entry represents the price of one vehicle) for a maximum of 10 Cars 5%:...
Write a program in which define a templated class mySort with private data members as a...
Write a program in which define a templated class mySort with private data members as a counter and an array (and anything else if required). Public member functions should include constructor(s), sortAlgorithm() and mySwap() functions (add more functions if you need). Main sorting logic resides in sortAlgorithm() and mySwap() function should be called inside it. Test your program inside main with integer, float and character datatypes.
Program must be in C++! Write a program which: Write a program which uses the following...
Program must be in C++! Write a program which: Write a program which uses the following arrays: empID: An array of 7 integers to hold employee identification numbers. The array should be initialized with the following values: 1, 2, 3, 4, 5, 6, 7. Hours: an array of seven integers to hold the number of hours worked by each employee. payRate: an array of seven doubles to hold each employee’s hourly pay rate. Wages: an array of seven doubles to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT