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 2.96 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(operate(operation));
  37. }
  38.  
  39. // Grab operation from user input
  40. private static int getOperator() {
  41. // Prompt user
  42. System.out.println("\nPick an operation from the list.");
  43. displayMenu();
  44.  
  45. // Grab input, close scanner, and return
  46. System.out.print("\n?: "); // Draw attention to input
  47. int operator = console.nextInt();
  48. return operator;
  49. }
  50.  
  51. // Display menu options
  52. private static void displayMenu() {
  53. System.out.println("Operations:" + "\nADD - " + ADD + "\nSUBTRACT - " + SUBTRACT + "\nMULTIPLY - " + MULTIPLY + "\nDIVIDE - " + DIVIDE + "\nREMAINDER - " + REMAINDER + "\nPOWER - " + POWER + "\nSQUARE_ROOT - " + SQRT + "\nABSOLUTE_VALUE - " + ABS + "\nQUIT - " + QUIT);
  54. }
  55.  
  56. // Determine operation from input
  57. private static int operate(int operation) {
  58. // Pick operation from list
  59. switch (operation) {
  60. case ADD:
  61. return add();
  62. case SUBTRACT:
  63. return subtract();
  64. case MULTIPLY:
  65. return multiply();
  66. case DIVIDE:
  67. return divide();
  68. case REMAINDER:
  69. return remainder();
  70. case POWER:
  71. return power();
  72. case SQRT:
  73. return sqrt();
  74. case ABS:
  75. return abs();
  76. case QUIT:
  77. default:
  78. return -1; // Quit or Error
  79. }
  80. }
  81.  
  82. // Get a number, and check if it's a good one
  83. private static int getIntegerInput() {
  84. // Grab input
  85. System.out.print("\n?: ");
  86. long input = console.nextLong();
  87.  
  88. // Grab user's input as long and check if it's in bounds
  89. if(input >= Integer.MIN_VALUE && input <= Integer.MAX_VALUE) {
  90. // Return input
  91. return (int) input;
  92. } else {
  93. // Otherwise, try again
  94. return getIntegerInput();
  95. }
  96.  
  97. }
  98.  
  99. // Operations
  100. private static int add() {
  101. return getIntegerInput() + getIntegerInput();
  102. }
  103.  
  104. private static int subtract() {
  105. return 0;
  106. }
  107.  
  108. private static int multiply() {
  109. return 0;
  110. }
  111.  
  112. private static int divide() {
  113. return 0;
  114. }
  115.  
  116. private static int remainder() {
  117. return 0;
  118. }
  119.  
  120. private static int power() {
  121. return 0;
  122. }
  123.  
  124. private static int sqrt() {
  125. return 0;
  126. }
  127.  
  128. private static int abs() {
  129. return 0;
  130. }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement