mk9000
asked on
How can I create a Java program that creates license plate objects and then reads and writes them?
Here is the problem in full:
" 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. Also it has the following methods: two constructors, toString(), equals(), accessors, and mutators.
" 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 , 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.
I have done this so far. And it works, but isn't exactly done the way that my problem asks. Also, I can't get the program to count the number of entries. I tried using count, but can't make it work.
" 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. Also it has the following methods: two constructors, toString(), equals(), accessors, and mutators.
" 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 , 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.
I have done this so far. And it works, but isn't exactly done the way that my problem asks. Also, I can't get the program to count the number of entries. I tried using count, but can't make it work.
public class CarPlate {
private String plateLetter;
private String plateNumber;
private String plateState;
private String plateColor;
private int plateTotal;
private int count;
public CarPlate()
{
plateLetter="";
plateNumber="";
plateState="";
plateColor="";
count=0;
}
public CarPlate(String let, String num, String state, String color)
{
setPlate(let, num, state, color);
}
public String toString()
{
return ("Plate number "+plateLetter+" "+plateNumber+ " is a "
+plateState+" " + " plate that is "+plateColor+" in color.");
}
public String equals()
{
return plateColor;
}
public void setPlate(String let, String num, String state, String color)
{
plateLetter=let;
plateNumber=num;
plateState=state;
plateColor=color;
}
public String getPlateLetter(String let)
{
return plateLetter;
}
public String getPlateNumber(String num)
{
return plateNumber;
}
public String getPlateState(String state)
{
return plateState;
}
public String getPlateColor(String color)
{
return plateColor;
}
}
//The following is the class to test the program
public class CarPlateClient {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
CarPlate plate= new CarPlate();
CarPlate plate1=new CarPlate();
CarPlate plate2=new CarPlate();
plate.setPlate("YEN", "768", "Minnesota", "blue");
System.out.println(plate);
plate1.setPlate("RQY", "834", "North Dakota", "brown");
System.out.println(plate1);
plate2.setPlate("OND", "012", "Wisconsin", "orange");
System.out.println(plate2);
}
}
the spec does not appear to require these attributes
private String plateLetter;
private int plateTotal;
private int count;
private String plateLetter;
private int plateTotal;
private int count;
ASKER
Yeah. The count and plateTotal I had there because I was trying to count the number of objects and then have it print the total number of objects, which I still don't understand how to do with this sort of program. I just left those in there so you could see how I was attempting to count the objects.
As for the plateLetter. I have that in there for the license plate letters. I'm sure I could just have plateLetter and plateNumber as one, but at the time I didn't think of that and did them separate.
But the equals is nice to know. I've never really known how to use that and still don't completely understand it's purpose in this program.
But do you know how I could count the number of objects, assuming I don't know how many are there, and then print the total objects after the license plate descriptions?
As for the plateLetter. I have that in there for the license plate letters. I'm sure I could just have plateLetter and plateNumber as one, but at the time I didn't think of that and did them separate.
But the equals is nice to know. I've never really known how to use that and still don't completely understand it's purpose in this program.
But do you know how I could count the number of objects, assuming I don't know how many are there, and then print the total objects after the license plate descriptions?
counting would be done in main, not in your CarPlate class
equals() is used to compare two objects to see if they are equal
Theres no real need to count the plates in what you have as you hard code there creation ie. you know you have 3 plates
equals() is used to compare two objects to see if they are equal
Theres no real need to count the plates in what you have as you hard code there creation ie. you know you have 3 plates
Use a PrintWriter to write to a file
PrintWriter out = new PrintWriter(new FileWriter(path));
you can then use out to write to the file
out.println("3"); // count
out.println(plate);
out.println(plate2);
out.println(plate3);
out.close();
To read use a BufferedReader and its readLine() method
http://helpdesk.objects.com.au/java/how-do-i-read-a-text-file-line-by-line
PrintWriter out = new PrintWriter(new FileWriter(path));
you can then use out to write to the file
out.println("3"); // count
out.println(plate);
out.println(plate2);
out.println(plate3);
out.close();
To read use a BufferedReader and its readLine() method
http://helpdesk.objects.com.au/java/how-do-i-read-a-text-file-line-by-line
ASKER
Yes, I agree with you. I know that I have 3 plates, so I shouldn't need to count. But I am supposed to develop a program that can will display the number of plates if you didn't know how many there are. I've tried many different ways of doing this with using a counter, but can't seem to get it to count each object since I'm not using a loop. Is there a way of doing it?
ASKER
Sorry. That last post is supposed to be "...develop a program that can display the number of plates..."
you only need the count when reading them from the file dont you?
If you keep them in an array then the count is the size of the array
ASKER
Alright. I see what you guys are getting at. I'm actually supposed to write the objects to a file, then read them from the file as objects, then read the descriptions and the total objects. So I feel that I might have skipped a couple read or write steps. I'll have to try to get the bufferReader and printWriter involved in my program and see how things go from there.
sorry I think I misread your problem. Looks more like you are supposed to use an ObjectInputStream and ObjectOutputSTream to read/write the object to file.
ASKER
Alright. Well I don't know much about that, so I'll have to look into my book a little bit for that. I'll look over it and see what I can do.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
public boolean equals(Object o)
{
CarPlate plate = (CarPlate) o;
return plateState.equals(plate.pl
}