Question

Use BlueJ to write a program that reads a sequence of data for several car objects...

Use BlueJ to write a program that reads a sequence of data for several car objects from an input file. It stores the data in an ArrayList<Car> list . Program should work for input file containing info for any number of cars. (You should not assume that it will always be seven lines in the input file). Use notepad to create input file "inData.txt". File should be stored in the same folder where all files from BlueJ for this program are located.

Class Car describes one car object and has variables vin, make , model of String type, cost of type double, and year of int type. Variable vin is vehicle identification number and consist of digits and letters . In addition, class Car has methods:

public String getModel()            // returns car’s model

public int getYear()                       // returns car’s year

public boolean isExpensive()   // returns true if car has cost above 30000 and false

                                                                             //otherwise

public boolean isAntique()        // returns true if car’s year is before 1968, and false

                                                                             // otherwise

public String toString()         // returns string with all car’s data in one line

                                                                             // separated by tabs.

   

Design class CarList that has instance variable list which is of ArrayList<Car> type. Variable list is initialized in the constructor by reading data for each car from an input file. Each line of input file "inData.txt" has vin, make, model, cost, and year data in this order, and separated by a space. Input file should contain 7 cars. The data for the first five cars in the input file should be as follows:

1234567CS2 Subaru Impreza 27000 2018

1233219CS2 Toyota Camry   31000 2004

9876543CS2 Ford Mustang 45000 1960

3456789CS2 Toyota Tercel 20000 2004

4567890CS2 Crysler Royal 21000 1960

Add remaining two non-antique cars of your choice.

Class CarList also has the following methods:

  • public void printList() // Prints title followed by list of all cars (each row has data

                                                              //for one car)   

  • public void printExpensiveCars() //Prints "List of expensive cars:" followed by

                                                                              // complete data for each expensive car

  • public Car oldestCar()      // Method returns oldest Car (which has smallest year.)

                                                                 // In case of multiple cars with the same smallest year

                                                                 // return first such car in the list.

  • public int countCarsWithMake(String make) // Method accepts a makel

                                                                               //and returns count of cars with given make.   

  • public ArrayList<Car> antiqueExpensiveCarList()    // Returns ArrayList

                                               // of all cars from the list that are both antique and expensive.

                                               // The method antiqueExpensiveCarList is extra credit .

The last three methods just return the specified data type. Do not print anything within those methods. Just return the requred result, and have explanation printed at the place where those methods are invoked.

Class TestCarList will have main method. In it, instantiate an object from CarList class and use it to invoke each of the five methods from CarList class. If method countCarsWithMake returns zero, report that there are no cars with specified make, otherwise provide count with full sentence.

NOTE: Do not forget to append throws IOException to the constructor of CarList class and main method header in class TestCarList. In addition, you have to provide

import java.io.*;

import java.util.*;

in order to use Scanner and ArrayList classes from Java.

SUBMIT a single word or PDF document named p1_yourLastName_CS152 with the following:

  • Your name, class section and project number and date of submission in the upper left corner
  • Copy of the code for each class in separate rectangle
  • Copy of your input file
  • Picture of program run from BlueJ.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Note: Could you plz go through this code and let me know if u need any changes in this.Thank You
=================================


We have paste the input file in the project folder so that our program can able to detect.

// inData.txt

1234567CS2 Subaru Impreza 27000 2018
1233219CS2 Toyota Camry 31000 2004
9876543CS2 Ford Mustang 45000 1960
3456789CS2 Toyota Tercel 20000 2004
4567890CS2 Crysler Royal 21000 1960

==============================

// Car.java

public class Car {
private String vin;
private String make;
private String model;
private int price;
private int year;
/**
* @param vin
* @param make
* @param model
* @param price
* @param year
*/
public Car(String vin, String make, String model, int price, int year) {

   this.vin = vin;
   this.make = make;
   this.model = model;
   this.price = price;
   this.year = year;
}
/**
* @return the vin
*/
public String getVin() {
   return vin;
}
/**
* @param vin the vin to set
*/
public void setVin(String vin) {
   this.vin = vin;
}
/**
* @return the make
*/
public String getMake() {
   return make;
}
/**
* @param make the make to set
*/
public void setMake(String make) {
   this.make = make;
}
/**
* @return the model
*/
public String getModel() {
   return model;
}
/**
* @param model the model to set
*/
public void setModel(String model) {
   this.model = model;
}
/**
* @return the price
*/
public int getPrice() {
   return price;
}
/**
* @param price the price to set
*/
public void setPrice(int price) {
   this.price = price;
}
/**
* @return the year
*/
public int getYear() {
   return year;
}
/**
* @param year the year to set
*/
public void setYear(int year) {
   this.year = year;
}

public boolean isExpensive() // returns true if car has cost above 30000 and false otherwise
{
   if(price>30000)
       return true;
   else
       return false;
}


public boolean isAntique() // returns true if car’s year is before 1968, and false otherwise
{
   if(year<1968)
       return true;
   else
       return false;
}

public String toString() // returns string with all car’s data in one line separated by tabs.
{
   return vin+"\t"+make+"\t"+model+"\t"+price+"\t"+year;
}
}

