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
  • I need help with my IM (instant messaging) java program, I created the Server, Client, and...

    I need help with my IM (instant messaging) java program, I created the Server, Client, and Message class. I somehow can't get both the server and client to message to each other. I am at a roadblock. Here is the question below. Create an IM (instant messaging) java program so that client and server communicate via serialized objects (e.g. of type Message). Each Message object encapsulates the name of the sender and the response typed by the sender. You may...

  • 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 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...

  • 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); } //...

  • I have a program that reads a file and then creates objects from the contents of...

    I have a program that reads a file and then creates objects from the contents of the file. How can I create a linked list of objects and use it with the package class instead of creating and using an array of objects? I am not allowed to use any arrays of objects or any java.util. lists in this program. Runner class: import java.util.Scanner; import java.io.*; class Runner { public static Package[] readFile() { try { File f = new...

  • Please write where its written write your code here!! please use java for the code! please...

    Please write where its written write your code here!! please use java for the code! please use the given code and I will rate thanks Magic index in an array a[1..n] is defined to be an index such that a[ i ] = i. Given an array of integers, write a recursive method to find the first magic index from left to right. If one exists in the given array, return the index number i, otherwise return -1. Here are...

  • 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...

  • Modify the program that you wrote for the last exercise in a file named Baseball9.java that...

    Modify the program that you wrote for the last exercise in a file named Baseball9.java that uses the Player class stored within an array. The program should read data from the file baseball.txt for input. The Player class should once again be stored in a file named Player.java, however Baseball9.java is the only file that you need to modify for this assignment. Once all of the input data from the file is stored in the array, code and invoke a...

  • Step 1: Getting Started Create a new .java file named Lab12.java. At the beginning of this...

    Step 1: Getting Started Create a new .java file named Lab12.java. At the beginning of this file, include your assignment documentation code block. After the documentation block, you will need several import statements. import java.util.Scanner; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; Next, declare the Lab12 class and define the main function. public class Lab12 { public static void main (String [] args) { Step 2: Declaring Variables For this section of the lab, you will need to declare...

  • Please write this in Java, please write comments as you create it, and please follow the...

    Please write this in Java, please write comments as you create it, and please follow the coding instructions. As you are working on this project, add print statements or use the debugger to help you verify what is happening. Write and test the project one line at a time. Before you try to obtain currency exchange rates, obtain your free 32 character access code from this website: https://fixer.io/ Here's the code: 46f27e9668fcdde486f016eee24c554c Choose five international source currencies to monitor. Each...

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
ADVERTISEMENT