This is what the program asks us to do. Here is an example of the output: Here i
ID: 3726753 • Letter: T
Question
This is what the program asks us to do.
Here is an example of the output:
Here is my code for the main class:
Here is my code for the CarDatabase class:
Here is my code for the Car class:
And here is my output:
I understand what it is doing wrong but I can't for the life of me figure out how to fix it, if someone could please give me a helping hand it would be much appreciated. Thank you.
CSC 212 Principles of Programming Homework Assignment 4 Write a Java program that reads in a file containing data about cars and builds a car database The input file can be found here (be sure to access this file using your Oswego-Google account, not your private one). Each line of file input should be used to create a Car object. Use an array of Car objects in a carDatabase class to access and print various information about the cars. Finally, your program should also use a command interpreter in the Main class that allows the user to issue commands and retrieve the data as shown in the example (see below) Design Make sure that your program uses a Java package called csc212hw04'. Your program 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 CarDatabase 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 isFul1 returns a boolean value indicating if array is full o readFile 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 displayMake 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 like mpg, but for weight o displayAL1 ) -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
// Car class
package csc212hw04;
public class Car {
public String model;
public String make;
public double mpg;
public int weight;
public int year;
public Car(String model,String make,double mpg, int weight,int year)
{
this.model=model;
this.make=make;
this.mpg = mpg;
this.weight = weight;
this.year = year;
}
public String toString()
{
return("Model: "+model+" Make: "+make+" Mpg: "+mpg+" Weight: "+weight+" year: "+year);
}
public String getMake()
{
return make;
}
public double getMpg()
{
return mpg;
}
public int getWeight()
{
return weight;
}
}
// end of Car
// CarDatabase class
package csc212hw04;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class CarDatabase {
private Car[] car;
private int filled;
public CarDatabase(int data)
{
car = new Car[data];
filled=0;
}
public boolean isFull()
{
return(filled>=car.length);
}
public void readFile(String file) throws FileNotFoundException
{
// update the filled parameter in this function to reflect the vacant positions in array
File input = new File(file);
Scanner kb = new Scanner(input);
while(kb.hasNextLine())
{
String line = kb.nextLine();
String[] cars = line.trim().split(",");
String model = cars[0].trim();
String make = cars[1].trim();
double mpg = Double.parseDouble(cars[2].trim());
int weight = Integer.parseInt(cars[3].trim());
int year = Integer.parseInt(cars[4].trim());
// if array is full then break from loop
if(!isFull())
{
car[filled]= new Car(model,make,mpg,weight,year);
filled++;
}else
break;
}
kb.close();
}
public void displayMake(String make)
{
for(int i=0;i<filled;i++)
{
if(car[i].getMake().equalsIgnoreCase(make))
System.out.println(car[i]);
}
}
public void mpgRange(double a, double b)
{
for(int i=0;i<filled;i++)
{
if(car[i].getMpg()>=a && car[i].getMpg()<=b)
System.out.println(car[i]);
}
}
// input arguments must be integers
public void weightRange(int a, int b)
{
for(int i=0;i<filled;i++)
{
if(car[i].getWeight()>=a && car[i].getWeight()<=b)
System.out.println(car[i]);
}
}
public void displayAll()
{
for(int i=0;i<filled;i++)
System.out.println(car[i]);
}
}
// end of CarDatabase
//Main class
package csc212hw04;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws FileNotFoundException {
String line,userString,make,command;
int weight,arraySize;
double mpg;
Scanner sc = new Scanner(System.in);
System.out.println("Welcome to the car Database");
System.out.println("Enter the size of the array :");
line =sc.nextLine();
arraySize = Integer.parseInt(line);
CarDatabase car = new CarDatabase(arraySize);
System.out.println("Enter the name of the input file :");
userString = sc.nextLine();
car.readFile(userString);
System.out.println("Enter make, mpg, weight, all or quit: ");
command = sc.nextLine();
while(!command.equalsIgnoreCase("quit"))
{
if(command.equalsIgnoreCase("make"))
{
System.out.println("Enter make: ");
make = sc.nextLine();
car.displayMake(make);
}
else if(command.equalsIgnoreCase("mpg"))
{
System.out.println("Enter the mpg range :");
line = sc.nextLine();
String[] doubles = line.trim().split(" ");
double mpg2 = Double.parseDouble(doubles[0]);
mpg = Double.parseDouble(doubles[1]);
// check the smaller and larger value so that the first argument is lesser than the second argument
if(mpg2 < mpg)
car.mpgRange(mpg2,mpg);
else
car.mpgRange(mpg,mpg2);
}
else if(command.equalsIgnoreCase("weight"))
{
System.out.println("Enter the weight range :");
line = sc.nextLine();
String[] integers = line.trim().split(" ");
// check the smaller and larger value so that the first argument is lesser than the second argument
int weight2 = Integer.parseInt(integers[0]);
weight = Integer.parseInt(integers[1]);
if(weight2 < weight)
car.weightRange(weight2,weight);
else
car.mpgRange(weight,weight2);
}else if(command.equalsIgnoreCase("all"))
car.displayAll();
else
System.out.println("Unknown command -- please try again");
System.out.println("Enter make, mpg, weight, all or quit: ");
command = sc.nextLine();
}
System.out.println("Closing the Database");
sc.close();
}
}
// end of Main
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.