For Java Programming Write a class named CarPlate encapsulating the concept of a
ID: 3920532 • Letter: F
Question
For Java Programming
Write a class named CarPlate encapsulating the concept of a car license plate, assuming that it has the following attributes: the plate number, the state, and its color. Write a client program that creates three CarPlate objects and writes them to a file as objects, then reads them from the file as objects, outputs a description of each of the objects using the toString method (which the CarPlate class should override), and outputs the number of objects. When reading the objects, you should assume that you do not know the number of objects in the file and detect the end of file when an EOFException is thrown by the readObject method
Explanation / Answer
Given below is the code for the question.
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please do rate the answer if it was helpful. Thank you
CarPlate.java
-----------
import java.io.Serializable;
public class CarPlate implements Serializable {
private String state;
private int plateNumber;
private String color;
public CarPlate(){
}
public CarPlate(String state, int number, String col){
this.state = state;
this.color = col;
this.plateNumber = number;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public int getPlateNumber() {
return plateNumber;
}
public void setPlateNumber(int plateNumber) {
this.plateNumber = plateNumber;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String toString(){
return "State: " + state +", Number: " + plateNumber + ", Color: " + color;
}
}
TestCarPlate.java
------
package BANKACCOUNTS;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class TestCarPlate {
public static void main(String[] args) {
CarPlate cp1 = new CarPlate("AZ", 1234, "Red");
CarPlate cp2 = new CarPlate("NY", 5678, "Black");
CarPlate cp3 = new CarPlate("CA", 2222, "White");
try {
//write objects to file
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("license.ser"));
oos.writeObject(cp1);
oos.writeObject(cp2);
oos.writeObject(cp3);
oos.close();
System.out.println("Written 3 objects to file");
//read the object file
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("license.ser"));
CarPlate cp;
while(true){
cp = (CarPlate)(ois.readObject());
System.out.println("Read from file: " + cp);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
output
-----
Written 3 objects to file
Read from file: State: AZ, Number: 1234, Color: Red
Read from file: State: NY, Number: 5678, Color: Black
Read from file: State: CA, Number: 2222, Color: White
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.