4 Replies - 4062 Views - Last Post: 06 October 2012 - 06:38 AM Rate Topic: -----

#1 MeeechiganMan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 28-June 12

No suitable constructor found for Parking Ticket Simulator

Posted 06 October 2012 - 06:25 AM

In this problem I am doing I have to create a Parking Ticket Simulator. I seem to have all the necessary components in my program but when I compile my TestDemo file, I continue to get two errors.

"no suitable constructor found & cannot find symbol."

Any advice would be greatly appreciated, thank you again.

The guide lines to my problem are:

The ParkedCar Class: This class should simulate a parked car. The class's responsibilities are as follows:

- To know the car's make, model, color, license number, and the number of minutes that the car has been parked.

The ParkingMeter Class: This class should simulate a parking meter. The class's only responsibility is as follows:

- To know the number of minutes of parking time that has been purchased.

The ParkingTicket Class: This class should simulate a parking ticket. The class's responsibilities are as follows:

- To report the make, model, color, and license number of the illegally parked car

- To report the amount of the fine, which is $25 for the first hour or part of an hour that the car is illegally parked, plus $10 for every additional hour or part hour that the car is illegally parked, plus $l0 for every additional hour or part of an hour that the car is illegally parked

- To report the name and badge number of the police officer issuing the ticket

The PoliceOfficer Class: This class should simulate a police officer inspecting parked cars. The class's responsibilities are as follows:

- To know the police officer's name and badge number

- To examine a ParkedCar object and a ParkingMeter object, and determine whether the car's time has expired

- To issue a parking ticket (generate a ParkingTicket object) if the car's time has expired

Here is the code I have for the 4 classes:

public class ParkedCar
{
	private String make; 
	private String model; 
	private String color; 
	private String licenseNumber; 
	public int minutes; 
	
	/**
	This constructor initializes the make, model, color, licenseNumber, and minutes fields.
	@param pcmake The parked car's make.
	@param pcmodel The parked car's model.
	@param pccolor The parked car's color.
	@param pclnumber The parked car's license plate number.
	@param pcminutes The number of minutes that the car has been parked.
	*/
	
	public ParkedCar(String pcmake, String pcmodel, String pccolor, String pclnumber, int pcminutes)
	{
		make = pcmake;
		model = pcmodel;
		color = pccolor;
		licenseNumber = pclnumber;
		minutes = pcminutes;
	}
	
	/** The set method sets a value for each field.
	@param pcmake The parked car's make.
	@param pcmodel The parked car's model.
	@param pccolor The parked car's color.
	@param pclnumber The parked car's license plate number.
	@param pcminutes The number of minutes that the car has been parked.
	*/
	
	public void set(String pcmake, String pcmodel, String pccolor, String pclnumber, int pcminutes)
	{
		make = pcmake;
		model = pcmodel;
		color = pccolor;
		licenseNumber = pclnumber;
		minutes = pcminutes;
	}
	
	/** The copy constructor initializes the object
	as a copy of another ParkedCar object.
	@param object2 The object to copy.
	*/
	
	public ParkedCar(ParkedCar object2)
	{
		make = object2.make;
		model = object2.model;
		color = object2.color;
		licenseNumber = object2.licenseNumber;
		minutes = object2.minutes;
	}
	
	/**
	toString method
	@return A string containing the parked car information.
	*/
	
	public String toString()
	{
	
		String str = "Parked car's make.....................: " + make +
		"\nParked car's model..................: " + model +
		"\nParked car's color..................: " + color +
		"\nParked car's license plate number...: " + licenseNumber +
		"\nMinutes that the car has been parked: " + minutes;
		
		return str;
	}
}






public class ParkingMeter
{
	public int minutesPurchased; // The number of minutes of parking time that has been purchased
	
	/**
	This constructor initializes the minutesPurchased field.
	@param Minutes of parking time purchased.
	*/
	
	public ParkingMeter(int pmmpurchased)
	{
		minutesPurchased = pmmpurchased;
	}
	
	/**
	The set method sets a value for each field.
	@param Minutes of parking time purchased.
	*/
	
	public void set(int pmmpurchased)
	{
		minutesPurchased = pmmpurchased;
	}
	
	/** The copy constructor initializes the object
	as a copy of another ParkingMeter object.
	@param object2 The object to copy.
	*/
	
