Copy your program you wrote for Assignment 8 and rename the file Assign9.java an
ID: 3566414 • Letter: C
Question
Copy your program you wrote for Assignment 8 and rename the file Assign9.java and the class Assign9.
Declare another integer variable named priceCount and initialize it to 0.
Declare another double variable named totalOfAllPrices and initialize it to 0.
At the top of the do while loop, increment the counter named priceCount by 1 each time through the loop.
After calculating the total of the price entered plus the sales tax calculated, add the total to the totalOfAllPrices variable.
After the loop ends but before the good-bye (trailer) message, display the value in the counter variable and, on a second double-spaced line, display the value in the accumulator variable.
Please see the attached Assign 9 Output.txt document for sample output
import java.util.Scanner;
import java.text.DecimalFormat;
public class Assign8
{
public static void main(String[] args)
{
final double sales_tax_rate = 0.01;
final double second_tax_rate = 0.0275;
final double third_tax_rate = 0.035;
final double fourth_tax_rate = 0.0475;
double price,
salesTax = 0,
total;
int ans = 0;
DecimalFormat currency = new DecimalFormat("$#,###,##0.00");
Scanner keyScan = new Scanner(System.in);
System.out.print("Welcome to the NIU Pricing Ststem");
do
{
System.out.print(" Please enter the item's price: ");
price = keyScan.nextDouble();
System.out.printf(" Price: %13s " , currency.format(price));
if(price>0&&price<75)
{
salesTax = price * fourth_tax_rate;
}
else if(75<= price && price <125)
{
salesTax = (price * second_tax_rate);
}
else if(125 <= price && price <225)
{
salesTax = (price * third_tax_rate);
}
else if(price>=225)
{
salesTax = (price * sales_tax_rate);
}
System.out.printf(" Sales Tax: %13s " , currency.format(salesTax));
for(int i=0;i<13;i++)
{
System.out.print("-");
}
total = price + salesTax;
System.out.printf(" Total: %13s ", currency.format(total));
System.out.printf("Please enter '1' if you want to do another: ");
ans = keyScan.nextInt();
} while (ans == 1);
System.out.print(" Thank you for using the NIU Pricing System!");
}
}
Explanation / Answer
package p1;
import java.util.Scanner;
import java.text.DecimalFormat;
public class Assign9
{
public static void main(String[] args)
{
final double sales_tax_rate = 0.01;
final double second_tax_rate = 0.0275;
final double third_tax_rate = 0.035;
final double fourth_tax_rate = 0.0475;
int priceCount = 0;
double totalOfAllPrices = 0.0;
double price,
salesTax = 0,
total;
int ans = 0;
DecimalFormat currency = new DecimalFormat("$#,###,##0.00");
Scanner keyScan = new Scanner(System.in);
System.out.print("Welcome to the NIU Pricing Ststem");
do
{
System.out.print(" Please enter the item's price: ");
price = keyScan.nextDouble();
System.out.printf(" Price: %13s " , currency.format(price));
if(price>0&&price<75)
{
salesTax = price * fourth_tax_rate;
}
else if(75<= price && price <125)
{
salesTax = (price * second_tax_rate);
}
else if(125 <= price && price <225)
{
salesTax = (price * third_tax_rate);
}
else if(price>=225)
{
salesTax = (price * sales_tax_rate);
}
System.out.printf(" Sales Tax: %13s " , currency.format(salesTax));
for(int i=0;i<13;i++)
{
System.out.print("-");
}
total = price + salesTax;
totalOfAllPrices += total;
System.out.printf(" Total: %13s ", currency.format(total));
System.out.printf("Please enter '1' if you want to do another: ");
ans = keyScan.nextInt();
priceCount = priceCount+1;
} while (ans == 1);
System.out.println("Number Processed: " + priceCount);
System.out.println(" Grand Total: " + totalOfAllPrices);
System.out.print(" Thank you for using the NIU Pricing System!");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.