Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

( follow the comments ) here is the code that you should use and fix it to get the output in the picture import java.util.ArrayList;

( follow the comments )
here is the code that you should use and fix it to get the output in the picture
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();
}
}
image text in transcribed
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 Car Tester class and accordingly write the required code in class CarRep. Compile and run the code. You should have this output: inport java.util. Arraylist; public class Cartester \{ public CartesterO \{ Arraylist cars = nen Arraylist clar>O; cars. add(new Car("A123", "Toyota", 5eeee)); cars, add(new Car ("B123", "Hondo", 40000)); cars, add(new Car("C123", "Nissan", 6e000)); cars, add(nen Car("D123", "Ford", 3ese0)); I/Write the objects of the list "theCars" to the file. "carsfile" as objects. Carkepo, writeToobjfiled"carsfile", cars): //Read the cars objects from the file "corsfile" into the list "theCars" Arraylist theCars; theCars = CarRepo, readfranobjFile("carsfile"); for(Car c : the Cars) \{ 3 System, aut, println(c.toString O) ): 1/ modify some the second car object by adding 2000 to the price. theCars, get(1), setPrice(theCars, get(1).getPriceO+20ee): //write the data of the cars objects of the list "theCars" to a text file "carsfile.txt" CarRepo, writeTofextfile "carsfile. tat", thetars); //Read the dota of the cars object from the text file "corsfile. txt" into the list theCarsz. ArrayListeCars theCars2 - Carkepo. readfronfextfile("carsfile. txt"): //Add the cor objects of the list "theCars2" to the list "theCars" for(Car-c : the Cars2) f the Cars.add(c): 3 /roisplay the contents of the list "theCars". for(Car c : the(ars) \{ System, out, println(c,tostring ()) : 3 3 public static void main(String args) \& new CarTester(); 3

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases Theory And Applications 27th Australasian Database Conference Adc 20 Sydney Nsw September 28 29 20 Proceedings Lncs 9877

Authors: Muhammad Aamir Cheema ,Wenjie Zhang ,Lijun Chang

1st Edition

3319469215, 978-3319469218

More Books

Students also viewed these Databases questions

Question

What are oxidation and reduction reactions? Explain with examples

Answered: 1 week ago

Question

1. Identify and control your anxieties

Answered: 1 week ago