
Patrick B. answered 10/31/20
Math and computer tutor/teacher
First I changed the class name to SalesTax
just to make myself for comfortable.
COMPILER ERRORS:
(1)
The sales tax constant MUST be declared inside
the class, not in main() as
public static final double TAX = 0.06;
As a result, it must be referenced as this.TAX if you're in the class
or SalesTax.TAX if you're outside the class
(2) String data type begins with a Captial letter S...
String costString;
(3) cost = Double.parsedouble(coststring); <-- declared as costString,
so you got to STICK WITH IT!!!! I recommend camelCase notation
(4) Double.parseDouble(costString)
(5) THe algebraic expression must be inside parenthesis
so the compiler knows take the resulting value
and convert it to string...
You had cost + cost * TAX =
cost * (1 + TAX) <-- DISTRIBUTIVE PROPERTY FROM ALGEBRA IN ACTION!!!
how about that?!?!???!!
System.out.println("With " + TAX * 100 +
"% tax, purchase is $" + (cost*(1 + SalesTax.TAX)));
At this point I was able to get the code into a compile-able state.
It did work, as I input 20 and got the correct result of 21.20
you may want to round the result to and output only 2 digits so
that you don't see all the garbage in the memory buffer
here's the finished product:
================================================================
import java.util.*;
public class SalesTax
{
public static final double TAX = 0.06;
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
String costString;
double cost;
System.out.println("Enter price of item you are buying");
costString = input.next();
cost = Double.parseDouble(costString);
System.out.println("With " + TAX * 100 +
"% tax, purchase is $" + (cost*(1 + SalesTax.TAX)));
}
}