Question

I had a question for my project:

I've got four class files, one stores info about a car's traits and how long it has been parked, another stores the data on how much time they purchased from the parking meter, the third is an officer class that depending on if they've exceeded their purchased time or not, either returns null or creates a parking ticket object through the use of the fourth class.

My problem though is the method to generate the ParkingTicket object is in the officer class, and the parking ticket object must take 3 arguments: the car object, the minutes past their purchased time, and the officer object. But I am not sure how to reference the officer object within its own class... I know the solution is probably trivial, but I just seem to be stumped.

I created a copy object of the officer like I was instructed, but I'm not sure how to even reference that within the officer class. (I had a bit of a stupid moment and tried to use the copy constructor's argument as the argument for the parking ticket object...)

Could anyone point me in the right direction maybe? I'll post my officer class code and error message below. Thanks for any and all help!

// This class holds information about the Police Officer
public class PoliceOfficer {


   private String name;                   // Officer's name
   private String badgeNumber;            // Officer's badge #
   private int minutes;


   // Initialize PoliceOfficer object
   public PoliceOfficer(String offName, String badgeNum)
   {
      name = offName;
      badgeNumber = badgeNum;
   }


   // Create Officer copy object
   public PoliceOfficer(PoliceOfficer officer)
   {
      name = officer.name;
      badgeNumber = officer.badgeNumber;
   }

   // Set Officer name and badge number fields
   public void setOfficer(String pName, String pNumber)
   {
      name = pName;
      badgeNumber = pNumber;
   }


   // Method that executes Officer's car inspection vs. Parking meter time -
   //  returns null if time parked is less than minutes on the meter
   public ParkingTicket patrol(ParkedCar car, ParkingMeter meter)
   {
      if (car.getMinutes() > meter.getMinutesPurchased())
      {
         minutes = car.getMinutes() - meter.getMinutesPurchased();
         ParkingTicket ticket = new ParkingTicket(car, officer, minutes);
         return ticket;
      }
      else
         return null;
   }



   // Generate toString method for officer's info to be displayed on the ticket
   public String toString()
   {
      return "Name: " + name +
             "\nBadge Number: " + badgeNumber;
   }


}



PoliceOfficer.java:46: error: cannot find symbol
         ParkingTicket ticket = new ParkingTicket(car, officer, minutes);
                                                       ^
  symbol:   variable officer
  location: class PoliceOfficer
5 errors

 ----jGRASP wedge2: exit code for process is 1.
 ----jGRASP: operation complete.
Was it helpful?

Solution

Simply use this to represent the current PoliceOfficer whose object you are in. For example:

// substitute *this* for the officer parameter
ParkingTicket ticket = new ParkingTicket(car, this, minutes); 

Note that this refers to the current object of the class you're in. Note that if you are in an inner class and are trying to reference the object of the outer class, you will need to specify which this you mean by pre-pending the class name. For instance, if you were inside of an inner class in the PoliceOfficer class and wished to reference the current officer, you would use PoliceOfficer.this.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top