Basic program which gets cost, adds tax, gets payment then calculates change.
Here is the code:
Code java:
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:
Code java:
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! ^___^
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)
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:
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:
Code java:
public static void setCost(double x)
And when you create your variables, you must say:
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.
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
This makes it clear that they are class variables.
Note: You can also refer to static fields with an object reference like
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.
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! :)) :D
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?
:D :)>-
Re: Basic program which gets cost, adds tax, gets payment then calculates change.
Quote:
Originally Posted by
bibboorton
So to be clear, the class variable can be used by all methods
unless the access modifier allow this.
Quote:
Originally Posted by
bibboorton
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.
Re: Basic program which gets cost, adds tax, gets payment then calculates change.
Great! Thanks, j2me64! :-bd