Program 7 Fall 2016 All updated are in bold. Note: Remember to follow all progra
ID: 3573471 • Letter: P
Question
Program 7 Fall 2016 All updated are in bold. Note: Remember to follow all programming guidelines as given in coding standard handout. Note: Turn in the program with DEBUG = true. For this assignment, use the Car class from Program 7, with no changes. 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 a debug print switch to print the make, year, and price of each car immediately after each variable is read in, if DEBUG=true. See the sample output sections below. 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 with DEBUG=true: ------------------------------ DEBUG make = Chevrolet DEBUG year = 2003 DEBUG price = $12000.00 DEBUG make = Lincoln DEBUG year = 2007 DEBUG price = $32000.00 DEBUG make = Lexus DEBUG year = 2005 DEBUG price = $23678.00 DEBUG make = Hyundai DEBUG year = 2008 DEBUG price = $21000.00 DEBUG make = Honda DEBUG year = 2004 DEBUG price = $15500.00 DEBUG 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 DEBUG Search, make = Lexus DEBUG Search, year = 2005 DEBUG Search, price = $23678.00 Search key = Description of car: Make : Lexus Year : 2005 Price: $23678.00 This vehicle was found at index = 2 DEBUG Search, make = Ford DEBUG Search, year = 2001 DEBUG Search, price = $7595.00 Search key = Description of car: Make : Ford Year : 2001 Price: $7595.00 This vehicle was not found in the database. DEBUG Search, make = Honda DEBUG Search, year = 2004 DEBUG Search, price = $15500.00 Search key = Description of car: Make : Honda Year : 2004 Price: $15500.00 This vehicle was found at index = 4 DEBUG Search, make = EndSearchKeys Sample Output with DEBUG=false: ------------------------------ 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 key = Description of car: Make : Lexus Year : 2005 Price: $23678.00 This vehicle was found at index = 2 Search key = Description of car: Make : Ford Year : 2001 Price: $7595.00 This vehicle was not found in the database. Search key = Description of car: Make : Honda Year : 2004 Price: $15500.00 This vehicle was found at index = 4 NOTES: 1. Submit your Java source file, CarSearch.java and Car.java.
Explanation / Answer
// Car.java
public class Car {
private String make;
private int year;
private double price;
public Car(String make, int year, double price) {
this.make = make;
this.year = year;
this.price = price;
}
public String getMake() {
return make;
}
public void setMake(String make) {
this.make = make;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String toString(){
String desc = "Description of car:";
desc += " Make : " + getMake();
desc += " Year : " + getYear();
desc += " Price $: " + getPrice();
return desc;
}
public boolean equals(Car c){
if(this.make.equals(c.getMake()) && this.year == c.getYear() && this.price == c.getPrice()) return true;
return false;
}
}
// CarSearch.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class CarSearch
{
public static void main(String[] args) throws FileNotFoundException
{
final int SIZE_ARR = 30;
Car[] carArr = new Car[SIZE_ARR];
int pos;
final String endDb = "EndDatabase";
final String endSk = "EndSearchKeys";
String make;
int year;
double price;
Scanner in;
in = new Scanner(new File("input.txt"));
int numCars = 0;
while(in.hasNext()){
make = in.next();
if(make.equals(endDb)) break;
year = in.nextInt();
price = in.nextDouble();
Car car1 = new Car(make, year, price);
carArr[numCars++] = car1;
}
printData(carArr, numCars);
System.out.println();
while(in.hasNext()){
make = in.next();
if(make.equals(endSk)) break;
year = in.nextInt();
price = in.nextDouble();
Car key = new Car(make, year, price);
pos = seqSearch(carArr, numCars, key);
if(pos == -1){
System.out.println("This vehicle was not found in the database.");
}
else System.out.println("This vehicle was found at index = " + pos);
System.out.println();
}
}
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] + " ");
}
}
private static int seqSearch(Car[] carArr, int numCars, Car key)
{
System.out.println("Search key = ");
System.out.println(key);
for(int i = 0; i < carArr.length; i++){
if(key.equals(carArr[i])) return i;
}
return -1;
}
} // End public class CarSearch
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.