Advertisement
Guest User

Java Hw2/3 Example

a guest
Mar 30th, 2015
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.37 KB | None | 0 0
  1. 3public class Expenses{
  2. 4.          public static void main(String[] args){
  3. 5.                  String expense[] = {"Coffee", "Snacks", "Tuition", "Gas", "Fun", "Website"};// The names of each expenses
  4. 6.                  int expFreq[] = {104, 52, 2, 24, 52, 12};// The frequency of each expense
  5. 7.                  double amount[] = {3.62, 12, 6500, 55, 25, 4.99};// The amounts of each expense
  6. 8.                  double total = 0;
  7. 9.                  System.out.printf("%-15s\t%-15s\t%-10s\t%10s\t%15s\n",
  8. 10.                                 "Item", "Frequency", "Times", "Amount", "Annual Total");
  9. 11.                 for(int i = 0; i < 6; i++){ // do not hard code 6; use array length property
  10. 12.                         outputInfo(expense[i], getLabel(expFreq[i]), expFreq[i], amount[i]);
  11. 13.                         total += expFreq[i] * amount[i];
  12. 14.                 }
  13. 15.                 System.out.printf("%-15s\t%-15s\t%-10s\t%10s\t%15.2f\n",
  14. 16.                                 "Total", " ", " ", " ", total);
  15. 17.         }
  16. 18.         public static String getLabel(int times){
  17. 19.                 String freq = "";
  18. 20.                 switch(times){// Switch statement to determine a string-value for Frequency
  19. 21.                 case 2:
  20. 22.                         freq = "Bi-Yearly";
  21. 23.                         break;
  22. 24.                 case 12:
  23. 25.                         freq = "Monthly";
  24. 26.                         break;
  25. 27.                 case 24:
  26. 28.                         freq = "Bi-Monthly";
  27. 29.                         break;
  28. 30.                 case 52:
  29. 31.                         freq = "Weekly";
  30. 32.                         break;
  31. 33.                 case 104:
  32. 34.                         freq = "Bi-Weekly";
  33. 35.                         break;
  34. 36.                 default:
  35. 37.                         freq = "Invalid Frequency";// inconsistent with flowchart
  36. 38.                         break;
  37. 39.                 }
  38. 40.                 return freq;
  39. 41.         }
  40. 42.         public static void outputInfo(String item, String freq, int times, double amount){
  41. 43.                 System.out.printf("%-15s\t%-15s\t%-10s\t%10s\t%15s\n",
  42. 44.                                 item, freq, times, amount, times * amount);
  43. 45.         }// not exactly what was required… in requirement, the method should call getLabel to get label based on times.
  44. 46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement