Problem Write a class Car that implements the Comparable int
Problem
Write a class Car that implements the Comparable interface. A car has a vin (vehicle identification number that contains digits and letters), a brand, a model, a year, and a mileage. Compare cars by their mileage. Ask the user to input ten cars and generate ten Car objects. Using the compareTo method, determine the first and last car among them and print them.
Problem ( Please provide comments and all the steps to solve this problem )
Solution
public class Car implements Comparable<Car> {
   //Attributes
    private String vin;
    private String brand;
    private String model;
    private int year;
    private double mileage;
   
    /**
    * Constructor
    * @param vin
    * @param brand
    * @param model
    * @param year
    * @param mileage
    */
    public Car(String vin, String brand, String model, int year, double mileage) {
        this.vin = vin;
        this.brand = brand;
        this.model = model;
        this.year = year;
        this.mileage = mileage;
    }
   /**
    * @return the vin
    */
    public String getVin() {
        return vin;
    }
   /**
    * @param vin the vin to set
    */
    public void setVin(String vin) {
        this.vin = vin;
    }
   /**
    * @return the brand
    */
    public String getBrand() {
        return brand;
    }
   /**
    * @param brand the brand to set
    */
    public void setBrand(String brand) {
        this.brand = brand;
    }
   /**
    * @return the model
    */
    public String getModel() {
        return model;
    }
   /**
    * @param model the model to set
    */
    public void setModel(String model) {
        this.model = model;
    }
   /**
    * @return the year
    */
    public int getYear() {
        return year;
    }
   /**
    * @param year the year to set
    */
    public void setYear(int year) {
        this.year = year;
    }
   /**
    * @return the mileage
    */
    public double getMileage() {
        return mileage;
    }
   /**
    * @param mileage the mileage to set
    */
    public void setMileage(double mileage) {
        this.mileage = mileage;
    }
   @Override
    public String toString() {
        return String.format(\"%-20s %-30s %-30s %-6d %-10.2f\", getVin(), getBrand(), getModel(), getYear(), getMileage());
    }
   @Override
    public int compareTo(Car car) {
        if((this.getMileage() - car.getMileage()) > 0)
            return 1;
        else if((this.getMileage() - car.getMileage()) < 0)
            return -1;
        else
            return 0;
    }
 }
import java.util.Arrays;
 import java.util.Scanner;
public class CarTest {
   public static void main(String[] args) {
        final int SIZE = 10;
       
        //Array to hold 10 car objects
        Car[] list = new Car[10];
       
        //Scanner to get user input
        Scanner sc = new Scanner(System.in);
       
        for(int i = 0 ; i < SIZE ; i++) {
            //Get vin
            System.out.print(\"Enter vehicle identification number: \");
            String vin = sc.nextLine();
           
            //Get brand
            System.out.print(\"Enter vehicle brand: \");
            String brand = sc.nextLine();
           
            //Get model
            System.out.print(\"Enter vehicle model: \");
            String model = sc.nextLine();
           
            //Get year
            System.out.print(\"Enter vehicle make year: \");
            int year = sc.nextInt();
           
            //Get mileage
            System.out.print(\"Enter vehicle mileage: \");
            double mileage = sc.nextDouble();
           
            //clear keyboard buffer
            sc.nextLine();
           
            //Create and add car object to the array
            list[i] = new Car(vin, brand, model, year, mileage);
           
            System.out.println();
        }
       
        System.out.println(\"List of Cars\");
        System.out.printf(\"\ %-20s %-30s %-30s %-6s %-10s\ \", \"Vin\", \"Brand\", \"Model\", \"Year\", \"Mileage(km/litre)\");
        for (Car car : list) {
            System.out.println(car);
        }
       
        //Close scanner
        sc.close();
       
        //Sort array
        Arrays.sort(list);
        //Display result
        System.out.println(\"\ Car with the highest mileage: \ \" + list[9]);
        System.out.println(\"Car with the lowest mileage: \ \" + list[0]);
    }
 }
SAMPLE OUTPUT:
Enter vehicle identification number: 123A
 Enter vehicle brand: Ford Mustang
 Enter vehicle model: Fastback V8.
 Enter vehicle make year: 2016
 Enter vehicle mileage: 13
Enter vehicle identification number: 123B
 Enter vehicle brand: BMW
 Enter vehicle model: 5-Series 520d M Sport
 Enter vehicle make year: 2016
 Enter vehicle mileage: 18.12
Enter vehicle identification number: 123C
 Enter vehicle brand: AUDI
 Enter vehicle model: TT 45 TFSI
 Enter vehicle make year: 2015
 Enter vehicle mileage: 14.33
Enter vehicle identification number: 123D
 Enter vehicle brand: Mercedes-Benz
 Enter vehicle model: SLC AMG 43
 Enter vehicle make year: 2016
 Enter vehicle mileage: 10.7
Enter vehicle identification number: 123E
 Enter vehicle brand: Ferrari
 Enter vehicle model: California T Convertible
 Enter vehicle make year: 2013
 Enter vehicle mileage: 9
Enter vehicle identification number: 123F
 Enter vehicle brand: Toyota
 Enter vehicle model: Land Cruiser Prado VX L
 Enter vehicle make year: 2000
 Enter vehicle mileage: 11.13
Enter vehicle identification number: 123G
 Enter vehicle brand: Lamborghini
 Enter vehicle model: Huracan LP 580-2
 Enter vehicle make year: 2016
 Enter vehicle mileage: 11.24
Enter vehicle identification number: 123H
 Enter vehicle brand: BMW
 Enter vehicle model: M4 Coupe
 Enter vehicle make year: 2015
 Enter vehicle mileage: 10.75
Enter vehicle identification number: 123I
 Enter vehicle brand: BMW
 Enter vehicle model: i8 Hybrid
 Enter vehicle make year: 2016
 Enter vehicle mileage: 47.45
Enter vehicle identification number: 123J
 Enter vehicle brand: AudI
 Enter vehicle model: R8 V10 Plus
 Enter vehicle make year: 2016
 Enter vehicle mileage: 6.71
List of Cars
Vin Brand Model Year Mileage(km/litre)
 123A Ford Mustang Fastback V8 2016 13.00   
 123B BMW 5-Series 520d M Sport 2016 18.12   
 123C AUDI TT 45 TFSI 2015 14.33   
 123D Mercedes-Benz SLC AMG 43 2016 10.70   
 123E Ferrari California T Convertible 2013 9.00
 123F Toyota Land Cruiser Prado VX L 2000 11.13   
 123G Lamborghini Huracan LP 580-2 2016 11.24   
 123H BMW M4 Coupe 2015 10.75   
 123I BMW i8 Hybrid 2016 47.45   
 123J AudI R8 V10 Plus 2016 6.71
Car with the highest mileage:
 123I BMW i8 Hybrid 2016 47.45   
 Car with the lowest mileage:
 123J AudI R8 V10 Plus 2016 6.71





