Advertisement
Guest User

Untitled

a guest
Sep 25th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.36 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class main {
  4.  
  5. static double accumulator = 0;
  6.  
  7. public static void main(String [] args)
  8. {
  9. boolean loop = true;
  10.  
  11. do{
  12. try
  13. {
  14. System.out.println("To start please select an operation");
  15. System.out.println("Press + for addition");
  16. System.out.println("Press - for subtraction");
  17. System.out.println("Press * for multiplication");
  18. System.out.println("Press / for division");
  19. System.out.println("Press R to reset the accumulator to 0");
  20. System.out.println("Press P to power off the calculator");
  21.  
  22. Scanner keyboard = new Scanner(System.in);
  23.  
  24. String input = keyboard.next();
  25.  
  26. unknownOperator(input);
  27.  
  28. switch(input){
  29. case "+":
  30. System.out.println("Type in one number:");
  31. String x = keyboard.next();
  32. NumberFormatException(x);
  33. double input1 = Double.parseDouble(x);
  34. accumulator += input1;
  35. System.out.println("The sum is: " + accumulator);
  36. break;
  37.  
  38. case "-":
  39. System.out.println("Type in one number:");
  40. String x1 = keyboard.next();
  41. NumberFormatException(x1);
  42. double input2 = Double.parseDouble(x1);
  43. accumulator -= input2;
  44. System.out.println("The difference is: " + accumulator);
  45. break;
  46.  
  47. case "*":
  48. System.out.println("Type in one number:");
  49. String x2 = keyboard.next();
  50. NumberFormatException(x2);
  51. double input3 = Double.parseDouble(x2);
  52. accumulator -= input3;
  53. System.out.println("The product is: " + accumulator);
  54. break;
  55.  
  56. case "/":
  57. System.out.println("Type in one number:");
  58. String x3 = keyboard.next();
  59. NumberFormatException(x3);
  60. double input4 = Double.parseDouble(x3);
  61. accumulator -= input4;
  62. divideByZero(input4);
  63. System.out.println("The product is: " + accumulator);
  64. break;
  65.  
  66. case "R":
  67. accumulator = 0;
  68. System.out.println("Accumulator has been set to 0");
  69. break;
  70.  
  71. case "P":
  72. System.out.println("Shutting off calculator");
  73. break;
  74.  
  75.  
  76.  
  77.  
  78. }
  79. }
  80.  
  81. catch(DivideByZeroException e)
  82. {
  83. System.out.println(e.getMessage());
  84. }
  85.  
  86. catch(UnknownOperatorException e)
  87. {
  88. System.out.println(e.getMessage());
  89. }
  90.  
  91. catch(NumberFormatException e)
  92. {
  93. System.out.println(e.getMessage());
  94. }
  95.  
  96. }
  97.  
  98.  
  99. while(loop == true);
  100.  
  101.  
  102.  
  103. }
  104.  
  105. public static void divideByZero(double valueTwo) throws DivideByZeroException
  106. {
  107. if(valueTwo == 0)
  108. {
  109. throw new DivideByZeroException("You cannot divide by zero, try again.");
  110. }
  111.  
  112. }
  113.  
  114. public static void unknownOperator(String sign) throws UnknownOperatorException
  115. {
  116. if(sign.equals("+") || sign.equals("-") || sign.equals("*") || sign.equals("/") || sign.equals("R") || sign.equals("P"))
  117. {
  118.  
  119. } else {
  120. throw new UnknownOperatorException("That is an unrecognized symbol, try again.");
  121. }
  122.  
  123. System.out.println("nice");
  124. }
  125.  
  126. public static void NumberFormatException(String string) {
  127. if (string.equals("R")) {
  128. accumulator = 0;
  129. System.out.println("Accumulator has been set to 0");
  130. } else if (string.equals("P")) {
  131. System.out.println("Shutting off calculator");
  132. System.exit(0);
  133. }
  134. }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement