Write a class named CarPlate encapsulating the concept of a car license plate, a
ID: 3671292 • Letter: W
Question
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, 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.Explanation / Answer
CarPlate.java
public class CarPlate implements java.io.Serializable{
//instance variables
private String plateNumber;
private int state;
private String color;
/**
* Default constructor
* <p>
* Initializes plateNumber to 000 000, state to 1, and color to None
*/
public CarPlate(){
plateNumber = "000 000";
state = 1;
color = "None";
}
/**
* Overloaded constructor
* <p>
* Initializes fields from arguments, checks arguments for validity
* @param startPlateNumber
* @param startState
* @param startColor
*/
public CarPlate(String startPlateNumber, int startState, String startColor){
setPlateNumber(startPlateNumber);
setState(startState);
setColor(startColor);
}
/**
* Returns the plate number
* @return Plate number
*/
public String getPlateNumber(){
return plateNumber;
}
/**
* Returns the state
* @return state
*/
public int getState(){
return state;
}
/**
* Returns the color of the license plate
* @return Color of the license plate
*/
public String getColor(){
return color;
}
/**
* Sets the plate number of the license plate
* @param xPlateNumber - New plate number for the license plate
* @throws IllegalArgumentException - For arguments that are not 7
* characters in length and for arguments that do not have a space at the
* 3rd index position
*/
public final void setPlateNumber(String xPlateNumber) throws
IllegalArgumentException{
if (xPlateNumber.length() != 7)
throw new IllegalArgumentException("License plate number must be "
+ "7 characters long");
if (xPlateNumber.charAt(3) != ' ')
throw new IllegalArgumentException("Licese plate number must be two"
+ " groups of 3 characters separated by a space at the 3rd"
+ " index position");
plateNumber = xPlateNumber;
}
/**
* Sets the State of the license plate
* @param xState - New state for the license plate
* @throws IllegalArgumentException - For arguments that are not integers
* ranging 1-50
*/
public final void setState(int xState) throws IllegalArgumentException{
if (xState < 1 || xState > 50)
throw new IllegalArgumentException("State must be a whole number "
+ "1-50");
state = xState;
}
/**
* Sets the color for the license plate
* <p>
* The argument is simply assigned to the variable, any String input is
* considered valid.
* @param xColor - New color for the license plate
*/
public final void setColor(String xColor){
color = xColor;
}
/**
* toString method, Returns a printable version of the data contained in
* the object
* @return Printable string of data contained in object
*/
@Override
public String toString(){
String output = "Plate number: " + plateNumber;
output += " State: " + state;
output += " Color: " + color;
return output;
}
/**
* Equals method, compares two CarPlate objects for equality by comparing
* fields
* @return True if objects are equal, false if not
*/
@Override
public boolean equals(Object obj) {
if (obj == null)
return false;
if (!(obj instanceof CarPlate))
return false;
CarPlate other = (CarPlate)obj;
if (!(plateNumber.equals(other.getPlateNumber())))
return false;
if (state != other.getState())
return false;
return color.equals(other.getColor());
}
}
CarPlateTest.java
import java.util.Random;
import java.io.*;
public class CarPlateTest {
//random number generator for class
private static final Random rand = new Random();
/**
* Generates a random license plate number in the format "000 000", where
* the two groups of characters are randomly generated as either integers
* 1-9 or as letters
* @return Randomly generated license plate number as a string
*/
private static String generatePlateNumber(){
String plateNumber = "";
for (int i = 0; i < 7; i++){
if (i == 3) //generates space for middle character
plateNumber += " ";
else if (Math.random() > .5) // generates a letter
plateNumber += (char)(rand.nextInt(26) + 'a');
else //generates a number
plateNumber += rand.nextInt(10);
}
return plateNumber.toUpperCase();
}
/**
* Generates a random integer 1-50 representing the State of the license
* plate
* @return Random integer 1-50
*/
private static int generateState(){
return rand.nextInt(50) + 1;
}
/**
* Generates a random color from a predefined array of 10 color choices
* @return String containing a color
*/
private static String generateColor(){
String[] colorList = new String[]{ "Red", "White", "Blue", "Black",
"Green", "Orange", "Pink", "Yellow", "Purple", "Periwinkle"};
return colorList[rand.nextInt(10)];
}
public static void main(String[] args) {
//generate 3 random carPlates objects and write them to a txt file
try {
FileOutputStream fos = new FileOutputStream("carPlates.txt", false);
ObjectOutputStream oos = new ObjectOutputStream(fos);
try{
//generate and write 3 objects to the file
for (int i = 0; i < 3; i++){
CarPlate tmpCarPlate = new CarPlate(generatePlateNumber(),
generateState(),
generateColor());
oos.writeObject(tmpCarPlate);
}
}
finally{
oos.close();
}
}
catch(FileNotFoundException fnfe){
System.out.println("Could not create/modify the file carPlates.txt "
+ " There is probably a problem with the permissions.");
System.out.print("Message: ");
System.out.println(fnfe.getMessage());
System.out.print("toString: ");
System.out.println(fnfe.toString());
fnfe.printStackTrace(System.out);
}
catch(IOException ioe){
System.out.print("There was a problem writing to the file: ");
System.out.println(ioe.getMessage());
System.out.print("toString: ");
System.out.println(ioe.toString());
ioe.printStackTrace(System.out);
}
//read objects from txt file, print toString for each object to the
//console window and write toString to new txt file output.
try {
FileInputStream fis = new FileInputStream("carPlates.txt");
ObjectInputStream ois = new ObjectInputStream(fis);
FileOutputStream fos = new FileOutputStream("output.txt", false);
PrintWriter pw = new PrintWriter(fos);
try {
while(true){
CarPlate tmp = (CarPlate)ois.readObject();
System.out.println(tmp.toString());
pw.println(tmp.toString());
}
}
catch(EOFException eofe){
System.out.println(" End of file reached");
}
catch(ClassNotFoundException cnfe){
System.out.println("A ClassNotFoundException was caught: " +
cnfe.getMessage());
System.out.println(cnfe.toString());
cnfe.printStackTrace(System.out);
}
finally{
System.out.println("Closing file");
ois.close();
pw.close();
}
}
catch(FileNotFoundException fnfe){
System.out.println("Couldn't find the file: " +
fnfe.getMessage());
System.out.print("toString: ");
System.out.println(fnfe.toString());
fnfe.printStackTrace(System.out);
}
catch(IOException ioe){
System.out.print("There was a problem reading from the file: ");
System.out.println(ioe.getMessage());
System.out.print("toString: ");
System.out.println(ioe.toString());
ioe.printStackTrace(System.out);
}
}
}
output
Plate number: RZ2 2UH
State: 20
Color: Blue
Plate number: 38N MS4
State: 1
Color: Pink
Plate number: K7T 696
State: 40
Color: Periwinkle
End of file reached
Closing file
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.