Trouble accessing variables from other classes
I'll try to keep this short.
I need to access ParkedCar.minutesParked and ParkingMeter.minutesPurchased from ParkingTicket.calculateFine() and I keep getting an error that the variable isn't visable. I understand why it's happening, the variables are set to private. But, I'm working off of a UML, so they have to stay that way.
Any suggestions would be great.
Code Java:
public class ParkedCar {
private String make;
private String model;
private String color;
private String licenseNumber;
private int minutesParked;
public ParkedCar(String mk, String mod, String col, String lic, int min)
{
make = mk;
model = mod;
color = col;
licenseNumber = lic;
minutesParked = min;
}
public ParkedCar(ParkedCar car2)
{
make = car2.make;
model = car2.model;
color = car2.color;
licenseNumber = car2.licenseNumber;
minutesParked = car2.minutesParked;
}
public String getMake() {
return make;
}
public void setMake(String make) {
this.make = make;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String getLicenseNumber() {
return licenseNumber;
}
public void setLicenseNumber(String licenseNumber) {
this.licenseNumber = licenseNumber;
}
public int getMinutesParked() {
return minutesParked;
}
public void setMinutesParked(int minutesParked) {
this.minutesParked = minutesParked;
}
public String toString()
{
String str = "Car Data:" +
"\nMake: " + make +
"\nModel: " + model +
"\nColor: " + color +
"\nLicense Number: " + licenseNumber +
"\nMinutes Parked: " + minutesParked;
return str;
}
}
Code Java:
public class ParkingMeter {
private int minutesPurchased;
public ParkingMeter(int m)
{
minutesPurchased = m;
}
public int getMinutesPurchased() {
return minutesPurchased;
}
public void setMinutesPurchased(int minutesPurchased) {
this.minutesPurchased = minutesPurchased;
}
}
Code Java:
public class ParkingTicket {
private ParkedCar car;
private PoliceOfficer officer;
private double fine;
private int minutes;
public double BASE_FINE = 25.0;
public double HOURLY_FINE = 10.0;
public ParkingTicket(ParkedCar aCar, PoliceOfficer anOfficer, int min)
{
car = new ParkedCar(aCar);
officer = new PoliceOfficer(anOfficer);
minutes = min;
}
public ParkingTicket(ParkingTicket ticket2)
{
car = ticket2.car;
officer = ticket2.officer;
minutes = ticket2.minutes;
}
//This is where I'm having issues
public void calculateFine()
{
if(ParkedCar.minutesParked - ParkingMeter.minutesPurchased <= 60)
fine = BASE_FINE;
else
fine = BASE_FINE + (HOURLY_FINE * ((ParkedCar.minutesParked - ParkingMeter.minutesPurchased) / 60 ));
}
public ParkedCar getCar() {
return car;
}
public void setCar(ParkedCar car) {
this.car = car;
}
public PoliceOfficer getOfficer() {
return officer;
}
public void setOfficer(PoliceOfficer officer) {
this.officer = officer;
}
public double getFine() {
return fine;
}
public void setFine(double fine) {
this.fine = fine;
}
public int getMinutes() {
return minutes;
}
public void setMinutes(int minutes) {
this.minutes = minutes;
}
public double getBASE_FINE() {
return BASE_FINE;
}
public void setBASE_FINE(double bASE_FINE) {
BASE_FINE = bASE_FINE;
}
public double getHOURLY_FINE() {
return HOURLY_FINE;
}
public void setHOURLY_FINE(double hOURLY_FINE) {
HOURLY_FINE = hOURLY_FINE;
}
public String toString()
{
String str = "Minutes Parked: " + minutes;
return str;
}
}
Re: Trouble accessing variables from other classes
Well...you could always replace
licenseNumber = car2.licenseNumber;
minutesParked = car2.minutesParked;
with
licenseNumber = car2.getLicenseNumbe()r;
minutesParked = car2.getMinutesParked();
I also might add that as you never call any of your set methods anywhere...they aren't really doing much.
How about this?
Code java:
public ParkedCar(ParkedCar car2)
{
this.make = car2.getMake();
this. model = car2.getModel();
this.color = car2.getColor()r;
this. licenseNumber = car2.getLicenseNumber();
this.minutesParked = car2.getMinutesParked();
// those lines above set the value in your current car object to the values returned by the get methods of your ParkedCar class
}
Also, if you are going to use your set methods, either call them in other classes to set them or use your constructor of the class itself...like this:
Code java:
public ParkingMeter(int m)
{
set MinutesPurchased(m)
}
Re: Trouble accessing variables from other classes
i am confused about one thing you had developed getter and setter method , then why didn't you use them.
I have one question in mind that , do you really know why we create getter and setter method.
Please don't mind , but go through basic java tutorials
Access Modifiers
Re: Trouble accessing variables from other classes
Quote:
Originally Posted by
DanBrown
do you really know why we create getter and setter method.
Please don't mind , but go through basic java tutorials
Access Modifiers
I agree. Understanding the reasoning is extremely important. Just my opinion, but this is a situation where the java tutorials do a very poor job at explaining the reasoning. For example, not once is the word encapsulation used on that tutorial page. Again, just my .02 :)