All updated are in red. Note: Remember to follow all programming guidelines as g
ID: 3575043 • Letter: A
Question
All updated are in red. 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
import java.io.*;
class Car //declaration of Car Object
{
String make;
int year;
float price;
}
public class CarSearch
{
int SIZE_ARR =30;
Car [] carArr;
int count=-1;
public static void main(String[] args) throws Exception
{
Car [] carArr =new Car[SIZE_ARR];
char ch;
BufferedReader reader;//create BufferedReader object
reader=new BufferedReader(new InputStreamReader(System.in));
carname=new BufferedReader(new InputStreamReader(System.in));
Car key=new Car;
while(1)
{
System.out.println("1 Add Car Detail ");
System.out.println("2 Print Car Detal ");
System.out.println("3 Seach Car ")'
System.out.println("4 Exit");
System.out.println(" Enter your choice ");
ch=(char)System.in.read();
switch(ch)
{
case '1':
count=count+1;
if(count>=SIZE_ARR)
{
System.out.println("Car Limit Exceeded you can not add more car details");
}
else
{
System.out.println("Enter Car Make");
carArr[count].make= System.console().readLine();
System.out.println("Enter Car Year");
carArr[count].year= Integer.parseInt(reader.readLine());
System.out.println("Enter Car Price");
carArr[count].price= Floar.parseFloat(reader.readLine());
}
break;
case '2':
printData(carArr, count);
break;
case '3':
System.out.printtln("Enter Following Detail to search");
System.out.println("Enter Car Make");
key.make=System.console().readLine();
System.out.println( "Enter Car Year");
key.year= Integer.parseInt(reader.readLine());
System.out.println( "Enter Car Price ");
key.price= Floar.parseFloat(reader.readLine());
loc=seqSearch(carArr,count, key);
if(loc==-1)
{System.out.println("Car Not Found "); }
else
{
loc=loc+1;
System.out.println("Car found at position "+String.valueOf(loc));
System.out.println("Description of Car:");
System.out.println("Make:" +carArr[loc].make);
System.out.println("Year:+ String.valuOf(carArr[loc]".year));
System.out.println("Price: $"+ Float.toString(carArr[loc]".price)
}
break;
case '4':
System.exit(1);
}
}
private static void printData(Car[] carArr, int numCars)
{
int i;
for(i=0;i<=numCars;i++)
{
System.out.println("Description of Car:");
System.out.println("Make:" +carArr[i].make);
System.out.println("Year:+ String.valuOf(carArr[i]".year));
System.out.println("Price: $"+ Float.toString(carArr[i]".price)
}
}
private static int seqSearch(Car[] carArr, int numCars, Car key)
{
int i;
int pos=-1;
for(i=0;i<numCarsl;i++)
{
if(key.equals(carArr[i])
pos=i;
}
if(pos==-1)
return(0);
else
return(pos+1);
}
} // End public class CarSearch