Posted By


brandonfreeman on 11/30/12

Tagged


Statistics


Viewed 586 times
Favorited by 0 user(s)

Question 1: Grocery Tax Calculator


/ Published in: Java
Save to your folder(s)

Question 1 for CS1030 Group 2 Assignment 7


Copy this code and paste it in your HTML
  1. import java.util.*;
  2.  
  3. public class taxCalculator
  4. {
  5. public static void main (String[] args)
  6. {
  7.  
  8. // initialization and setup
  9. double foodBill = 150.00; // grocery bill total for food
  10. double foodTaxRate = 0.03; // food sales tax 3%
  11. double totalFoodTax = 0.00; // total food tax amount
  12.  
  13. double nonFoodBill = 50.00; // grocery bill total for non-food
  14. double nonFoodTaxRate = 0.0675; // non food tax 6.75%
  15. double totalNonFoodTax = 0.00; // total non food tax amount
  16.  
  17. double totalTaxes = 0.00;// total taxes for both food and nonfood bills
  18. double rounded; // total tax rounded to the nearest cent
  19.  
  20.  
  21. // do calculations
  22. totalFoodTax = foodBill * foodTaxRate;
  23. totalNonFoodTax = nonFoodBill * nonFoodTaxRate;
  24. totalTaxes = totalFoodTax + totalNonFoodTax;
  25. rounded = Round(totalTaxes, 2); // pass in totalTaxes to be rounded
  26. // and to which decimal place (2)
  27. // to the Round function
  28.  
  29. //print results
  30. System.out.println("The total tax for your bill is $" + rounded);
  31.  
  32.  
  33. } // end main
  34.  
  35.  
  36. // round totalTaxes to the nearest cent
  37. public static double Round(double Rval, int Rpl)
  38. {
  39. double p = Math.pow(10,Rpl);
  40. Rval = Rval * p;
  41. double tmp = Math.round(Rval);
  42. return tmp/p; // return rounded value
  43. } // end Round
  44.  
  45.  
  46. } // end class

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.