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 7 of 7

Thread: Basic program which gets cost, adds tax, gets payment then calculates change.

  1. #1
    Junior Member
    Join Date
    Aug 2010
    Posts
    6
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Smile Basic program which gets cost, adds tax, gets payment then calculates change.

    Here is the code:


    import java.io.*;
     
    class Change
    {
    	double cost;
    	double tax;
    	double payment;
     
    	public void setCost(double x)
    	{
    		cost = x;
    	}
    	public double getCost()
    	{
    		return cost;
    	}
    	public void setTax(double x)
    	{
    		tax = x;
    	}
    	public double getTax()
    	{
    		return (cost+tax);
    	}
    	public void setPayment(double x)
    	{
    		payment = x;
    	}
    	public double getPayment()
    	{
    		return (payment-tax);
    	}
    	public static void main (String [] args) throws Exception
    	{
    		BufferedReader object = new BufferedReader (new InputStreamReader (System.in));
     
    		Change change = new Change();
     
    		System.out.println("Welcome to the Supermarket!");
    		System.out.println("");
    		System.out.println("How much is the total cost?");
    		System.out.println("Input here: ");
    		cost = Double.parseDouble (object.readLine());
     
    		change.setCost(cost);
    		System.out.println("");
    		System.out.println("The cost is: "+change.getCost());
    		System.out.println("");
     
    		tax = cost * 0.06;
    		System.out.println("");
    		System.out.println("The tax is: "+tax);
    		change.setTax(tax);
    		System.out.println("The total cost is: "+change.getTax());
    		System.out.println("");
     
    		System.out.println("How much will you pay?");
    		System.out.print("Input here:");
    		payment = Double.parseDouble(object.readLine());
    		System.out.println("");
     
    		change.setPayment(payment);
    		System.out.println("Your change is "+change.getPayment());
    		System.out.println("");
    		System.out.println("Have a nice day!");
    	}
    }




    But it has 8 errors of the same origin:


     
    Main.java:43: non-static variable cost cannot be referenced from a static context
    		cost = Double.parseDouble (object.readLine());
    		^
    Main.java:44: non-static variable cost cannot be referenced from a static context
    		change.setCost(cost);
    		               ^
    Main.java:48: non-static variable tax cannot be referenced from a static context
    		tax = cost * 0.06;
    		^
    Main.java:48: non-static variable cost cannot be referenced from a static context
    		tax = cost * 0.06;
    		      ^
    Main.java:50: non-static variable tax cannot be referenced from a static context
    		System.out.println("The tax is: "+tax);
    		                                  ^
    Main.java:51: non-static variable tax cannot be referenced from a static context
    		change.setTax(tax);
    		              ^
    Main.java:56: non-static variable payment cannot be referenced from a static context
    		payment = Double.parseDouble(object.readLine());
    		^
    Main.java:58: non-static variable payment cannot be referenced from a static context
    		change.setPayment(payment);
    		                  ^
    8 errors



    What do those errors mean? Can someone please explain and/or correct them? Thank you so much! ^___^
    Last edited by bibboorton; August 23rd, 2010 at 10:51 AM. Reason: Indention


  2. #2
    Member
    Join Date
    Jul 2010
    Posts
    45
    Thanks
    10
    Thanked 3 Times in 3 Posts

    Default Re: Basic program which gets cost, adds tax, gets payment then calculates change.

    tax, cost, and payment are instance variables. This means that every Change object created will have its own copy of these variables. If you include the static keyword in the declaration, these variables become shared by all Change objects.

    You can't just access tax directly like that, because which instance does it belong to? Try including an object reference, like:

    change.tax = change.cost * 0.06;

    This link may be helpful to you:

    Understanding Instance and Class Members (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

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

    bibboorton (August 24th, 2010)

  4. #3
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Basic program which gets cost, adds tax, gets payment then calculates change.

    This is an extremely common beginner error. You'll keep doing this until you get used to doing it.

    When you create the main, you said this:
    public static void main (String [] args) throws Exception
    Notice the word static in that declaration.

    Since your main has to be static, all of the methods and variables must ALSO be static.

    So when you create your methods, such as your setCost method, you have to say:
    public static void setCost(double x)
    And when you create your variables, you must say:
    static double cost;
    Notice the word static in both of those declarations?
    Doing this will stop that error from being thrown. It is really only necessary to do this in the main.

  5. The Following User Says Thank You to aussiemcgr For This Useful Post:

    bibboorton (August 24th, 2010)

  6. #4
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Re: Basic program which gets cost, adds tax, gets payment then calculates change.

    (Taken from bbr201's link)

    --A few things to know--

    Class variables are referenced by the class name itself, as in

    Bicycle.numberOfBicycles

    This makes it clear that they are class variables.


    Note: You can also refer to static fields with an object reference like

     myBike.numberOfBicycles

    but this is discouraged because it does not make it clear that they are class variables.


    Class methods cannot access instance variables or instance methods directly—they must use an object reference. Also, class methods cannot use the this keyword as there is no instance for this to refer to.
    Last edited by Brt93yoda; August 23rd, 2010 at 01:27 PM.

  7. The Following User Says Thank You to Brt93yoda For This Useful Post:

    bibboorton (August 25th, 2010)

  8. #5
    Junior Member
    Join Date
    Aug 2010
    Posts
    6
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Thumbs up Re: Basic program which gets cost, adds tax, gets payment then calculates change.

    Wow! Thanks a lot for the help, guys! That was quite enlightening!

    So to be clear, the class variable can be used by all methods, but the instance variable can only be used by the method it is declared in, unless with object reference? Is that right?

    Last edited by bibboorton; August 24th, 2010 at 10:04 AM.

  9. #6
    Member
    Join Date
    May 2010
    Posts
    36
    Thanks
    0
    Thanked 13 Times in 12 Posts

    Default Re: Basic program which gets cost, adds tax, gets payment then calculates change.

    Quote Originally Posted by bibboorton View Post
    So to be clear, the class variable can be used by all methods
    unless the access modifier allow this.

    Quote Originally Posted by bibboorton View Post
    but the instance variable can only be used by the method it is declared in, unless with object reference? Is that right?
    right. but also here the access depends on the access modifier the variable has been declared.

  10. The Following User Says Thank You to j2me64 For This Useful Post:

    bibboorton (August 25th, 2010)

  11. #7
    Junior Member
    Join Date
    Aug 2010
    Posts
    6
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Basic program which gets cost, adds tax, gets payment then calculates change.

    Great! Thanks, j2me64!

Similar Threads

  1. Check difference between no. of stops, calculate cost
    By JavaStudent23 in forum Java Theory & Questions
    Replies: 4
    Last Post: November 17th, 2009, 03:29 AM
  2. how to using button to change linechart
    By NARs in forum AWT / Java Swing
    Replies: 3
    Last Post: October 30th, 2009, 12:53 PM
  3. Basic Java Program Help
    By roaster in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 6th, 2009, 10:28 PM
  4. [SOLVED] Change the size of an image
    By subhvi in forum Algorithms & Recursion
    Replies: 4
    Last Post: August 23rd, 2009, 11:44 PM
  5. Change JFrame components problem
    By bruno88 in forum AWT / Java Swing
    Replies: 0
    Last Post: June 30th, 2009, 01:25 PM

Tags for this Thread