Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Trouble accessing variables from other classes

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    4
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default 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.

     
    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;
    	}
     
    }



    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;
    	}
     
    }



    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;
    	}
     
    }


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default 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?
    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:

      public ParkingMeter(int m)
        {
           set MinutesPurchased(m)
     
        }
    Last edited by javapenguin; February 4th, 2011 at 06:56 PM.

  3. The Following User Says Thank You to javapenguin For This Useful Post:

    Elementality (February 6th, 2011)

  4. #3
    Member DanBrown's Avatar
    Join Date
    Jan 2011
    Posts
    134
    My Mood
    Confused
    Thanks
    1
    Thanked 12 Times in 12 Posts

    Default 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
    Thanks and Regards
    Dan Brown

    Common Java Mistakes

  5. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Trouble accessing variables from other classes

    Quote Originally Posted by DanBrown View Post
    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

Similar Threads

  1. accessing
    By gcsekhar in forum Java Native Interface
    Replies: 1
    Last Post: December 14th, 2010, 10:04 PM
  2. Accessing Arrays all At Once HELP
    By scottgilliland2003 in forum Member Introductions
    Replies: 0
    Last Post: December 13th, 2010, 10:25 AM
  3. Changing Array variables from different classes
    By smellyhole85 in forum Collections and Generics
    Replies: 6
    Last Post: December 9th, 2010, 03:18 PM
  4. using variables from other classes in arrays :/
    By mozyman in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 30th, 2010, 11:26 PM
  5. State Variables interaction with outside classes?
    By Ace Coder in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 24th, 2010, 03:52 PM