============================

// CarList.java

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;

public class CarList {
   private ArrayList<Car> arl = null;

   public CarList() throws FileNotFoundException {

       arl = new ArrayList<Car>();

       String line;
       String vin, make, model;
       int price;
       int year;

       Scanner sc = new Scanner(new File("inData.txt"));

       while (sc.hasNext()) {
           line = sc.nextLine();
      
           String arr[] = line.split(" ");

           vin = arr[0];
           make = arr[1];
           model = arr[2];
           price = Integer.parseInt(arr[3]);
           year = Integer.parseInt(arr[4]);
           Car c = new Car(vin, make, model, price, year);
           arl.add(c);
       }

       sc.close();

   }

   /*
   * Prints title followed by list of all cars (each row has data for one car)
   */
   public void printList() {
       System.out.println("Displaying Cars :");
       for (int i = 0; i < arl.size(); i++) {
           System.out.println(arl.get(i));
       }
   }

   /*
   * Prints "List of expensive cars:" followed by complete data for each
   * expensive car
   */
   public void printExpensiveCars() {
       System.out.println("Displaying Expensiove cars :");
       for (int i = 0; i < arl.size(); i++) {
           if (arl.get(i).isExpensive()) {
               System.out.println(arl.get(i));
           }
       }
   }

   /*
   * Method returns oldest Car (which has smallest year.) In case of multiple
   * cars with the same smallest year return first such car in the list.
   */
   public Car oldestCar() {

       int indx = 0;
       int min = arl.get(0).getYear();

       for (int i = 0; i < arl.size(); i++) {
           if (min > arl.get(i).getYear()) {
               min = arl.get(i).getYear();
               indx = i;
           }
       }
       return arl.get(indx);
   }

   /*
   * Method accepts a make and returns count of cars with given make.
   */
   public int countCarsWithMake(String make) {
       int cnt = 0;
       for (int i = 0; i < arl.size(); i++) {
           if (arl.get(i).getMake().equalsIgnoreCase(make)) {
               cnt++;
           }
       }
       return cnt;
   }

   /*
   * // Returns ArrayList
   */
   public ArrayList<Car> antiqueExpensiveCarList() {
       ArrayList<Car> list = new ArrayList<Car>();
       for (int i = 0; i < arl.size(); i++) {
           if (arl.get(i).isAntique()) {
               list.add(arl.get(i));
           }
           if (arl.get(i).isExpensive()) {
               list.add(arl.get(i));
           }
       }
       return list;
   }
}

================================

// Test.java

import java.io.FileNotFoundException;
import java.util.ArrayList;

public class Test {

   public static void main(String[] args) throws FileNotFoundException {
CarList cl=new CarList();
cl.printList();
cl.printExpensiveCars();
Car oldC=cl.oldestCar();
System.out.println("No of Cars of make 'Subabu' cars :"+cl.countCarsWithMake("Subaru"));
ArrayList<Car> arl=cl.antiqueExpensiveCarList();
  
System.out.println("\nDisplaying the expensive and Antique Cars:");
for(int i=0;i<arl.size();i++)
{
   System.out.println(arl.get(i));
}
  
  

   }

}

=============================

Output:

Displaying Cars :
1234567CS2   Subaru   Impreza   27000   2018
1233219CS2   Toyota   Camry   31000   2004
9876543CS2   Ford   Mustang   45000   1960
3456789CS2   Toyota   Tercel   20000   2004
4567890CS2   Crysler   Royal   21000   1960
Displaying Expensiove cars :
1233219CS2   Toyota   Camry   31000   2004
9876543CS2   Ford   Mustang   45000   1960
No of Cars of make 'Subabu' cars :1

Displaying the expensive and Antique Cars:
1233219CS2   Toyota   Camry   31000   2004
9876543CS2   Ford   Mustang   45000   1960
9876543CS2   Ford   Mustang   45000   1960
4567890CS2   Crysler   Royal   21000   1960

=====================Could you plz rate me well.Thank You

