?
Solved

How can I create a Java program that creates license plate objects and then reads and writes them?

Posted on 2009-04-29
13
Medium Priority
?
1,694 Views
Last Modified: 2013-11-23
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.

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);
        
    }
 
}

Open in new window

0
Comment
Question by:mk9000
  • 7
  • 5
13 Comments
 
LVL 92

Expert Comment

by:objects
ID: 24266829
equals should be like:

    public boolean equals(Object o)
    {
       CarPlate plate = (CarPlate) o;
       return plateState.equals(plate.plateState) && plateNumber.equals(plate.plateNumber);
    }

0
 
LVL 92

Expert Comment

by:objects
ID: 24266830
the spec does not appear to require these attributes

    private String plateLetter;
    private int plateTotal;
    private int count;

0
 

Author Comment

by:mk9000
ID: 24266934
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?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 92

Expert Comment

by:objects
ID: 24266957
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

0
 
LVL 92

Expert Comment

by:objects
ID: 24266987
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

0
 

Author Comment

by:mk9000
ID: 24267014
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?
0
 

Author Comment

by:mk9000
ID: 24267019
Sorry.  That last post is supposed to be "...develop a program that can display the number of plates..."
0
 
LVL 92

Expert Comment

by:objects
ID: 24267030
you only need the count when reading them from the file dont you?

0
 
LVL 92

Expert Comment

by:objects
ID: 24267038
If you keep them in an array then the count is the size of the array
0
 

Author Comment

by:mk9000
ID: 24267084
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.
0
 
LVL 92

Expert Comment

by:objects
ID: 24267105
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.

0
 

Author Comment

by:mk9000
ID: 24267160
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.  
0
 
LVL 3

Accepted Solution

by:
hazgoduk earned 2000 total points
ID: 24337872

Vector<CarPlate> plates = new Vector();
 
plates.add(new CarPlate(....));
plates.add(new CarPlate(....));
plates.add(new CarPlate(....));
plates.add(new CarPlate(....));
 
 
System.out.println("There are " + plates.size() + " plate" + (plates.size() == 1 ? "" : "s"));

Open in new window

0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month17 days, 4 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question