Write a program that will askthe user to enter the amount of a purchase. The pro
ID: 3613598 • Letter: W
Question
Write a program that will askthe user to enter the amount of a purchase.The program should thencompute the state and county sales tax.
Assume the state sales tax is4 percent and the county sales tax is 2 percent.
The program should displaythe amount of the purchase, the state tax, the county
sales tax, the total salestax, and the total of the sale.
(which is the sum of theamount of purchase plus the total sales tax).
Hint: Use the value 0.02 torepresent 2 percent, and 0.04 to represent 4 percent.
I tried towrite the program, but something is wrong.
import javax.swing.JOptionPane; // Needed for JOptionPane
public class SalesTax
{
public static void main(String[] args)
{
double purchase, stateTax,countyTax, totalTax, totalSale;
String input; // To hold the Stringinput
stateTax = 0.04; //To hold the state tax 4 percent
countyTax =0.02; // To hold the county tax 2 percent
totalRate = (stateTax +countyTax); // The formula to calculate total tax <- Myteacher told me this part should betotal rate, not tax amount. I don't know what does itmean.
// Get the amount ofpurchase.
input =JOptionPane.showInputDialog("How much is the amount ofpurchase?");
purchase =Double.parseDouble(input);
// Calculate the total of thesale.
totalSale = (1 + totalTax) *purchase;
// Display the resultinginformation.
JOptionPane.showMessageDialog(null, "The amount of thepurchase is $" + purchase + " The amount of the state " + "tax is " +stateTax + " " + "The amount of the county tax is " + countyTax + " " + "The total sales tax is " +totalTax + " " + "The total of the sale is $" + totalSale);
System.exit(0);
}
}
Explanation / Answer
You need to note that the tax rate is not thesame as the amount of tax paid. You should rename yourvariables so you don't get confused. doublepurchase, stateTax, countyTax, totalTax, totalSale; double stateRate, countyRate,totalRate; stateRate = 0.04; // To hold the state tax 4percent countyRate= 0.02; // Tohold the county tax 2 percent totalRate = (stateTax+ countyTax); //The formula to calculate total tax Now you need to calculate the amount of tax paid. stateTax= purchase*stateRate; countyTax = purchase*countyRate; totalTax = purchase*totalRate; // Calculate the total of thesale. totalSale = (1 + totalTax) *purchase; Your output statement now uses the correctvariables to give the correct output.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.