should consist of the following classes. Main Read input from the user to get si
ID: 3828138 • Letter: S
Question
should consist of the following classes. Main Read input from the user to get size of the array, input file name, and commands. Process commands as shown in the example. Car Database This should contain a private array of Car objects Write a constructor that accepts an integer to set the size of the array. Write the following public methods o isFull returns a boolean value indicating if array is full. o read File accepts a String parameter (the file name) and uses a Scanner to read in all the lines, create Car objects, and put them in the array o display Make accepts a string parameter and prints all cars with that make o mpgRange accepts two double parameters and prints all the cars whose mpg falls in that range. o weightRange ke mpg, but for weight o displayAll display all the cars in the database. Car Should have the following public variables o String model o String make o double mpg o int weight o int year A constructor method that accepts parameters to initialize all the variables A tostring method that returns all the variables with a label (as shown in the example)Explanation / Answer
PROGRAM
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class testclass {
testclass(){
}
int size;
Car a[];
Car B=new Car();
int count=0;
Scanner in =new Scanner(System.in);
testclass(int size)
{
a=new Car[size];
this.size=size;
}
public void isFull()
{
if(a.length==count)
{
System.out.println("array is full");
}
else
{
System.out.println("array is not full");
}
}
//method for reading the string from a file
public void readFile(String path) throws FileNotFoundException
{
FileInputStream fs=new FileInputStream(path);
Scanner sc=new Scanner(fs);
while(sc.hasNext())
{
Car c=new Car();
String s[]=sc.nextLine().split(",");
c.setMake(s[0]);
c.setModel(s[1]);
c.setMpg(Integer.parseInt(s[2]));
c.setWeight(Integer.parseInt(s[3]));
c.setYear(Integer.parseInt(s[4]));
a[count]=c;
count++;
}
}
//method for displaying the weight between a range
public boolean displayWeight(int from ,int to)
{
for(int i=0;i<=count;i++ )
{
//printing the weight between a range
if(a[i].getWeight()>from && a[i].getWeight()<to)
{
System.out.println(a[i]);
return true;
}
}
return false;
}
//method for searching a make
public boolean displayMake(String make)
{
for(int i = 0;i<size;i++)
{
if(a[i].getMake().equals(make))
{
System.out.println("equal");
return true;
}
}
return false;
}
//method for displaying the mpg between a range
public boolean rangeMPG(int from , int to)
{
for(int i=0;i<=count;i++ )
{
if(a[i].getMpg()>from && a[i].getMpg()<to)
{
System.out.println(a[i]);
return true;
}
}
return false;
}
//method for displaying all the contents
public void display()
{
for(int i=0;i<=count;i++ )
{
System.out.println(a[i]);
}
}
//main method
public static void main(String[] args) throws FileNotFoundException {
// TODO Auto-generated method stub
testclass t=new testclass();
System.out.println("enter the array size");
Scanner in =new Scanner(System.in);
int n=in.nextInt();
System.out.println("enter your choice");
int ch=in.nextInt();
do
{
System.out.println("1.isfull");
System.out.println("2.readfile");
System.out.println("3.displaymake");
System.out.println("4.mprange");
System.out.println("5.weightrange");
System.out.println("6.displayall");
System.out.println("4.exit");
switch(ch)
{
case 1:t.isFull();
// break;
case 2:System.out.println("enter the file path");
String path=in.nextLine();
t.readFile(path);
break;
case 3: System.out.println("enter the string to compare");
String make=in.nextLine();
t.displayMake(make);
break;
case 4:System.out.println("enter the range from");
int from=in.nextInt();
System.out.println("enter the range to");
int to=in.nextInt();
t.rangeMPG(from, to);
break;
case 5:System.out.println("enter weightrange from ");
int fromw=in.nextInt();
System.out.println("enter the range to");
int tow=in.nextInt();
t.displayWeight(fromw, tow);
break;
case 6:t.display();
break;
default:System.out.println("enter a valid input");
break;
}
}while(true);
}
}
OUTPUT
enter the array size
1
1.isfull
2.readfile
3.displaymake
4.exit
enter model
2
enter mpg
23
enter weight
34
enter year
22332
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.