	public ParkingMeter(ParkingMeter object2)
	{
		minutesPurchased = object2.minutesPurchased;
	}
	
	/**
	getMinutesPurchased method
	@return The number of minutes of parking time that has been purchased.
	*/
	
	public int getMintuesPurchased()
	{
		return minutesPurchased;
	}
}







public class ParkingTicket
{
	private double fine; 
	private ParkedCar parkedcar; 
	private ParkingMeter parkingmeter;
	private PoliceOfficer policeofficer; 
	
	/**
	This constructor initializes the parkedcar, parkingmeter, and policeofficer fields.
	@param parkedcar
	@param parkingmeter
	@param policeofficer
	*/
	
	public ParkingTicket(double fine, ParkedCar pcar, ParkingMeter pmeter)
	{
	// Create a new ParkedCar object, passing
	// pcar as an argument to the copy constructor.
	parkedcar = new ParkedCar(pcar);
	
	// Create a new ParkingMeter object, passing
	// pmeter as an argument to the copy constructor.
	parkingmeter = new ParkingMeter(pmeter);
	
	// Create a new PoliceOfficer object, passing
	// pofficer as an argument to the copy constructor.
	//policeofficer = new PoliceOfficer(pofficer);
	
	this.fine = fine;
	}
	
	
	
	/**
	toString method
	@return A string containing the parking ticket information.
	*/
	
	public String toString()
	{
		String str = "Illegally parked car info: " + parkedcar +
		"\nParking meter info:" + parkingmeter +
		"\nAmount of the fine...: " + fine;
		//"\nPolice officer info: " + policeofficer;
		
		return str;
	}
}






public class PoliceOfficer
{
	private String name; 
	private int badgeNumber; 
	private ParkedCar parkedcar; 
	private ParkingMeter parkingmeter; 
	private ParkingTicket parkingticket; 
	public double fine = 0; 
	
	/**
	This constructor initializes the name and badgeNumber fields.
	@param poname The police officer's name.
	@param pobnumber The police officer's badge number.
	*/
	
	public PoliceOfficer(String poname, int pobnumber, ParkedCar pcar, ParkingMeter pmeter)
	{
		name = poname;
		badgeNumber = pobnumber;
		
		// Create a new ParkedCar object, passing
		// pcar as an argument to the copy constructor.
		parkedcar = new ParkedCar(pcar);
		
		// Create a new ParkingMeter object, passing
		// pmeter as an argument to the copy constructor.
		parkingmeter = new ParkingMeter(pmeter);
	}
	
	/** The set method sets a value for each field.
	@param poname The police officer's name.
	@param pobnumber The police officer's badge number.
	*/
	
	public void set(String poname, int pobnumber)
	{
		name = poname;
		badgeNumber = pobnumber;
	}
	
	/** The copy constructor initializes the object
	as a copy of another PoliceOfficer object.
	@param object2 The object to copy.
	*/
	
	public PoliceOfficer(PoliceOfficer object2)
	{
		name = object2.name;
		badgeNumber = object2.badgeNumber;
		
		
		// Create a new ParkedCar object, passing
		// pcar as an argument to the copy constructor.
		//parkedcar = new ParkedCar(pcar);
		
		//Create a new ParkedCar object.
		//parkedcar = new ParkedCar(pcar);
		
		//Create a ParkingMeter object.
		//parkingmeter = new ParkingMeter(pmeter);
	}
	
	public ParkedCar getParkedCar()
	{
		// Return a copy of the ParkedCar object.
		return new ParkedCar(parkedcar);
	}
	
	public ParkingMeter getParkingMeter()
	{
		// Return a copy of the ParkingMeter object.
		return new ParkingMeter(parkingmeter);
	}
	
	public void inspectParkedCar()
	{
		// Determine whether the car is illegally parked.
		if (parkedcar.minutes > parkingmeter.minutesPurchased)
		// Determine the amount of the fine.
		if (parkedcar.minutes - parkingmeter.minutesPurchased <= 60)
		fine = 25;
		else
		fine = 25 + (10 * ((parkedcar.minutes - parkingmeter.minutesPurchased) / 60));
		
		// Create a ParkingTicket object, passing
		// 3 objects as arguments to the constructor.
		parkingticket = new ParkingTicket(fine, parkedcar, parkingmeter);
	}
}






