0

I have four classes: ParkedCar, ParkingMeter, ParkingTicket, and PoliceOfficer. The PoliceOfficer class should "issue a ticket", ie create a new ParkingTicket object, if a car has been parked longer than it should be. A ParkingTicket object has, as member objects, a ParkedCar object and PoliceOfficer object (basically objects containing info for what car is getting the ticket, and the issuing officer). Below are my constructors for my ParkingTicket class, I'm trying to use the first one in creating my ParkingTicket.

From ParkingTicket class:

public ParkingTicket(ParkedCar parkedCarObj, PoliceOfficer officerObj){
    this.car = new ParkedCar(parkedCarObj);
    this.officer = new PoliceOfficer(officerObj);
}

//Copy constructor, makes new instance a copy of object passed as argument
public ParkingTicket(ParkingTicket obj2){
    this.car = new ParkedCar(obj2.car);
    this.officer = new PoliceOfficer(obj2.officer);
}

My method for issuing a ticket in the PoliceOfficer class is below. If I need to issue a ticket, I try to pass the ParkedObject and the self PoliceOfficer object (by using this) to the ParkingTicket constructor, as below.
From PoliceOfficer class:

public boolean issueTicket(ParkedCar car, ParkingMeter meter){
        boolean expired = false;

        if (car.getMinsParked() > meter.getMinsPurchased()){
            expired = true;
            ParkingTicket ticket = new ParkingTicket(car, this); //Compiler error
        }

        return expired;
    }

However, the line where I instantiate the new ParkingTicket throws a compiler error. The message is:

constructor ParkingTicket in class ParkingTicket cannot be applied to given types;

required: ParkedCar

found: ParkedCar,PoliceOfficer

reason: actual and formal argument lists differ in length

I'm very confused because it should be invoking the first overloaded constructor, which takes a ParkedCar and a PoliceOfficer as arguments. I'm not certain why it's saying it should only take one argument of ParkedCar. If I only pass a ParkedCar object as an argument, it compiles fine, but I know this is incorrect because I've not passed the needed PoliceOfficer info to the ParkingTicket object.

Any ideas? Appreciate any help.

5
  • 2
    silly question - did you recompile your code to make sure the .class files are up to date? the error message says it requires a ParkedCar parameter only. The single argument constructor you show had a different type so it's not an overload issue. Commented Feb 1, 2018 at 4:14
  • it is making so much confusion, on what are you trying to achieve Commented Feb 1, 2018 at 4:19
  • In the constructor for ParkingTicket, you pass instances of ParkedCar and PoliceOfficer as parameters. Why are you creating new instances of ParkedCar and PoliceOfficer when assigning them to this.car and this.officer, with the passed instances? It sounds like "assign a car to this by creating a car with an identical car". Other than that, try do as dkatzel is suggesting, try rebuild your project. Commented Feb 1, 2018 at 4:21
  • @dkatzel, can't be a silly question if it turns out to be the right answer. Still getting used to the Netbeans IDE, thanks for the suggestion, it now correctly takes both objects as arguments. Commented Feb 1, 2018 at 4:32
  • @eli - I'm learning Java and I'm following the examples in Starting out with Java by Gaddis. My understanding is that it's better to create a deep copy of the object when it's a member of a class, rather than reference the same one, to prevent unwanted access to changing the object by outside code. Am I being led astray? Please point me in the right direction or to a better resource, if you feel I am, it would be a big help. Commented Feb 1, 2018 at 4:38

1 Answer 1

1

I'll put the answer down here since I apparently answered it in the comments.

The error message says it requires a ParkedCar parameter only. The single argument constructor you show had a different type so it's not an overload issue.

Since the expected parameters were a partial list of what you expected it to be it is probably a stale .class file. Recompile the classes and it should work.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.