Create a class called Vehicle that implements the Comparable

Create a class called Vehicle that implements the Comparable interface. Vehicle will contain a member variable to hold the make of the vehicle, a constructor that sets up the make, and three methods (getMake, compareTo, toString). getMake returns the member variable, compareTo works as defined by the Comparable interface, and toString returns the name neatly formatted. Second, you\'ll compare Vehicles by their makes. Include a main method in Vehicle that asks the user to input eight makes and generates eight Vehicle objects. A blank make should not be used to create an object. Using the compareTo method, determine first and last vehicle among them and print them. Do not simply sort the Vehicle data.

Sample Output:

Sample output output SER200 HW11 (run) #3 Please enter the vehicle\'s make or a blank line to quit Toyota Please enter the vehicle\'s make or a blank line to quit Nissan Please enter the vehicle\'s make or a blank line to quit Honda Please enter the vehicle\'s make or a blank line to quit Chevy Please enter the vehicle\'s make or a blank line to quit Mercury Please enter the vehicle\'s make or a blank line to quit Kia Please enter the vehicle\'s make or a blank line to quit Mercedes-Benz Please enter the vehicle\'s make or a blank line to quit Audi Please enter the vehicle\'s make or a blank line to quit First Vehicle [make Audi Last Vehicle make Toyota

Solution

Vehicle.java

public class Vehicle implements Comparable<Vehicle>{
   private String make;
   public Vehicle(String make){
       this.make = make;
   }
   public String getMake() {
       return make;
   }
   public int compareTo(Vehicle v)
   {
   return getMake().compareTo(v.getMake());
     
   }
   public String toString(){
       return getClass().getName()+\"[make=\"+getMake()+\"]\";
   }
}

VehicleTest.java


import java.util.Arrays;
import java.util.Scanner;

public class VehicleTest {

  
   public static void main(String[] args) {
       Scanner scan = new Scanner(System.in);
       Vehicle v[] = new Vehicle[8];
       for(int i=0; i<v.length; i++){
           System.out.println(\"Please enter the vehicle\'s make or blank line to quit: \");
           String make = scan.nextLine();
           if(make == null || make.trim().equals(\"\")){
               break;
           }
           else{
               v[i]= new Vehicle(make);
           }
       }
       Arrays.sort(v);
       System.out.println(\"First: \"+v[0].toString());
       System.out.println(\"Last: \"+v[v.length-1].toString());
   }

}

Output:

Please enter the vehicle\'s make or blank line to quit:
Toyoto
Please enter the vehicle\'s make or blank line to quit:
Nissan
Please enter the vehicle\'s make or blank line to quit:
Honda
Please enter the vehicle\'s make or blank line to quit:
Chevy
Please enter the vehicle\'s make or blank line to quit:
Mercury
Please enter the vehicle\'s make or blank line to quit:
Kia
Please enter the vehicle\'s make or blank line to quit:
Mer
Please enter the vehicle\'s make or blank line to quit:
Audi
First: Vehicle[make=Audi]
Last: Vehicle[make=Toyoto]

Create a class called Vehicle that implements the Comparable interface. Vehicle will contain a member variable to hold the make of the vehicle, a constructor th
Create a class called Vehicle that implements the Comparable interface. Vehicle will contain a member variable to hold the make of the vehicle, a constructor th

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site