Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.50 KB | None | 0 0
  1. // Task #1 Practice: Refactor code that is all ‘stuffed’ into main()
  2. // Define and call at least 2 subroutines that would help the program to be more understandable and re-usable.
  3. // They should be MEANINGFUL not just like public static void printTitle() { System.out.println("**Simple Calculator**"); }
  4. // They should produce (almost) the same output and behavior* as the original, 'stuffed main()' version
  5. // *There is ONE boo-boo in the calculations...find it and fix it.
  6. public class SimpleCalculator {
  7.  
  8. public static void main(String[] args) {
  9.  
  10. System.out.println("**Simple Calculator**");
  11.  
  12. System.out.println("Here are your options:");
  13. System.out.println("1. Addition");
  14. System.out.println("2. Subtraction");
  15. System.out.println("3. Division");
  16. System.out.println("4. Multiplication");
  17.  
  18. int choice;
  19. do {
  20. System.out.print("What would you like to do?: ");
  21. choice = TextIO.getlnInt();
  22. System.out.println();
  23. } while(choice != 1 && choice != 2 && choice != 3 && choice != 4);
  24.  
  25. if (choice == 1){
  26. int numOne, numTwo;
  27.  
  28. System.out.print("First Number: ");
  29. numOne = TextIO.getlnInt();
  30.  
  31. System.out.print("Second Number: ");
  32. numTwo = TextIO.getlnInt();
  33.  
  34. System.out.println("Result: " + numOne + " + " + numTwo + " = " + (numOne + numTwo));
  35. }
  36. else if (choice == 2){
  37. int numOne, numTwo;
  38.  
  39. System.out.print("First Number: ");
  40. numOne = TextIO.getlnInt();
  41.  
  42. System.out.print("Second Number: ");
  43. numTwo = TextIO.getlnInt();
  44.  
  45. System.out.println("Result: " + numOne + " - " + numTwo + " = " + (numOne - numTwo));
  46. }
  47. else if (choice == 3){
  48. int numOne, numTwo;
  49.  
  50. System.out.print("First Number: ");
  51. numOne = TextIO.getlnInt();
  52.  
  53. System.out.print("Second Number: ");
  54. numTwo = TextIO.getlnInt();
  55.  
  56. System.out.println("Result: " + numOne + " / " + numTwo + " = " + (numOne / numTwo));
  57. }
  58. else if (choice == 4){
  59. int numOne, numTwo;
  60.  
  61. System.out.print("First Number: ");
  62. numOne = TextIO.getlnInt();
  63.  
  64. System.out.print("Second Number: ");
  65. numTwo = TextIO.getlnInt();
  66.  
  67. System.out.println("Result: " + numOne + " x " + numTwo + " = " + (numOne * numTwo));
  68. }
  69. }
  70.  
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement