For this assignment, use the Car class from Program 7, with no changes. import j
ID: 3576184 • Letter: F
Question
For this assignment, use the Car class from Program 7, with no changes.
import java.text.DecimalFormat; /* Programming Assignment 7 * Fall 2016 * CS1113 * The purpose of this class is to represent a Car in Java. */ public class Car { private String make; private int year; private double price; public static DecimalFormat twoDecPl = new DecimalFormat("$0.00"); // The constructor for the car. public Car(String make, int year, double price) { this.make = make; this.year = year; this.price = price; } // The accessor methods public String getMake() { return make; } public int getYear() { return year; } public double getPrice() { return price; } // The mutator method public void setPrice(double price) { this.price = price; } // The equals method public boolean equals(Car car) { return this.make.equals(car.make) && this.year == car.year && this.price == car.price; } // The toString method public String toString() { return " Description of car: Make : " + this.make + " Year : " + this.year + " Price: " + twoDecPl.format(this.price); } } Write a main class CarSearch, stored in a file named CarSearch.java.
Until now, each main class has contained only one method, public static void main(String[] args). This time the main class will contain three methods: public class CarSearch { public static void main(String[] args) { ... } private static void printData(Car[] carArr, int numCars) { ... } private static int seqSearch(Car[] carArr, int numCars, Car key) { ... } } // End public class CarSearch The execution of the program will start at the top of the main program. When main needs to have printData do something, main will "invoke" or "call" printData, as shown below. printData will do its job and then return to main. seqSearch will be invoked similarly. printData will consist of a statement to print a title line, "Print out the database.", a "for" loop over "i", say, and inside the for loop, one statement to print carArr[i]. seqSearch will be like the seqSearch method given in lecture on Tuesday, 10 November, except that it will use an array of Car objects instead of an array of "double" quantities. Therefore seqSearch will have to use .equals(...) to compare an array element to the search key, instead of using == . Main program ------------ Declare SIZE_ARR, a final int equal to 30. Declare an array of Car objects with size equal to SIZE_ARR: Car[] carArr = new Car[SIZE_ARR]; Declare all necessary variables. Make a sentinel loop to read in the make, year, and price of a Car object, instantiate the object, and store it into the array carArr[]. If the Car object is car1, you can just do carArr[i] = car1; where "i" is an int counter. Count the elements in this array as they are stored. Keep the count in an int variable, "i" or numCars. The sentinel String for this loop is "EndDatabase", which must of course be a "final String" named constant. After the sentinel has been reached, invoke the method printData to print the database. To invoke this method, just give its name: printData(carArr, numCars); Next, make a second sentinel loop to read in the make, year, and price of a Car object and instantiate the object. Call this object "key". Inside this loop, invoke the method seqSearch to search in carArr[] for a copy of the search key object, "key". You can do this using a statement such as pos = seqSearch(carArr, numCars, key); where pos is an int variable that has been declared previously. Print the result of each search as shown in the sample output below. The sentinel String for this loop is "EndSearchKeys", which must of course be a "final String" named constant. Use defensive programming with .hasNext(), .hasNextInt(), and .hasNextDouble() to check that valid input is available at each stage. You do not have to check that the data values are reasonable, such as that year>0 or year>1800, price>0.0, etc. Sample Input: ------------- Chevrolet 2003 12000.0 Lincoln 2007 32000.0 Lexus 2005 23678.0 Hyundai 2008 21000.0 Honda 2004 15500.0 EndDatabase Lexus 2005 23678.0 Ford 2001 7595.0 Honda 2004 15500.0 EndSearchKeys Sample Output: make = Chevrolet year = 2003 price = $12000.00 make = Lincoln year = 2007 price = $32000.00 make = Lexus year = 2005 price = $23678.00 make = Hyundai year = 2008 price = $21000.00 make = Honda year = 2004 price = $15500.00 make = EndDatabase Print out the database. Description of car: Make : Chevrolet Year : 2003 Price: $12000.00 Description of car: Make : Lincoln Year : 2007 Price: $32000.00 Description of car: Make : Lexus Year : 2005 Price: $23678.00 Description of car: Make : Hyundai Year : 2008 Price: $21000.00 Description of car: Make : Honda Year : 2004 Price: $15500.00 Search, make = Lexus Search, year = 2005 Search, price = $23678.00 Search key = Description of car: Make : Lexus Year : 2005 Price: $23678.00 This vehicle was found at index = 2 Search, make = Ford Search, year = 2001 Search, price = $7595.00 Search key = Description of car: Make : Ford Year : 2001 Price: $7595.00 This vehicle was not found in the database. Search, make = Honda Search, year = 2004 Search, price = $15500.00 Search key = Description of car: Make : Honda Year : 2004 Price: $15500.00 This vehicle was found at index = 4 Search, make = EndSearchKeys NOTES: 1. Submit your Java source file, CarSearch.java and Car.java.
Explanation / Answer
CarSearch.java
import java.util.Scanner;
public class CarSearch
{
public static void main(String[] args)
{
final int SIZE_ARR = 30;
Car[] carArr = new Car[SIZE_ARR];
int numCars = 0;
Scanner scan = new Scanner(System.in);
for(int i=0; i<carArr.length; i++){
System.out.println("Enter the make (EndDatabase to quit): ");
String make = scan.next();
if(make.equalsIgnoreCase("EndDatabase")){
break;
}
System.out.println("Enter the year: ");
int year = scan.nextInt();
System.out.println("Enter the price: ");
double price = scan.nextDouble();
numCars++;
carArr[i] = new Car(make, year, price);
}
printData(carArr, numCars);
System.out.println("Enter the Search car: ");
for(;;){
System.out.println("Enter the make (EndSearchKeys to quit): ");
String make = scan.next();
if(make.equalsIgnoreCase("EndSearchKeys")){
break;
}
System.out.println("Enter the year: ");
int year = scan.nextInt();
System.out.println("Enter the price: ");
double price = scan.nextDouble();
Car key = new Car(make, year, price);
int n = seqSearch(carArr, numCars, key);
if(n!=-1)
System.out.println("This vehicle was found at index: "+n);
else
System.out.println("This vehicle was not found in the database.");
}
}
private static void printData(Car[] carArr, int numCars)
{
for(int i=0; i<numCars; i++){
System.out.println(carArr[i]);
}
}
private static int seqSearch(Car[] carArr, int numCars, Car key)
{
for(int i=0; i<carArr.length; i++){
if(carArr[i].equals(key)){
return i;
}
}
return -1;
}
} //
Output:
Enter the make (EndDatabase to quit):
Chevrolet
Enter the year:
200
Enter the price:
12000
Enter the make (EndDatabase to quit):
Lincoln
Enter the year:
2007
Enter the price:
32000
Enter the make (EndDatabase to quit):
Lexus
Enter the year:
2005
Enter the price:
23678
Enter the make (EndDatabase to quit):
EndDatabase
Description of car:
Make : Chevrolet
Year : 200
Price: $12000.00
Description of car:
Make : Lincoln
Year : 2007
Price: $32000.00
Description of car:
Make : Lexus
Year : 2005
Price: $23678.00
Enter the Search car:
Enter the make (EndSearchKeys to quit):
Lexus
Enter the year:
2005
Enter the price:
23678
This vehicle was found at index: 2
Enter the make (EndSearchKeys to quit):
EndSearchKeys
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.