And the Demo for running the code.
public class TestParkingTicket
{
   public static void main(String[] args)
   {
		// This 1986 Ford was parked for 85 minutes
      ParkedCar car = new ParkedCar("Ford", "1986", "Grey",
                                    "BAC4455", 85);
      
      // The meter had 30 minutes on it
      ParkingMeter meter = new ParkingMeter(30);
      
      PoliceOfficer officer = new PoliceOfficer("John Doe",
                                                "9932");
                                                
      // The officer is on patrol and sees the parked car
		// and the meter.  
		// If the car was parked for a longer time than was
		// on the meter, then the "patrol" method will 
		// issue a ticket. 
		// If there was enough time on the meter to cover the
		// time the car was parked, then no ticket is 
		// issued (i.e., a null reference is returned).
      ParkingTicket ticket = officer.patrol(car, meter);
      
      // If no ticket was issued, then "ticket" is null
      if (ticket == null)
		   System.out.println("No crimes committed!");

      else
		{
			System.out.println("TICKET ISSUED: ");
         System.out.println(ticket);
		}
   }
}



Is This A Good Question/Topic? 0
  • +

Replies To: No suitable constructor found for Parking Ticket Simulator

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2205
  • View blog
  • Posts: 5,239
  • Joined: 10-September 10

Re: No suitable constructor found for Parking Ticket Simulator

Posted 06 October 2012 - 06:30 AM

It helps us isolate the error to the code if you post the exact error, copied and pasted as it appears to you.
Was This Post Helpful? 0
  • +
  • -

#3 MeeechiganMan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 28-June 12

Re: No suitable constructor found for Parking Ticket Simulator

Posted 06 October 2012 - 06:33 AM

View PostGregBrannon, on 06 October 2012 - 06:30 AM, said:

It helps us isolate the error to the code if you post the exact error, copied and pasted as it appears to you.



This is what I get when I compile my Test Demo file.

Quote

----jGRASP exec: javac -g TestParkingTicket.java

TestParkingTicket.java:12: error: no suitable constructor found for PoliceOfficer(String,String)
PoliceOfficer officer = new PoliceOfficer("John Doe",
^
constructor PoliceOfficer.PoliceOfficer(PoliceOfficer) is not applicable
(actual and formal argument lists differ in length)
constructor PoliceOfficer.PoliceOfficer(String,int,ParkedCar,ParkingMeter) is not applicable
(actual and formal argument lists differ in length)
TestParkingTicket.java:23: error: cannot find symbol
ParkingTicket ticket = officer.patrol(car, meter);
^
symbol: method patrol(ParkedCar,ParkingMeter)
location: variable officer of type PoliceOfficer
2 errors

----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.

Was This Post Helpful? 0
  • +
  • -

#4 Kakerergodt  Icon User is offline

  • D.I.C Head

Reputation: 87
  • View blog
  • Posts: 201
  • Joined: 01-May 12

Re: No suitable constructor found for Parking Ticket Simulator

Posted 06 October 2012 - 06:34 AM

You should really have posted the stack trace, but nonetheless the error:
"no suitable constructor found"
implies that you have tried to create a new object and not passed the correct arguments, let's say you have a class called fruit with constructor "Fruit(String flavor)" and you tried to create a new fruit with the code "new Fruit(flavor, taste, weight)" ie. the wrong type/number of parameters.
"cannot find symbol" often means that you use a variable that hasn't been initialized.
Was This Post Helpful? 0
  • +
  • -

#5 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2205
  • View blog
  • Posts: 5,239
  • Joined: 10-September 10

Re: No suitable constructor found for Parking Ticket Simulator

Posted 06 October 2012 - 06:38 AM

So the first error (and stack trace) goes to great lengths to tell you that there is no constructor that meets the requirements of line 12 in TestParkingClass:

PoliceOfficer officer = new PoliceOfficer("John Doe", "9932");

That statement requires a constructor that accepts two String arguments. So look at your PoliceOfficer class and create an applicable constructor or modify line 12 to use one of the existing constructors. Perhaps "9932" was supposed to be a number (an int) instead of a String.

This post has been edited by GregBrannon: 06 October 2012 - 06:39 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1