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 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...
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.
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%:...
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...
Using class, write a program that: Body Mass Index (BMI) calculation - write a program that...
Using class, write a program that: Body Mass Index (BMI) calculation - write a program that takes users' input (weight and Height) and calculates the BMI. If the result is less than 18.5 display "you are underweight", if the result is greater than 18.5 display "you have a normal weight", if the result is greater than 24.9 display "your weight is considered overweight", and the result is greater than 30 display "your weight is considered as obese" (BMI = 703...
write a program to make scientific calculator in java
Problem StatementWrite a program that uses the java Math library and implement the functionality of a scientific calculator. Your program should have the following components:1. A main menu listing all the functionality of the calculator.2. Your program should use the switch structure to switch between various options of the calculator. Your Program should also have the provision to handle invalidoption selection by the user.3. Your calculator SHOULD HAVE the following functionalities. You can add any other additional features. PLEASE MAKE...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT