I have the following programming challenge to complete, and am stuck. I know what i have to do, which is to get information from the other three classes into the parking ticket class, and then print the parking ticket using an if statement if the time has expired. I am having difficulty understanding how to link the classes together... after the instructions, I have posted my four classes that I have created thus far.

Design a set of classes that work together to simlulate a police officer issuing a parking ticket. The classes to design are:

  • ParkedCar class: simulates a parked car
  • This class knows the car's make, model, color, license number, and the number of minutes that the car has been parked.

  • ParkingMeter Class: simulates a parking meter.

  • This classes only responsibility is to know the number of minutes of parking time that has been purchased.

-ParkingTicket class: Simulates a parking ticket
Responsibilities:
- To report make, model, color, and license number of the illegally parked car.
- To report the amount of the fine which is $25 dollars for the first hour or part of the hour that the car is illegally parked, plus $10 for every additional hour or part of an hour that the car is parked.
- To report the name and badge number of the police officer issuing the ticket.
-PoliceOfficer class: Simulates a police officer
Responsibilities:
- To know the police officer's name and badge number
- To examine a parked car 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.

Write a program to test the classes as well.

import java.util.Scanner;



public class parkedCar


{
public static void main(String[] args)
{


String make;
String model;
String color;
double licenseNumber;
double minutesParked;


Scanner keyboard = new Scanner (System.in);



System.out.print("Enter the make of the car: ");
make = keyboard.nextLine();


System.out.print("Enter the model of the car: ");
model = keyboard.nextLine();


System.out.print("Enter the color of the car: ");
color = keyboard.nextLine();


System.out.print("Enter the lisence number of the car: ");
licenseNumber = keyboard.nextDouble();


System.out.print("Enter the minutes that the car was parked ");
minutesParked = keyboard.nextDouble();
}


}



import java.util.Scanner;



public class parkingMeter
{


public static void main(String[] args)
{


double minutesPurchased;


Scanner keyboard = new Scanner (System.in);


System.out.print("Enter the amount of minutes purchased: ");
minutesPurchased = keyboard.nextDouble();



}


}


class parkingTicket // I know this class is wrong, it is where I am confused... I attempted to
// use a similar class that was used in a previous program i think it
//confused me more...


{
private parkedCar questionedCar = null;
private policeOfficer officerOnDuty =null;


public parkingTicket ()
{
questionedCar = new parkedCar();
officerOnDuty = new policeOfficer();
}


public parkingTicket (parkedCar qCar, policeOfficer oOd)
{
questionedCar = qCar;
officerOnDuty = oOd;
}


public void set(parkedCar qCar, policeOfficer oOd)
{
questionedCar = qCar;
officerOnDuty = oOd;
}


public parkedCar getCar()
{
return questionedCar;
}


public policeOfficer getOfficer()
{
return officerOnDuty;
}


public String toString()
{
String str = "Questioned Car: " + questionedCar
+ "\nOfficer: " + officerOnDuty;


return str;
}



}



import java.util.Scanner;


public class policeOfficer
{
public static void main (String [] args)
{


String officerName;
double badgeNumber;



Scanner keyboard = new Scanner(System.in);


System.out.print("Enter the officer's name: ");
officerName = keyboard.nextLine();


System.out.print("Enter the officer's badge number: ");
badgeNumber = keyboard.nextDouble();


}


}

Recommended Answers

All 6 Replies

Hi there, well your approach is actually one of the best ways to pass data between classes. One usually does this via the class constructor just as you have in the parking ticket class.

The class that handles the parking tickets would require both objects to achieve its goal. What you're missing is what is usually (I think usually) referred to as a driver class. This class would be responsible for getting the input from the keyboard and supplying the objects of the appropriate classes with data.

So the design of the parking meter class and parked car class is somewhat incorrect. These classes should only have attributes that store the required information using get and set methods. For example, setMinutesPurchased(int min) and getMinutesPurchased(). You could also store this information via the class constructor but is it good practise to include the get and set methods.

So once you have the three proper classes whose job is to only store data and process it, then you create the driver class that actually creates the objects from the classes and populates the objects with data from the user (i.e. keyboard). I hope this makes sense?

And if you read the specifications you'll see that my recommendations fit. Oh and btw, please begin class names with a capital letter as per the convention for Java programming. A Java programmer that uses the Java standards is a happy programmer :icon_wink: it just makes it easier for other people to read your code... and possible it will be easier for you to read your code.

Oopsy, didn't see your reply there Ezzaral. I agree with you! Only stuff up the main method when you're experimenting on small pieces of code.... only for the brave at heart hehe

okay thank you should the first two classes look more like this....

import java.util.Scanner;



public class parkedCar


{


private String make;
private String model;
private String color;
private double licenseNumber;
private double minutesParked;


public parkedCar(String mk, String mod, String col, double ln, double mp)
{
make = mk;
model = mod;
color = col;
licenseNumber = ln;
minutesParked = mp;
}



public void set(String mk, String mod, String col, double ln, double mp)
{
make = mk;
model = mod;
color = col;
licenseNumber = ln;
minutesParked = mp;
}


public String toString()
{
String str = "Make: " + make
+ "\nModel: " + model
+ "\nColor: " + color
+ "\nLicense Number: " + licenseNumber
+ "\nMinutes Parked: " + minutesParked;


return str;



}


}


import java.util.Scanner;



public class parkingMeter
{
private double minutesPurchased;



public parkingMeter(double mp)
{
minutesPurchased = mp;
}


public void set(double mp)
{
minutesPurchased = mp;


}



public String toString()
{
String str = "Minutes purchased: " + minutesPurchased;


return str;
}
}
commented: No code tags, ouch -2

You should have a set and get method for each of the properties in the class. That is how your other classes will interact with an instance of that class.

I agree with Ezzaral. Since you've declared the class attributes (or class variables) as being private, there would be no way for yo to access the values stored in those variables outside of your class. Thus, by creating a get method, you allow the values to be 'gotten,' i.e. retrieved. So for example, you'd need a getMake() method that simply returns the private variable make.

You've improved which is what this is all about. I hope you've completed your work. Sorry for the late reply... for some reason I wasn't subscribed to this thread.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.