*** 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 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 31000 1960
Add remaining two 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 same smallest year
// return first such car in the list.
public int countCarsWithModel(String model) // Method accepts a
model
//and returns count of cars with given model.
public ArrayList<Car> antiqueCarList() // Returns ArrayList
of all antique
//cars from the list. The method antiqueCarList is extra credit
worth 1 point.
The last three methods just return the specified data type. Do
not print anything within those methods. Just return 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 countCarsWithModel returns
zero, report that there are no cars with specified model, and
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.
There are total of 3 files.
1. Car.java which contains all the methods.
2. CarList.java which contains the car list and all the methods
3. TestCarList.java which contains the main method to test our CarList
Additionally, I added comments for each and every method.
Also, I am attaching blueJ screenshots.
Code for Car.java class
class Car
{
private String vin;
private String make;
private String model;
private double cost;
private int year;
// Constructor to create car class object
public Car(String vin , String make , String model , double cost , int year){
this.vin = vin;
this.make = make;
this.model = model;
this.cost = cost;
this.year = year;
}
// returns car’s model
public String getModel()
{
return model;
}
// returns car’s year
public int getYear()
{
return year;
}
// returns true if car has cost above 30000 and false otherwise
public boolean isExpensive()
{
if(cost > 30000){
return true;
}
return false;
}
// returns true if car’s year is before 1968, and false otherwise
public boolean isAntique()
{
if(year < 1968){
return true;
}
return false;
}
// returns string with all car’s data in one line separated by tabs.
public String toString()
{
String s = vin + "\t" + make + "\t" + model + "\t" + cost + "\t" + year ;
return s;
}
}
Code for CarList.java
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
class CarList {
private ArrayList<Car> carList;
// Constructor to read all car details from text file
// In this Method, line by line is stored in string and then each line is converted into array of string using split method.
// We pass space " " to split the words separated by space and then create a car class object and add into carList
public CarList(String fileName) throws IOException {
carList = new ArrayList<Car>();
String[] words;
BufferedReader reader = new BufferedReader(new FileReader(fileName));
String line = reader.readLine();
while (line != null) {
words = line.split(" ");
Car car = new Car(words[0], words[1], words[2], Integer.parseInt(words[3]), Integer.parseInt(words[4]));
carList.add(car);
line = reader.readLine();
}
reader.close();
}
// Prints title followed by list of all cars (each row has data for one car)
public void printList() {
for (Car car : carList) {
System.out.println(car.toString());
}
}
//Prints "List of expensive cars:" followed by complete data for each expensive car
public void printExpensiveCar()
{
System.out.println("List of expensive cars:");
for (Car car : carList) {
if(car.isExpensive()){
System.out.println(car.toString());
}
}
}
// Method returns oldest Car (which has smallest year.)
public Car oldestCar() {
int year = carList.get(0).getYear();
for (Car car : carList) {
if (car.getYear() < year) {
year = car.getYear();
}
}
for (Car car : carList) {
if (year == car.getYear()) {
return car;
}
}
return null;
}
// Method accepts a model and returns count of cars with given model.
public int countCarsWithModel(String model) {
int count = 0;
for (Car car : carList) {
if (car.getModel().equals(model)) {
count += 1;
}
}
return count;
}
// Returns ArrayList of all antique cars from the list
public ArrayList<Car> antiqueCarList() {
ArrayList<Car> listOfAntiqueCars = new ArrayList<Car>();
for (Car car : carList) {
if (car.isAntique()) {
listOfAntiqueCars.add(car);
}
}
return listOfAntiqueCars;
}
}
Code for TestCarList.java
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
public class TestCarList
{
public static void main(String[] args) throws IOException{
// Reading the file name from user in which car details are stored
Scanner sc = new Scanner(System.in);
System.out.println("Enter the text file name in which car details are stored");
String fileName = sc.nextLine();
CarList carList = new CarList(fileName);
// print all cars
carList.printList();
// print the oldest car
Car oldestCar = carList.oldestCar();
System.out.println("Oldest Car is : " + oldestCar.toString());
// Count car with model
int count = carList.countCarsWithModel("Mustang");
System.out.println("Total number of cars with Mustang is " + count);
// List of Antique cars
ArrayList<Car> antiqueCar = carList.antiqueCarList();
System.out.println("List of Antique Cars:");
for (Car car : antiqueCar) {
System.out.println(car.toString());
}
// list of expensive car
carList.printExpensiveCar();
sc.close();
}
}
Screenshots of BlueJ
Feel free to comment for any doubts and give thumbs up!
Thank you!
*** FOR A BEGINNER LEVEL JAVA CLASS, PLEASE KEEP CODE SIMPLE, AND WE USE BLUE J...
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...
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...
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 =...
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...
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...
In Java programming language Please write code for the 6 methods below: Assume that Student class has name, age, gpa, and major, constructor to initialize all data, method toString() to return string reresentation of student objects, getter methods to get age, major, and gpa, setter method to set age to given input, and method isHonors that returns boolean value true for honors students and false otherwise. 1) Write a method that accepts an array of student objects, and n, the...
Exercise 11 - in Java please complete the following: For this exercise, you need to work on your own. In this exercise you will write the implementation of the pre-written implementation of the class CAR. The class CAR has the following data and methods listed below: . Data fields: A String model that stores the car model, and an int year that stores the year the car was built. . Default constructor . Overloaded constructor that passes values for both...
This is a java program that runs on the Eclipse. Java Programming 2-1: Java Class Design Interfaces Practice Activities Lesson objectives: Model business problems using Java classes Make classes immutable User Interfaces Vocabulary: Identify the vocabulary word for each definition below. A specialized method that creates an instance of a class. A keyword that qualifies a variable as a constant and prevents a method from being overridden in a subclass. A class that it can't be overridden by a subclass,...
All those points need to be in the code Project 3 Iterative Linear Search, Recursive Binary Search, and Recursive Selection Sort Class River describes river's name and its length in miles. It provides accessor methods (getters) for both variables, toString() method that returns String representation of the river, and method isLong() that returns true if river is above 30 miles long and returns false otherwise. Class CTRivers describes collection of CT rivers. It has no data, and it provides the...
P9.17 (For Java, and can you please explain as well.) Declare an interface Filter as follows: public interface Filter { boolean accept(Object x); } Write a method: public static ArrayList collectAll(ArrayList objects, Filter f) that returns all objects in the objects list that are accepted by the given filter. Provide a class ShortWordFilter whose filter method accepts all strings of length < 5. Then write a program that asks the user for input and output textfile names, reads all words...