Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.62 KB | None | 0 0
  1. import java.util.InputMismatchException;
  2. import java.util.Scanner;
  3.  
  4. public class Lab {
  5.  
  6. // Configuration
  7. public static final String TITLE = "Smart Calc";
  8. public static final String VERSION = "0.1";
  9.  
  10. // Variables
  11. private static Scanner console;
  12.  
  13. // Operation
  14. public static final int ADD = 1;
  15. public static final int SUBTRACT = 2;
  16. public static final int MULTIPLY = 3;
  17. public static final int DIVIDE = 4;
  18. public static final int REMAINDER = 5;
  19. public static final int POWER = 6;
  20. public static final int SQRT = 7;
  21. public static final int ABS = 8;
  22. public static final int QUIT = 10;
  23.  
  24. // Initialization
  25. public static void main(String[] args) {
  26. // Welcome Message
  27. System.out.println(TITLE + " v." + VERSION);
  28.  
  29. // Initialize Scanner
  30. console = new Scanner(System.in);
  31.  
  32. // Grab operator from user
  33. int operation = getOperator();
  34.  
  35. // Return output from operation
  36. System.out.println("--");
  37. System.out.println(operate(operation));
  38. }
  39.  
  40. // Grab operation from user input
  41. private static int getOperator() {
  42. // Prompt user
  43. System.out.println("\nPick an operation from the list.");
  44. displayMenu();
  45.  
  46. // Grab input, close scanner, and return
  47. System.out.print("\n?: "); // Draw attention to input
  48. int operator = console.nextInt();
  49. return operator;
  50. }
  51.  
  52. // Display menu options
  53. private static void displayMenu() {
  54. System.out.println("Operations:" + "\nADD - " + ADD + "\nSUBTRACT - " + SUBTRACT + "\nMULTIPLY - " + MULTIPLY + "\nDIVIDE - " + DIVIDE + "\nREMAINDER - " + REMAINDER + "\nPOWER - " + POWER + "\nSQUARE_ROOT - " + SQRT + "\nABSOLUTE_VALUE - " + ABS + "\nQUIT - " + QUIT);
  55. }
  56.  
  57. // Determine operation from input
  58. private static int operate(int operation) {
  59. // Pick operation from list
  60. switch (operation) {
  61. case ADD:
  62. return add();
  63. case SUBTRACT:
  64. return subtract();
  65. case MULTIPLY:
  66. return multiply();
  67. case DIVIDE:
  68. return divide();
  69. case REMAINDER:
  70. return remainder();
  71. case POWER:
  72. return power();
  73. case SQRT:
  74. return sqrt();
  75. case ABS:
  76. return abs();
  77. case QUIT:
  78. default:
  79. return -1; // Quit or Error
  80. }
  81. }
  82.  
  83. // Get a number, and check if it's a good one
  84. private static int getIntegerInput(char c) {
  85. // Grab input
  86. System.out.print(c + ": ");
  87. long input = console.nextLong();
  88.  
  89. // Grab user's input as long and check if it's in bounds
  90. if(input >= Integer.MIN_VALUE && input <= Integer.MAX_VALUE) {
  91. // Return input
  92. return (int) input;
  93. } else {
  94. // Otherwise, try again
  95. return getIntegerInput(c);
  96. }
  97.  
  98. }
  99.  
  100. private static int getIntegerInput() {
  101. return getIntegerInput('?');
  102. }
  103.  
  104. // Operations
  105. private static int add() {
  106. System.out.println("x + y");
  107. return getIntegerInput('x') + getIntegerInput('y');
  108. }
  109.  
  110. private static int subtract() {
  111. System.out.println("x - y");
  112. return getIntegerInput('x') - getIntegerInput('y');
  113. }
  114.  
  115. private static int multiply() {
  116. System.out.println("x * y");
  117. return getIntegerInput('x') * getIntegerInput('y');
  118. }
  119.  
  120. private static int divide() {
  121. System.out.println("x / y");
  122. return getIntegerInput('x') / getIntegerInput('y');
  123. }
  124.  
  125. private static int remainder() {
  126. System.out.println("x % y");
  127. return getIntegerInput('x') % getIntegerInput('y');
  128. }
  129.  
  130. private static int power() {
  131. System.out.println("x ^ y");
  132. return (int) Math.pow(getIntegerInput('x'), getIntegerInput('y'));
  133. }
  134.  
  135. private static int sqrt() {
  136. System.out.println("sqrt(x)");
  137. return (int) Math.sqrt(getIntegerInput('x'));
  138. }
  139.  
  140. private static int abs() {
  141. System.out.println("|x|");
  142. return (int) Math.abs(getIntegerInput('x'));
  143. }
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement