Question
complete CarTester please ! 2. [2.5 pts] Consider the class Car that implements Serializable. The Car class has three attributes plateNumber, make, and price. -
complete CarTester please !
2. [2.5 pts] Consider the class Car that implements Serializable. The Car class has three attributes plateNumber, make, and price. - Use the following code of the
CarTester class and accordingly write the required code in class CarRep.
- Compile and run the code. You should have this output:
import java.util.ArrayList;
public class CarTester {
public CarTester() {
ArrayList cars = new ArrayList();
cars.add(new Car("A123", "Toyota", 50000));
cars.add(new Car("B123", "Honda", 40000)); cars.add(new Car("C123", "Nissan", 60000));
cars.add(new Car("D123", "Ford", 30000));
//Write the objects of the list "theCars" to the file "carsfile" as objects.
CarRepo.writeToObjFile("carsfile", cars); //Read the cars objects from the file "carsfile" into the list "theCars" ArrayList theCars; theCars = CarRepo.readFromObjFile("carsfile"); for(Car c : theCars) { System.out.println(c.toString()); } //modify some the second car object by adding 2000 to the price. theCars.get(1).setPrice(theCars.get(1).getPrice()+2000); //write the data of the cars objects of the list "theCars" to a text file "carsfile.txt"
CarRepo.writeToTextFile("carsfile.txt", theCars);
//Read the data of the cars object from the text file "carsfile.txt" into the list theCars2.
ArrayList theCars2 = CarRepo.readFromTextFile("carsfile.txt");
//Add the car objects of the list "theCars2" to the list "theCars"
for(Car c : theCars2) { theCars.add(c); } //Display the contents of the list "theCars". for(Car c : theCars) {
System.out.println(c.toString()); } }
public static void main(String[] args) { new CarTester(); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started