Results 1 to 13 of 13
Thread: Trouble with aggregation
- 05-01-2010, 05:12 PM #1
Member
- Join Date
- Feb 2010
- Posts
- 14
- Rep Power
- 0
Trouble with aggregation
Having a real difficulty understanding how to implement aggregation. I am currently working on a project that is called parking ticket. One class represents the parked car and defines the description of it. Then theres parking meter class that holds how much time is bought. Police officer class hold name and badge number. Then theres a parking ticket class that creates the ticket with the information of the other classes then outputs to the similator program.
So heres where I am stuck currently. The "ParkingTicketSimulator.java" is getting an error:
Heres the code for the Simulator which was provided to me by the instructor and does not need to be edited:ParkingTicketSimulator.java:24: cannot find symbol
symbol : method patrol(ParkedCar,ParkingMeter)
location: class PoliceOfficer
ParkingTicket ticket = officer.patrol(car, meter);
ParkingMeter class that I wrote. Having trouble aggregating the PoliceOfficer class into here (code in red are the areas in question).Java Code:public class ParkingTicketSimulator { public static void main(String[] args) { // Create a ParkedCar object. // The car was parked for 125 minutes. ParkedCar car = new ParkedCar("Volkswagen", "1972", "Red", "147RHZM", 125); // Create a ParkingMeter object. 60 minutes were purchased. ParkingMeter meter = new ParkingMeter(60); // Create a PoliceOfficer object. PoliceOfficer officer = new PoliceOfficer("Joe Friday", "4788"); [COLOR="Red"] // Let the officer patrol. ParkingTicket ticket = officer.patrol(car, meter);[/COLOR] // Did the officer issue a ticket? if (ticket != null) System.out.println(ticket); else System.out.println("No crimes committed!"); } }
If anybody can help it would be greatly appreciated, I am completely confused. Been over the chapter a number of times and it's just not clicking for me. Thanks.Java Code:public class ParkingTicket { //private String patrol, officer, issuer, badge; private int fine; private ParkedCar car, make, year, color, license; private ParkingMeter time; [COLOR="red"]private PoliceOfficer officer;[/COLOR] public ParkingTicket(ParkedCar car, ParkingMeter time) { this.make = make; this.year = year; this.color = color; this.license = license; this.time = time; [COLOR="red"]PoliceOfficer patrol = new PoliceOfficer(officer);[/COLOR] } public int getFine() { if(car.getParked() > time.getTimePurchased()) { fine = 25 + 10*(int)(Math.ceil((double)(car.getParked()-60)/60)); } else{ fine = 25; } return fine; } [COLOR="Red"] public PoliceOfficer getPoliceOfficer() { return new PoliceOfficer(officer); }[/COLOR] //To string public String toString() { //Create a string representing the car information String str = "\nMake: " + make + "\nYear: " + year + "\nColor: " + color + "\nLicense Plate: " + license + "\n\n Amount of Fine: " + fine + "Issued by:\n" + officer; //Return string return str; } }
-
Is this definitely from your instructor's code?
Java Code:// Let the officer patrol. ParkingTicket ticket = officer.patrol(car, meter);
If so, then it appears that the ParkingTicket is created in and returned from the PoliceOfficer's patrol(...) method. We don't see your PoliceOfficer code, so it would be interesting to see what you have written for the patrol method.
Also, your ParkingTicket class should not be creating new PoliceOfficer objects anywhere inside of it. It just doesn't make sense in real life for a ticket to create a police officer but rather the other way around, right? Your Java code should reflect this.
-
Edit: also, it is likely that your ParkingTicket constructor should take a PoliceOfficer object as one of its parameters -- another reason why there's no need to call "new PoliceOfficer()" in this class.
- 05-01-2010, 06:09 PM #4
Member
- Join Date
- Feb 2010
- Posts
- 14
- Rep Power
- 0
The ParkingTicketSimulator.java was absolutely from the instructor.
This is the code for the PoliceOfficer class
Reason why I created the police officer object in the ticket was to bring in the officer name and badge number. Is that wrong? I just was thinking that I had to create an object because of the line "ParkingTicket ticket = officer.patrol(car, meter);" in the ParkingTicketSimulator.Java Code:public class PoliceOfficer { private String officer, badge; public PoliceOfficer(String officer, String badge) { this.officer = officer; this.badge = badge; } //Copy constructor public PoliceOfficer(PoliceOfficer object2) { this.officer = object2.officer; this.badge = object2.badge; } public void set(String officer, String badge) { this.officer = officer; this.badge = badge; } public String toString() { String str = "Officer: " + officer + "\nBadge #: " + badge; //Return string return str; } }
-
Where's the code for the patrol method?
But the patrol method is being called from the officer PoliceOfficer object. This not a ParkingTicket constructor call (nor should it be). Again, you'll want to give your PoliceOfficer class a patrol method with the parameters that match car and meter. Then inside of this method create a new ParkingTicket object and return it from the method. I'd give the ParkingTicket constructor a PoliceOfficer, a Car, and a ParkingMeter parameter.Reason why I created the police officer object in the ticket was to bring in the officer name and badge number. Is that wrong? I just was thinking that I had to create an object because of the line "ParkingTicket ticket = officer.patrol(car, meter);" in the ParkingTicketSimulator.
- 05-02-2010, 05:42 PM #6
Member
- Join Date
- Feb 2010
- Posts
- 14
- Rep Power
- 0
You're absolutely right, not sure why I did that. I made those changes but am having a little trouble with the new method in the police officer class. Little confused on returning the new ticket, keeps showing an error there.
What I did with the ticket class:
Java Code:public class ParkingTicket { private int fine; private ParkedCar car; private ParkingMeter time; private PoliceOfficer officer; public ParkingTicket(ParkedCar car, ParkingMeter time, PoliceOfficer officer) { this.car = car; this.time = time; this.officer = officer; } public int getFine() { if(car.getParked() > time.getTimePurchased()) { fine = 25 + 10*(int)(Math.ceil((double)(car.getParked()-60)/60)); } else{ fine = 25; } return fine; } //To string public String toString() { //Create a string representing the car information String str = "Vehicle Information: " + car + "\nFine: " + fine + "\nIssued by: " + officer; //Return string return str; } }
The police officer class as of now:
Thanks!Java Code:public class PoliceOfficer { private String officer, badge; private ParkedCar car; private ParkingMeter time; public PoliceOfficer(String officer, String badge) { this.officer = officer; this.badge = badge; } //Copy constructor public PoliceOfficer(PoliceOfficer object2) { this.officer = object2.officer; this.badge = object2.badge; } public void set(String officer, String badge) { this.officer = officer; this.badge = badge; } [COLOR="Red"]public ParkingTicket patrol(ParkedCar car, ParkingMeter time) { this.car = car; this.time = time; return new ParkingTicket(ticket); }[/COLOR] public String toString() { String str = "Officer: " + officer + "\nBadge #: " + badge; //Return string return str; } }Last edited by gto400no1; 05-02-2010 at 05:58 PM.
- 05-02-2010, 06:00 PM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,375
- Blog Entries
- 7
- Rep Power
- 17
- 05-04-2010, 10:30 PM #8
Member
- Join Date
- Feb 2010
- Posts
- 14
- Rep Power
- 0
Alright so making good headway now. All the classes are compiling without error. However when I run the ParkingTicketSimulator.java program I am getting an error.
So heres the error:
So then I refer to the book and it says that the ParkingTicket.java class should report the amount of fine, then proceeds to describe how the fines should accumulate $25 first hour, then $10 after, ect. So I look at line 18 due to the error, everything looks ok to me, just a copy constructor (see method in red)Exception in thread "main" java.lang.NullPointerException
at ParkingTicket.<init>(ParkingTicket.java:18)
at PoliceOfficer.patrol(PoliceOfficer.java:33)
at ParkingTicketSimulator.main(ParkingTicketSimulator .java:24)
Java Code:public class ParkingTicket { private int fine; private ParkedCar car; private ParkingMeter time; private PoliceOfficer officer; public ParkingTicket(ParkedCar car, ParkingMeter time, PoliceOfficer officer) { this.car = car; this.time = time; this.officer = officer; } [COLOR="Red"] public ParkingTicket(ParkingTicket object2) { this.car = object2.car; this.time = object2.time; this.officer = object2.officer; }[/COLOR] public void set(ParkedCar car, ParkingMeter time, PoliceOfficer officer) { this.car = car; this.time = time; this.officer = officer; } public int getFine() { if(car.getParked() > time.getTimePurchased()) { fine = 25 + 10*(int)(Math.ceil((double)(car.getParked()-60)/60)); } else{ fine = 25; } return fine; } //To string public String toString() { //Create a string representing the car information String str = "Vehicle Information: " + car + "\nFine: " + fine + "\nIssued by: " + officer; //Return string return str; } }
Ok so maybe its the PoliceOfficer.java class, book says examine a parked car object and a parking meter object and determine whether the car's time has expired. So method passes the car and the time into the ticket class to check. Error shows that it is in line 33 where it returns the new ParkingTicket object (see method in red).
So I'm confused where this goes wrong, should the actual check if the time parked is less than the time bought within the police officer class? Could that be the reason for the error?Java Code:public class PoliceOfficer { private String officer, badge; private ParkedCar car; private ParkingMeter time; private ParkingTicket ticket; public PoliceOfficer(String officer, String badge) { this.officer = officer; this.badge = badge; } //Copy constructor public PoliceOfficer(PoliceOfficer object2) { this.officer = object2.officer; this.badge = object2.badge; } public void set(String officer, String badge) { this.officer = officer; this.badge = badge; } [COLOR="red"] public ParkingTicket patrol(ParkedCar car, ParkingMeter time) { this.car = car; this.time = time; return new ParkingTicket(ticket); }[/COLOR] public String toString() { String str = "Officer: " + officer + "\nBadge #: " + badge; //Return string return str; } }
Thanks in advanced.
-
Your problem is in PatrolOfficer here:
You have one of two constructors you can use to create a ParkingTicket object, and what you need to do is look carefully at both constructors and decide which one is the best to use in this method above. If you use the one that copies the ParkingTicket variable that is in PoliceOfficer, ask yourself where this variable has been initialized previously.Java Code:public ParkingTicket patrol(ParkedCar car, ParkingMeter time) { this.car = car; this.time = time; return new ParkingTicket(ticket); }
- 05-04-2010, 11:36 PM #10
Member
- Join Date
- Feb 2010
- Posts
- 14
- Rep Power
- 0
I'm really confused... you mean my issue is in how I'm returning the new ticket object?
- 05-05-2010, 12:12 AM #11
Member
- Join Date
- Feb 2010
- Posts
- 14
- Rep Power
- 0
I removed the:
this.car = car;
this.time = time;
Compiles fine, so I think I'm on the right track?
-
You didn't answer either of my questions above,
1) where is ParkingTicket ever initialized in the PoliceOfficer code? If it has not been initialized, how can it be anything but null? If you use a null object for the parameter of a copy constructor, what do you think is going to happen?
2) Again, which of the two ParkingTicket constructor do you think you should use here? and try to justify your answer.
Regarding "compiles fine", is your current problem a compile problem or a NullPointerException (NPE)? Does your changes solve the NPE problem?
Yes, the easy thing for me to do would be to just spoon feed you the answers, but I'm trying my darnedest to get you to think, because if you can truly understand why you're having this problem and it's solution, you'll not have it again. Again, think on these questions and try to answer them as best you can. Best of luck.Last edited by Fubarable; 05-05-2010 at 03:25 AM.
- 05-05-2010, 03:41 AM #13
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 4
Similar Threads
-
Aggregation !!
By Sary in forum New To JavaReplies: 13Last Post: 04-14-2010, 06:57 AM -
GUI Trouble
By rvgsd in forum New To JavaReplies: 2Last Post: 03-07-2010, 12:10 AM -
UML association,aggregation etc generator
By alexander.s in forum New To JavaReplies: 1Last Post: 09-18-2008, 06:27 PM -
having some trouble
By Unknown1369 in forum New To JavaReplies: 13Last Post: 07-21-2008, 11:52 PM -
inheritance and aggregation
By java_fun2007 in forum New To JavaReplies: 3Last Post: 12-13-2007, 01:36 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks