Question:
JAVA HELP? :( Challenging CarPlate question coding..?
KJ
2013-12-14 23:29:09 UTC
PLEASE HELP :( I don't know what to do ... I asked the professor, but asking didn't help that much because I'm still as lost as ever..and yes I did read the textbook *_* .

this is the 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 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.

and also please don't just write comments in **// ASTERIX//** as those don't help much because that is exactly where I hit a wall and fall off of a cliff and drown because I don't know how to fill those in ;(
Three answers:
Leo D
2013-12-14 23:45:38 UTC
If you're already dealing with serialization, I assume you know how to implement toString and every thing. Read this tutorial from the official Java website. That should teach you all you need to know :) http://docs.oracle.com/javase/tutorial/essential/io/index.html



Also check out the API for serializable: http://docs.oracle.com/javase/7/docs/api/java/io/Serializable.html Check all of the classes in the "See Also" section too: ObjectOutputStream, ObjectInputStream, ObjectOutput, ObjectInput, Externalizable.
brilliant_moves
2013-12-15 11:10:17 UTC
Using the javase tutorials, I managed to get the code you needed and after a bit of jiggery-pokery, finally got it to work. Hope it's what you were after. Good luck!



import java.io.*;



public class CarPlate implements Serializable {



private String plateNumber;

private String state;

private String color;



public CarPlate() {} // default constructor



public CarPlate(String pNumber, String st, String clr) {

plateNumber = pNumber;

state = st;

color = clr;

} // end overloaded constructor



public String toString() {

String s = "plate number: "+plateNumber;

s += " state: "+state;

s += " color: "+color;

return s;

} // end toString()

} // end class CarPlate



...and your client class:



import java.io.*;



public class TestCarPlate {



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



CarPlate plate0 = new CarPlate("ABC 123", "New Jersey", "Silver");

CarPlate plate1 = new CarPlate("DEF 1000", "Ohio", "Red");

CarPlate plate2 = new CarPlate("GH 3", "Kentucky", "Blue");



File f = new File("cardata.tmp");

ObjectOutputStream oos = null;



try {

FileOutputStream fos = new FileOutputStream(f);

oos = new ObjectOutputStream(fos);

oos.writeObject(plate0);

oos.writeObject(plate1);

oos.writeObject(plate2);

} catch (IOException e) {

e.printStackTrace();

} finally {

if (oos != null) oos.close();

} // end try



int c = 0;

CarPlate cp;



ObjectInputStream ois = null;



try {

FileInputStream fis = new FileInputStream(f);

ois = new ObjectInputStream(fis);

while (true) {

cp = (CarPlate) ois.readObject();

System.out.println(cp.toString());

c++;

} // end while

} catch (EOFException e) {

System.out.println("Number of objects = "+c);

} catch (ClassNotFoundException e) {

System.out.println("Class not found");

} finally {

if (ois != null) ois.close();

} // end try

} // end main()

} // end class TestCarPlate
?
2017-02-09 17:50:49 UTC
1


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...