Basic.java 1.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /**
  2. * Basic class to perform basic functions for our Graphing Calculator
  3. * (+, -, *, /)
  4. * Lauren Green
  5. * 10/19/18
  6. */
  7. public class Basic
  8. {
  9. int input1 = 0;
  10. int input2 = 0;
  11. int answer;
  12. public Basic()
  13. {
  14. //a check to make sure user input is valid.
  15. boolean validInput = false;
  16. while(!validInput) {
  17. //Get input of first number
  18. input1 = Console.getIntegerInput("What is the first number?");
  19. //Get input of operation
  20. String operation = Console.getStringInput("Which operation would you like to perform? \n +, -, *, /");
  21. //Get input of second number
  22. input2 = Console.getIntegerInput("What is the second number?");
  23. //Do the math depending on the inputs.
  24. //addition
  25. if (operation.equals("+")) {
  26. answer = input1 + input2;
  27. Console.println(Double.toString(answer));
  28. validInput = true;
  29. //subtraction
  30. } else if (operation.equals("-")) {
  31. answer = input1 - input2;
  32. Console.println(Double.toString(answer));
  33. validInput = true;
  34. //multiplication
  35. } else if (operation.equals("*")) {
  36. answer = input1 * input2;
  37. Console.println(Double.toString(answer));
  38. validInput = true;
  39. //division
  40. } else if (operation.equals("/")) {
  41. //error message when trying to divide by 0.
  42. if (input2 == 0) {
  43. Console.println("undefined");
  44. //Insert return to main menu.
  45. } else {
  46. answer = input1 / input2;
  47. Console.println(Double.toString(answer));
  48. validInput = true;
  49. }
  50. } else {
  51. //error if wrong input was entered
  52. Console.println("Error, try again \n");
  53. }
  54. }
  55. //Insert return to main menu.
  56. }
  57. }