Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

package com.company; import java.util.ArrayList; import java.util.Collections; import java.util.Scanner; import java.io.*; import java.lang.Comparable; public class Main { public static void main(String[] args) { // write your

package com.company; import java.util.ArrayList; import java.util.Collections; import java.util.Scanner; import java.io.*; import java.lang.Comparable; public class Main { public static void main(String[] args) { // write your code here  class cars{ } ArrayList carList=new ArrayList(); //read binary file and load cars to arraylist  Car car = null; try { FileInputStream fileIn = new FileInputStream("cars.ser"); ObjectInputStream in = new ObjectInputStream(fileIn); while(true){ try{ car = (Car) in.readObject(); carList.add(car); }catch(IOException e){ break; } } in.close(); fileIn.close(); }catch(IOException i) { }catch(ClassNotFoundException c) { } //print out list of cars  if(carList.isEmpty()) System.out.println("List is Empty!"); else{ System.out.println("Listing Cars (sorted by model,make,year):"); for(int i=0;iout.println(carList.get(i).getModel()); } } //create a new car by user  Scanner userInput=new Scanner(System.in); System.out.print("Creating new Car. Enter a model:"); Car newCar =new Car(userInput.nextLine().trim()); //add to list  carList.add(newCar); //sort list  Collections.sort(carList); //print list  System.out.println(" Listing Cars (sorted by model):"); for(int i=0;iout.println(carList.get(i).getModel()); } //save arrayList to file  try { FileOutputStream fileOut = new FileOutputStream("cars.ser"); ObjectOutputStream out = new ObjectOutputStream(fileOut); for(int i=0;icatch(IOException e) { e.printStackTrace(); } System.out.println("List successfully backed up!"); } } class Car implements Serializable,Comparable{ private String model; Car(String model){ this.model=model; } public String getModel(){ return this.model; } public int compareTo(Car car){ return this.model.compareTo(car.getModel()); } } 

output I have right now

"C:\Program Files\Java\jdk1.8.0_131\bin\java" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2017.1.2\lib\idea_rt.jar=52867:C:\Program Files\JetBrains\IntelliJ IDEA 2017.1.2\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_131\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext ashorn.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib esources.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib t.jar;C:\Users\New Owner\IdeaProjects\cars\out\production\cars" com.company.Main Listing Cars (sorted by model): Acura Audi bmw Creating new Car. Enter a model:

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

SQL Antipatterns Avoiding The Pitfalls Of Database Programming

Authors: Bill Karwin

1st Edition

1680508989, 978-1680508987

More Books

Students also viewed these Databases questions

Question

How will you monitor progress and sustainability?

Answered: 1 week ago