Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java problem The code below tries to store the ArrayList of Car objects in a fil

ID: 3851730 • Letter: J

Question

Java problem The code below tries to store the ArrayList of Car objects in a file, but the file turns out to be empty. The problem is that Java does not know whether or not to write objects of type Car to a file. What interface should the Car class implement to fix this problem? import java.io.*: import java.util.ArrayList: class Car { String make: String model: public Car(String make. String model) { this.make = make: this.model = model: } } public class Question { public static void main(String[] args) throws Exception { ArrayList fleet = new ArrayList (): fleet.addCar ("Honda", "Civic"): fleet.addCar("Ford", "Taurus"): ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new File("fleet.dat"))): oos.writeObject(fleet): oos.close(): } }

Explanation / Answer

////Program output will be feelt.txt file and if u opened it will show in byte code and to get proper content we should Deserialize above program/////

import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
class Car implements Serializable {
String make;
String model;
public Car(String make,String model) { this.make=make;
this.model=model;
}
}
public class Question {
public static void main(String[] args) throws Exception {
ArrayList fleet=new ArrayList(); fleet.add(new Car("Honda", "Civic")); fleet.add(new Car("Ford", "Taurus")); FileOutputStream fos =new
FileOutputStream("fleet.txt"); ObjectOutputStream out =new ObjectOutputStream(fos); out.writeObject(fleet); out.close(); fos.close(); }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote