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

The user should be able to add new car objects using GUI textboxes and an add bu

ID: 3850367 • Letter: T

Question

The user should be able to add new car objects using GUI textboxes and an add button (The objects should be inserted into their sorted position in the JList model) user should be able to delete objects in the JList by selecting them and using a delete button. When the program closes, the objects in JList model should be saved back out to the binary file.
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) {


class cars {
}
ArrayList<Car> carList = new ArrayList<Car>();
// 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 make,model,year):");
for (int i = 0; i < carList.size(); i++) {
System.out.println(
carList.get(i).getMake() + " " + carList.get(i).getModel() + " " + carList.get(i).getYear());
}
}
// create a new car by user
Scanner userInput = new Scanner(System.in);
System.out.println("Creating new Car. Enter a make:");
String model, make, year;
model = userInput.nextLine();
System.out.println("Enter a model: ");
make = userInput.next();
System.out.println("Enter the year: ");
year = userInput.next();
Car newCar = new Car(make, model, year);
// add to list
carList.add(newCar);
// sort list
Collections.sort(carList);
// print list
System.out.println(" Listing Cars (sorted by make):");
for (int i = 0; i < carList.size(); i++) {
System.out.println(
carList.get(i).getMake() + " " + carList.get(i).getModel() + " " + carList.get(i).getYear());
}
// save arrayList to file
try
{
FileOutputStream fileOut = new FileOutputStream("cars.ser");
ObjectOutputStream out = new ObjectOutputStream(fileOut);
for (int i = 0; i < carList.size(); i++) {
out.writeObject(carList.get(i));
}
out.close();
fileOut.close();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("List successfully backed up!");
}
}
class Car implements Serializable, Comparable<Car> {
private String make;
private String model;
private String year;
Car(String make, String model, String year) {
this.model = make;
this.make = model;
this.year = year;
}
public String getMake() {
return this.make;
}
public String getModel() {
return this.model;
}
public String getYear() {
return this.year;
}
@Override
public int compareTo(Car o) {
int i;
i = this.make.compareTo(o.make);
if (i != 0)
return i;
i = this.model.compareTo(o.model);
if (i != 0)
return i;
else
return this.year.compareTo(o.year);
}
}

The user should be able to add new car objects using GUI textboxes and an add button (The objects should be inserted into their sorted position in the JList model) user should be able to delete objects in the JList by selecting them and using a delete button. When the program closes, the objects in JList model should be saved back out to the binary file.
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) {


class cars {
}
ArrayList<Car> carList = new ArrayList<Car>();
// 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 make,model,year):");
for (int i = 0; i < carList.size(); i++) {
System.out.println(
carList.get(i).getMake() + " " + carList.get(i).getModel() + " " + carList.get(i).getYear());
}
}
// create a new car by user
Scanner userInput = new Scanner(System.in);
System.out.println("Creating new Car. Enter a make:");
String model, make, year;
model = userInput.nextLine();
System.out.println("Enter a model: ");
make = userInput.next();
System.out.println("Enter the year: ");
year = userInput.next();
Car newCar = new Car(make, model, year);
// add to list
carList.add(newCar);
// sort list
Collections.sort(carList);
// print list
System.out.println(" Listing Cars (sorted by make):");
for (int i = 0; i < carList.size(); i++) {
System.out.println(
carList.get(i).getMake() + " " + carList.get(i).getModel() + " " + carList.get(i).getYear());
}
// save arrayList to file
try
{
FileOutputStream fileOut = new FileOutputStream("cars.ser");
ObjectOutputStream out = new ObjectOutputStream(fileOut);
for (int i = 0; i < carList.size(); i++) {
out.writeObject(carList.get(i));
}
out.close();
fileOut.close();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("List successfully backed up!");
}
}
class Car implements Serializable, Comparable<Car> {
private String make;
private String model;
private String year;
Car(String make, String model, String year) {
this.model = make;
this.make = model;
this.year = year;
}
public String getMake() {
return this.make;
}
public String getModel() {
return this.model;
}
public String getYear() {
return this.year;
}
@Override
public int compareTo(Car o) {
int i;
i = this.make.compareTo(o.make);
if (i != 0)
return i;
i = this.model.compareTo(o.model);
if (i != 0)
return i;
else
return this.year.compareTo(o.year);
}
}