Add a comment
Know the answer?
Add Answer to:
Use BlueJ to write a program that reads a sequence of data for several car objects...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • *** FOR A BEGINNER LEVEL JAVA CLASS, PLEASE KEEP CODE SIMPLE, AND WE USE BLUE J...

    *** FOR A BEGINNER LEVEL JAVA CLASS, PLEASE KEEP CODE SIMPLE, AND WE USE BLUE J IN CLASS (IF IT DOESNT MATTER PLEASE COMMENT TELLING WHICH PROGRAM YOU USED TO WRITE THE CODE, PREFERRED IS BLUE J!!)*** ArrayList of Objects and Input File Use BlueJ to write a program that reads a sequence of data for several car objects from an input file. It stores the data in an ArrayList<Car> list . Program should work for input file containing info...

  • Wolfie's Car Repair Shop For this assignment you will be completing a program that manages cars...

    Wolfie's Car Repair Shop For this assignment you will be completing a program that manages cars and car repairs at Wolfie's Car Repair Shop. The program is menu-driven. The menu can be found in Homework.Driver.java. Provided in that file is a series of methods that begin with the prefix main_. Those methods request input from the user and then call methods from the class CarRepairShop. It is these methods in the CarRepairShop that you will be implementing for homework. In...

  • Create a class called CarNode which has fields for the data (a Car) and next (CarNode)...

    Create a class called CarNode which has fields for the data (a Car) and next (CarNode) instance variables. Include a one-argument constructor which takes a Car as a parameter. (For hints, see the PowerPoint on "Static vs. Dynamic Structures”.) public CarNode (Car c) { . . } The instance variables should have protected access. There will not be any get and set methods for the two instance variables. Create an abstract linked list class called CarList. This should be a...

  • Please try to write the code with Project 1,2 and 3 in mind. And use java language, thank you very much. Create an Edit Menu in your GUI Add a second menu to the GUI called Edit which will have one me...

    Please try to write the code with Project 1,2 and 3 in mind. And use java language, thank you very much. Create an Edit Menu in your GUI Add a second menu to the GUI called Edit which will have one menu item called Search. Clicking on search should prompt the user using a JOptionPane input dialog to enter a car make. The GUI should then display only cars of that make. You will need to write a second menu...

  • Design class House to describe a house object by using houseID, town, and zip code of...

    Design class House to describe a house object by using houseID, town, and zip code of String type   and price as integer. Write a program that reads a sequence of houseIDs, towns, zip codes and prices for up to 100 houses from an input file. Store the data in an array that has capacity to hold 100 house objects. Keep track of number of houses in variable count.   Assume that each line of input has houseID, town, zip,and price data...

  • Using Python. You will create two classes and then use the provided test program to make sure your program works This m...

    Using Python. You will create two classes and then use the provided test program to make sure your program works This means that your class and methods must match the names used in the test program You should break your class implementation into multiple files. You should have a car.py that defines the car class and a list.py that defines a link class and the linked list class. When all is working, you should zip up your complete project and...

  • Below are the Car class and Dealership class that I created In the previous lab import...

    Below are the Car class and Dealership class that I created In the previous lab import java.util.ArrayList; class Car { private String make; private String model; private int year; private double transmission; private int seats; private int maxSpeed; private int wheels; private String type; public Car() { } public Car(String make, String model, int year, double transmission, int seats, int maxSpeed, int wheels, String type) { this.make = make; this.model = model; this.year = year; this.transmission = transmission; this.seats =...

  • User Profiles Write a program that reads in a series of customer information -- including name,...

    User Profiles Write a program that reads in a series of customer information -- including name, gender, phone, email and password -- from a file and stores the information in an ArrayList of User objects. Once the information has been stored, the program should welcome a user and prompt him or her for an email and password The program should then use a linearSearch method to determine whether the user whose name and password entered match those of any of...

  • For Java Program In this lab you will gain experience using all the concepts we learned...

    For Java Program In this lab you will gain experience using all the concepts we learned to this point, which include classes, methods, collections, and file input/output. Also, you will gain experience in team programming. This assignment will be completed by teams of 2. Each member must complete an equal amount of the work in order to receive credit for this assignment. You must write the programmer’s name in a comment for each method you write. You need to create...

  • In Java(using BlueJ) Purpose Purpose is to practice using file input and output, and array list of objects. Also, this lab specification tells you only what to do, you now have more responsibility to...

    In Java(using BlueJ) Purpose Purpose is to practice using file input and output, and array list of objects. Also, this lab specification tells you only what to do, you now have more responsibility to design how to do it. Problem description You are given a text file called 'Students.txt' that contains information on many students. Your program reads the file, creating many Student objects, all of which will be stored into an array list of Student objects, in the Students...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT