import javatextDecimalFormat import javautil public class Ca

import java.text.DecimalFormat;
import java.util.*;


public class Car {

private int year;
   private String make;
private double price;

public static DecimalFormat twoDecPl = new DecimalFormat(\"$0.00\");

  
  
public Car(String make, int year, double price) {

this.make = make;
this.year = year;
this.price = price;
}
public String getMake() {
return make;
}
public int getYear() {
return year;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int hashCode() {
return Objects.hash(make, year, price);
}
  
public String toString(){
return \"\ Description Of Vehical : \"+\"\ Make : \"+this.make+\"\ Year : \"+this.year+\"\ Price : \"+twoDecPl.format(this.price);
}
public boolean equals(Object object) {

if (object == this){
return true;
}
if (!(object instanceof Car)) {
return false;
}
Car car = (Car) object;
return price == car.price && Objects.equals(make, car.make) && year==car.year;
}

}

Solution

import java.text.DecimalFormat;

public class Car {
   private int year;
   private String make;
   private double price;

   public static DecimalFormat twoDecPl = new DecimalFormat(\"$0.00\");

   public Car(String make, int year, double price) {
       this.make = make;
       this.year = year;
       this.price = price;
   }

   public String getMake() {
       return make;
   }

   public int getYear() {
       return year;
   }

   public double getPrice() {
       return price;
   }

   public void setPrice(double price) {
       this.price = price;
   }

   public String toString() {
       return \"\ Description Of Vehical : \" + \"\ Make : \" + this.make
               + \"\ Year : \" + this.year + \"\ Price : \"
               + twoDecPl.format(this.price);
   }

   /*
   * (non-Javadoc)
   *
   * @see java.lang.Object#hashCode()
   */
   @Override
   public int hashCode() {
       final int prime = 31;
       int result = 1;
       result = prime * result + ((make == null) ? 0 : make.hashCode());
       long temp;
       temp = Double.doubleToLongBits(price);
       result = prime * result + (int) (temp ^ (temp >>> 32));
       result = prime * result + year;
       return result;
   }

   /*
   * (non-Javadoc)
   *
   * @see java.lang.Object#equals(java.lang.Object)
   */
   @Override
   public boolean equals(Object obj) {
       if (this == obj)
           return true;
       if (obj == null)
           return false;
       if (!(obj instanceof Car))
           return false;
       Car other = (Car) obj;
       if (make == null) {
           if (other.make != null)
               return false;
       } else if (!make.equals(other.make))
           return false;
       if (Double.doubleToLongBits(price) != Double
               .doubleToLongBits(other.price))
           return false;
       if (year != other.year)
           return false;
       return true;
   }

}

import java.util.Scanner;

public class CarSearch {
   /**
   * @param args
   */
   public static void main(String[] args) {

       final int SIZE_ARR = 30;
       Car[] carArr = new Car[SIZE_ARR];
       int numCars = 0, i = 0;
       final String sentinel = \"EndDatabase\";
       final String sentinelKey = \"EndSearchKeys\";
       Scanner scanner = new Scanner(System.in);
       int year;
       String make;
       double price;
       while (true) {

           System.out.print(\"Enter the car make(or EndDatabase to exit):\");
           make = scanner.next();
           if (sentinel.equalsIgnoreCase(make))
               break;

           System.out.print(\"Enter the Year:\");
           year = scanner.nextInt();
           System.out.print(\"Enter the Price:\");
           price = scanner.nextDouble();
           carArr[numCars++] = new Car(make, year, price);
       }

       printData(carArr, numCars);

       while (true) {
           System.out
                   .print(\"Enter the car make to search as key(or EndDatabase to exit):\");
           make = scanner.next();
           if (sentinelKey.equalsIgnoreCase(make))
               break;

           System.out.print(\"Enter the Year:\");
           year = scanner.nextInt();
           System.out.print(\"Enter the Price:\");
           price = scanner.nextDouble();
           Car key = new Car(make, year, price);
           int pos = seqSearch(carArr, numCars, key);
           if (pos != -1)
               System.out.println(\"Car found at \" + pos);
           else
               System.out.println(\"Car Not found\");

       }

   }

   /**
   * @param carArr
   * @param numCars
   */
   private static void printData(Car[] carArr, int numCars) {

       System.out.println(\"Print out the database.\");
       for (int i = 0; i < numCars; i++) {

           System.out.println(carArr[i]);
       }
   }

   /**
   * @param carArr
   * @param numCars
   * @param key
   * @return
   */
   private static int seqSearch(Car[] carArr, int numCars, Car key) {

       for (int i = 0; i < numCars; i++) {

           if (carArr[i].equals(key))

               return i;
       }
       return -1;
   }
} // End public class CarSearch

OUTPUT:

Enter the car make(or EndDatabase to exit):Chevrolet
Enter the Year:2003
Enter the Price:12000
Enter the car make(or EndDatabase to exit):Lincoln
Enter the Year:2007
Enter the Price:32000
Enter the car make(or EndDatabase to exit):Lexus
Enter the Year:2005
Enter the Price:23678
Enter the car make(or EndDatabase to exit):EndDatabase
Print out the database.

Description Of Vehical :
Make : Chevrolet
Year : 2003
Price : $12000.00

Description Of Vehical :
Make : Lincoln
Year : 2007
Price : $32000.00

Description Of Vehical :
Make : Lexus
Year : 2005
Price : $23678.00
Enter the car make to search as key(or EndDatabase to exit):Lexus
Enter the Year:2005
Enter the Price:23678
Car found at 2
Enter the car make to search as key(or EndDatabase to exit):Alto
Enter the Year:2002
Enter the Price:21000
Car Not found
Enter the car make to search as key(or EndDatabase to exit):EndSearchKeys


Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site