The user should be able to add new car objects using GUI textboxes and an add button (The objects should be inserted into their sorted position in the JList model) user should be able to delete objects in the JList by selecting them and using a delete button. When the program closes, the objects in JList model should be saved back out to the binary file.
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) {


class cars {
}
ArrayList<Car> carList = new ArrayList<Car>();
// 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 make,model,year):");
for (int i = 0; i < carList.size(); i++) {
System.out.println(
carList.get(i).getMake() + " " + carList.get(i).getModel() + " " + carList.get(i).getYear());
}
}
// create a new car by user
Scanner userInput = new Scanner(System.in);
System.out.println("Creating new Car. Enter a make:");
String model, make, year;
model = userInput.nextLine();
System.out.println("Enter a model: ");
make = userInput.next();
System.out.println("Enter the year: ");
year = userInput.next();
Car newCar = new Car(make, model, year);
// add to list
carList.add(newCar);
// sort list
Collections.sort(carList);
// print list
System.out.println(" Listing Cars (sorted by make):");
for (int i = 0; i < carList.size(); i++) {
System.out.println(
carList.get(i).getMake() + " " + carList.get(i).getModel() + " " + carList.get(i).getYear());
}
// save arrayList to file
try
{
FileOutputStream fileOut = new FileOutputStream("cars.ser");
ObjectOutputStream out = new ObjectOutputStream(fileOut);
for (int i = 0; i < carList.size(); i++) {
out.writeObject(carList.get(i));
}
out.close();
fileOut.close();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("List successfully backed up!");
}
}
class Car implements Serializable, Comparable<Car> {
private String make;
private String model;
private String year;
Car(String make, String model, String year) {
this.model = make;
this.make = model;
this.year = year;
}
public String getMake() {
return this.make;
}
public String getModel() {
return this.model;
}
public String getYear() {
return this.year;
}
@Override
public int compareTo(Car o) {
int i;
i = this.make.compareTo(o.make);
if (i != 0)
return i;
i = this.model.compareTo(o.model);
if (i != 0)
return i;
else
return this.year.compareTo(o.year);
}
} 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) {


class cars {
}
ArrayList<Car> carList = new ArrayList<Car>();
// 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 make,model,year):");
for (int i = 0; i < carList.size(); i++) {
System.out.println(
carList.get(i).getMake() + " " + carList.get(i).getModel() + " " + carList.get(i).getYear());
}
}
// create a new car by user
Scanner userInput = new Scanner(System.in);
System.out.println("Creating new Car. Enter a make:");
String model, make, year;
model = userInput.nextLine();
System.out.println("Enter a model: ");
make = userInput.next();
System.out.println("Enter the year: ");
year = userInput.next();
Car newCar = new Car(make, model, year);
// add to list
carList.add(newCar);
// sort list
Collections.sort(carList);
// print list
System.out.println(" Listing Cars (sorted by make):");
for (int i = 0; i < carList.size(); i++) {
System.out.println(
carList.get(i).getMake() + " " + carList.get(i).getModel() + " " + carList.get(i).getYear());
}
// save arrayList to file
try
{
FileOutputStream fileOut = new FileOutputStream("cars.ser");
ObjectOutputStream out = new ObjectOutputStream(fileOut);
for (int i = 0; i < carList.size(); i++) {
out.writeObject(carList.get(i));
}
out.close();
fileOut.close();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("List successfully backed up!");
}
}
class Car implements Serializable, Comparable<Car> {
private String make;
private String model;
private String year;
Car(String make, String model, String year) {
this.model = make;
this.make = model;
this.year = year;
}
public String getMake() {
return this.make;
}
public String getModel() {
return this.model;
}
public String getYear() {
return this.year;
}
@Override
public int compareTo(Car o) {
int i;
i = this.make.compareTo(o.make);
if (i != 0)
return i;
i = this.model.compareTo(o.model);
if (i != 0)
return i;
else
return this.year.compareTo(o.year);
}
}

Explanation / Answer

The code is given below :

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) {

class cars {

}

ArrayList<Car> carList = new ArrayList<Car>();

// 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 make,model,year):");

for (int i = 0; i < carList.size(); i++) {

System.out.println(

carList.get(i).getMake() + " " + carList.get(i).getModel() + " " + carList.get(i).getYear());

}

}

// create a new car by user

Scanner userInput = new Scanner(System.in);

System.out.println("Creating new Car. Enter a make:");

String model, make, year;

model = userInput.nextLine();

System.out.println("Enter a model: ");

make = userInput.next();

System.out.println("Enter the year: ");

year = userInput.next();

Car newCar = new Car(make, model, year);

// add to list

carList.add(newCar);

// sort list

Collections.sort(carList);

// print list

System.out.println(" Listing Cars (sorted by make):");

for (int i = 0; i < carList.size(); i++) {

System.out.println(

carList.get(i).getMake() + " " + carList.get(i).getModel() + " " + carList.get(i).getYear());

}

// save arrayList to file

try

{

FileOutputStream fileOut = new FileOutputStream("cars.ser");

ObjectOutputStream out = new ObjectOutputStream(fileOut);

for (int i = 0; i < carList.size(); i++) {

out.writeObject(carList.get(i));

}

out.close();

fileOut.close();

} catch (IOException e) {

e.printStackTrace();

}

System.out.println("List successfully backed up!");

}

}

class Car implements Serializable, Comparable<Car> {

private String make;

private String model;

private String year;

Car(String make, String model, String year) {

this.model = make;

this.make = model;

this.year = year;

}

public String getMake() {

return this.make;

}

public String getModel() {

return this.model;

}

public String getYear() {

return this.year;

}

@Override

public int compareTo(Car o) {

int i;

i = this.make.compareTo(o.make);

if (i != 0)

return i;

i = this.model.compareTo(o.model);

if (i != 0)

return i;

else

return this.year.compareTo(o.year);

}

}