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

Write a class "car" with data fields "make" and "speed." The constructor should accept the "make"...
Write a class "car" with data fields "make" and "speed." The constructor should accept the "make" parameter. Be sure to use the "this" reference when setting the "make" parameter. The class should contain a static field for defaultSpeed set to 50. The class "car" should have a method "speed." The method should return the defaultSpeed. There should be an overloaded method for speed that takes a speed parameter. Finally, this class should take a getSpeed method that returns the speed....
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...
Java Write a class called Car that contains instance data that represents the make, model, and...
Java Write a class called Car that contains instance data that represents the make, model, and year of the car. Define the Car constructor to initialize these values Include getter and setter methods for all instance data and a toString method that returns a one-line description of the car. Add a method called isAntique that returns a boolean indicating if the car is an antique (if it is more than 45 years old). Create a driver class called CarTest, whose...
Write a C++ program where class 1 and class 2 (which is a base class) should...
Write a C++ program where class 1 and class 2 (which is a base class) should have a derived class (class 4 and class 5). Each of the derived classes should include at least 1 variable, 2 functions (one will be showing the function overriding case, the second one will be showing the function overloading case), 2 constructors (with default values, with parameters). Then class 3 (composition) (to relate class 1 and class 2) should include 3 variables (first variable...
1-Using the Note class and make it Comparable. A Note is larger than another note if...
1-Using the Note class and make it Comparable. A Note is larger than another note if it is higher in frequency. 2-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 list of Notes is sorted first by length of note, then within that by frequency. import java.io.*; import java.math.*; public class NoteTester { public static void main(String[] args) throws...
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 Python program that creates a class which represents a Student Grade. The class includes...
Write a Python program that creates a class which represents a Student Grade. The class includes a function that reads a text file called Course_Score.txt. A sample of the file is provided below. Each row in the file corresponds to the Last Name, First Name, ClassWork score (100), Mid-Term score (100), and Final-Exam score (100). Also include the the following functions to process the content read from the file. a. getData(): This method reads the data from a file and...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT