Question

Java Write a pair of serialization / deserialization apps that create a user’s Car list, w/...

Java

Write a pair of serialization / deserialization apps that create a user’s Car list, w/ a class named Car, which includes model, VINumber (int), and CarMake (an enum, with valid values FORD, GM, TOYOTA, and HONDA) as fields. Use an array similar to the way the SerializeObjects did to handle several BankAccounts. Your app must include appropriate Exception Handling via try catch blocks (what if the file is not found, or the user inputs characters instead of digits for VINumber).

import java.io.*;
import java.util.Scanner;

public class SerializeObjects
{
public static void main(String[] args)
throws IOException
{
double balance; // An account balance
final int NUM_ITEMS = 3; // Number of accounts
  
// Create a Scanner object for keyboard input.
Scanner keyboard = new Scanner(System.in);
  
// Create a BankAccount2 array
BankAccount2[] accounts =
new BankAccount2[NUM_ITEMS];
  
// Populate the array.
for (int i = 0; i < accounts.length; i++)
{
// Get an account balance.
System.out.print("Enter the balance for " +
"account " + (i + 1) + ": ");
balance = keyboard.nextDouble();

// Create an object in the array.
accounts[i] = new BankAccount2(balance);
}
  
// Create the stream objects.
FileOutputStream outStream =
new FileOutputStream("Objects.dat");
ObjectOutputStream objectOutputFile =
new ObjectOutputStream(outStream);
  
// Write the serialized objects to the file.
for (int i = 0; i < accounts.length; i++)
{
objectOutputFile.writeObject(accounts[i]);
}
  

0 0
Add a comment Improve this question Transcribed image text
Answer #1

package person;

import java.io.Serializable;

public class Car implements Serializable {
  
   public static final String serialVersionUID = "7920640115507587755";
  
   /**
   * @return the model
   */
   public String getModel() {
       return model;
   }

   /**
   * @param model the model to set
   */
   public void setModel(String model) {
       this.model = model;
   }

   /**
   * @return the vINumber
   */
   public int getVINumber() {
       return VINumber;
   }

   /**
   * @param vINumber the vINumber to set
   */
   public void setVINumber(int vINumber) {
       VINumber = vINumber;
   }

   /**
   * @return the make
   */
   public CarMake getMake() {
       return make;
   }

   /**
   * @param make the make to set
   */
   public void setMake(CarMake make) {
       this.make = make;
   }

   String model;
   int VINumber;
   CarMake make;

   enum CarMake {
       FORD, GM, TOYOTA, HONDA
   }

   /**
   * @param model
   * @param vINumber
   * @param make
   */
   public Car(String model, int vINumber, CarMake make) {
       super();
       this.model = model;
       VINumber = vINumber;
       this.make = make;
   }

   /* (non-Javadoc)
   * @see java.lang.Object#toString()
   */
   @Override
   public String toString() {
       return "Car [model=" + model + ", VINumber=" + VINumber + ", make=" + make + "]";
   }
}

===========================

package person;

import java.io.*;
import java.util.Scanner;

import person.Car.CarMake;

public class SerializeObjects {
   public static void main(String[] args) throws IOException {

       final int NUM_ITEMS = 2; // Number of Cars

// Create a Scanner object for keyboard input.
       Scanner keyboard = new Scanner(System.in);

// Create a Car array
       Car[] cars = new Car[NUM_ITEMS];

// Populate the array.
       for (int i = 0; i < cars.length; i++) {
// Get Car Name.
           System.out.print("Enter Car Name for Car" + (i + 1) + ": ");
           String carName = keyboard.next();
           // Get Car Name.
           System.out.print("Enter Car Number for Car" + (i + 1) + ": ");
           int VINumber = keyboard.nextInt();
           // Get Car Make.
           System.out.print("Enter Car Number for Car" + (i + 1) + ": ");
           String make = keyboard.next();
           CarMake carMake = null;
           if(make.equals("FORD")) {
               carMake = CarMake.FORD;
           }else if(make.equals("GM")) {
               carMake = CarMake.GM;
           }else if(make.equals("TOYOTA")) {
               carMake = CarMake.TOYOTA;
           }else if(make.equals("HONDA")) {
               carMake = CarMake.HONDA;
           }
// Create an object in the array.
           cars[i] = new Car(carName, VINumber,carMake);
       }

// Create the stream objects.
       FileOutputStream outStream = new FileOutputStream("Objects.dat");
       ObjectOutputStream objectOutputFile = new ObjectOutputStream(outStream);

// Write the serialized objects to the file.
       for (int i = 0; i < cars.length; i++) {
           objectOutputFile.writeObject(cars[i]);
       }
   }
}

=========================

package person;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;

public class DeSerializeObject {

   public static void main(String[] args) throws ClassNotFoundException, IOException {
       // TODO Auto-generated method stub
       FileInputStream fin = new FileInputStream(new File("Objects.dat"));
       ObjectInputStream oin = new ObjectInputStream(fin);
       Car car = null;
       try {
           while ((car = (Car) oin.readObject()) != null) {
               System.out.println(car.toString());

           }
       } catch (Exception e) {

       }

   }

}
=====================

Output

Console output shows the out after deserialization of object.dat file.

Add a comment
Know the answer?
Add Answer to:
Java Write a pair of serialization / deserialization apps that create a user’s Car list, w/...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Problem: Use the code I have provided to start writing a program that accepts a large...

    Problem: Use the code I have provided to start writing a program that accepts a large file as input (given to you) and takes all of these numbers and enters them into an array. Once all of the numbers are in your array your job is to sort them. You must use either the insertion or selection sort to accomplish this. Input: Each line of input will be one item to be added to your array. Output: Your output will...

  • java read integers from this binary file and when the value is 0 then stop reads...

    java read integers from this binary file and when the value is 0 then stop reads and print its summation here is my code import java.io.FileOutputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Random; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.FileInputStream; class Main { public static void main(String[] args) {        String fname = "out.txt";        prepare(fname);               ObjectInputStream inputStream = null;        // create code here    }    public static void prepare(String fname) {   ...

  • JAVA HELP: Directions Write a program that will create an array of random numbers and output...

    JAVA HELP: Directions Write a program that will create an array of random numbers and output the values. Then output the values in the array backwards. Here is my code, I am having a problem with the second method. import java.util.Scanner; import java.util.Random; public class ArrayBackwards { public static void main(String[] args) { genrate(); print(); } public static void generate() { Scanner scanner = new Scanner(System.in);    System.out.println("Seed:"); int seed = scanner.nextInt();    System.out.println("Length"); int length = scanner.nextInt(); Random random...

  • Java. Please write the output import java.util.Scanner; import java.text.Decimalformat: * This program demonstrates two-dimensional array. public...

    Java. Please write the output import java.util.Scanner; import java.text.Decimalformat: * This program demonstrates two-dimensional array. public class CorpSales public static void main(String[] args) Final int DIVS - 3; // Three divisions in the company final int QTRS = 4; // Four quarters double totalSales - e.e; / Accumulator // Create an array to hold the sales for each // division, for each quarter. double[][] sales - new double[DIVS][QTRS] // Create a Scanner object for keyboard input. Scanner keyboard = new...

  • Can someone help me with my Java code error! Domain package challenge5race; import java.util.Random; public class...

    Can someone help me with my Java code error! Domain package challenge5race; import java.util.Random; public class Car {    private int year; private String model; private String make; int speed; public Car(int year, String model, String make, int speed) { this.year = year; this.model = model; this.make = make; this.speed = speed; } public Car() { } public int getYear() { return year; } public void setYear(int year) { this.year = year; } public String getModel() { return model; }...

  • Java : Please help me correct my code: create a single class (Program11.java) with a main...

    Java : Please help me correct my code: create a single class (Program11.java) with a main method and some auxiliary methods to input a 2-D array from a disk file, input some “transactions” to change the 2-D array and output the changed 2-D array to another file. Your main method will be minimal (see below). Most of the work will go on in your methods. In main, declare (but do not instantiate) 2-D array. Then call the three methods. The...

  • I need to write a program in java that reads a text file with a list...

    I need to write a program in java that reads a text file with a list of numbers and sorts them from least to greatest. This is the starter file. import java.util.*; import java.io.*; public class Lab3 { static final int INITIAL_CAPACITY = 5; public static void main( String args[] ) throws Exception { // ALWAYS TEST FOR REQUIRED INPUT FILE NAME ON THE COMMAND LINE if (args.length < 1 ) { System.out.println("\nusage: C:\\> java Lab3 L3input.txt\n"); System.exit(0); } //...

  • Write a program to create a file named integerFile.txt if it does not exist. Write 100...

    Write a program to create a file named integerFile.txt if it does not exist. Write 100 integers created randomly into the file using text I/O. The random integers should be in the range 0 to 100 (including 0 and 100). Integers should be separated by spaces in the file. Read the data back from the file and display the data in increasing order This is what I have so far: import java.io.File; import java.util.Scanner; import java.io.PrintWriter; public class integerFile {...

  • Java Program 1. Write a class that reads in a group of test scores (positive integers...

    Java Program 1. Write a class that reads in a group of test scores (positive integers from 1 to 100) from the keyboard. The main method of the class calls a few other methods to calculate the average of all the scores as well as the highest and lowest score. The number of scores is undetermined but there will be no more than 50. A negative value is used to end the input loop. import java.util.Scanner; public class GradeStat {...

  • Write a Java method that will take an array of integers of size n and shift...

    Write a Java method that will take an array of integers of size n and shift right by m places, where n > m. You should read your inputs from a file and write your outputs to another file. Create at least 3 test cases and report their output. I've created a program to read and write to separate files but i don't know how to store the numbers from the input file into an array and then store the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT