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.

Threaded View

  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


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