Results 1 to 13 of 13
  1. #1
    gto400no1 is offline Member
    Join Date
    Feb 2010
    Posts
    14
    Rep Power
    0

    Default 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:
    ParkingTicketSimulator.java:24: cannot find symbol
    symbol : method patrol(ParkedCar,ParkingMeter)
    location: class PoliceOfficer
    ParkingTicket ticket = officer.patrol(car, meter);
    Heres the code for the Simulator which was provided to me by the instructor and does not need to be edited:
    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!");
    	}
    }
    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 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;
    	}
    
    }
    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.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  4. #4
    gto400no1 is offline Member
    Join Date
    Feb 2010
    Posts
    14
    Rep Power
    0

    Default

    The ParkingTicketSimulator.java was absolutely from the instructor.

    This is the code for the PoliceOfficer class
    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;
    	}
    
    }
    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.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by gto400no1 View Post
    The ParkingTicketSimulator.java was absolutely from the instructor.

    This is the code for the PoliceOfficer class
    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?


    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.
    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.

  6. #6
    gto400no1 is offline Member
    Join Date
    Feb 2010
    Posts
    14
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    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.
    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:
    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;
    	}
    
    }
    Thanks!
    Last edited by gto400no1; 05-02-2010 at 05:58 PM.

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,352
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by gto400no1 View Post
    Java Code:
    	[COLOR="Red"]public ParkingTicket patrol(ParkedCar car, ParkingMeter time)
    	{
    		this.car = car;
    		this.time = time;
    		
    		return new ParkingTicket(ticket);
    	}[/COLOR]
    Your ParkingTicket class doesn't have a constructor that takes (another?) ticket as a parameter.

    kind regards,

    Jos

  8. #8
    gto400no1 is offline Member
    Join Date
    Feb 2010
    Posts
    14
    Rep Power
    0

    Default

    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:
    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)
    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)
    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).
    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;
    	}
    
    }
    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?

    Thanks in advanced.

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Your problem is in PatrolOfficer here:

    Java Code:
    	public ParkingTicket patrol(ParkedCar car, ParkingMeter time)
    	{
    		this.car = car;
    		this.time = time;
    		
    		return new ParkingTicket(ticket);
    	}
    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.

  10. #10
    gto400no1 is offline Member
    Join Date
    Feb 2010
    Posts
    14
    Rep Power
    0

    Default

    I'm really confused... you mean my issue is in how I'm returning the new ticket object?

  11. #11
    gto400no1 is offline Member
    Join Date
    Feb 2010
    Posts
    14
    Rep Power
    0

    Default

    I removed the:
    this.car = car;
    this.time = time;

    Compiles fine, so I think I'm on the right track?

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by gto400no1 View Post
    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.

  13. #13
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    Maybe he's hinting that if one ParkingTicket constructor doesn't work because you're passing a null object into it -- perhaps it's that you're using the wrong ParkingTicket constructor. Hmmm... ;)

Similar Threads

  1. Aggregation !!
    By Sary in forum New To Java
    Replies: 13
    Last Post: 04-14-2010, 06:57 AM
  2. GUI Trouble
    By rvgsd in forum New To Java
    Replies: 2
    Last Post: 03-07-2010, 12:10 AM
  3. UML association,aggregation etc generator
    By alexander.s in forum New To Java
    Replies: 1
    Last Post: 09-18-2008, 06:27 PM
  4. having some trouble
    By Unknown1369 in forum New To Java
    Replies: 13
    Last Post: 07-21-2008, 11:52 PM
  5. inheritance and aggregation
    By java_fun2007 in forum New To Java
    Replies: 3
    Last Post: 12-13-2007, 01:36 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •