Basic.java 1.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import java.util.Scanner;
  2. /**
  3. * Basic class to perform basic functions for our Graphing Calculator
  4. * (+, -, *, /)
  5. * Lauren Green
  6. * 10/19/18
  7. */
  8. public class Basic
  9. {
  10. int input1 = 0;
  11. int input2 = 0;
  12. int answer;
  13. public Basic()
  14. {
  15. boolean validInput = false;
  16. while(!validInput) {
  17. //Get input of first number
  18. Scanner in2 = new Scanner(System.in);
  19. System.out.println("What is the first number?");
  20. input1 = in2.nextInt();
  21. //Get input of type of function
  22. Scanner in1 = new Scanner(System.in);
  23. System.out.println("Which operation would you like to perform? \n +, -, *, /");
  24. String operation = in1.nextLine();
  25. //Get input of second number
  26. Scanner in3 = new Scanner(System.in);
  27. System.out.println("What is the second number?");
  28. input2 = in3.nextInt();
  29. //Do the math depending on the input.
  30. if (operation.equals("+")) {
  31. answer = input1 + input2;
  32. System.out.println(answer);
  33. validInput = true;
  34. } else if (operation.equals("-")) {
  35. answer = input1 - input2;
  36. System.out.println(answer);
  37. validInput = true;
  38. } else if (operation.equals("*")) {
  39. answer = input1 * input2;
  40. System.out.println(answer);
  41. validInput = true;
  42. } else if (operation.equals("/")) {
  43. answer = input1 / input2;
  44. System.out.println(answer);
  45. validInput = true;
  46. } else {
  47. System.out.println("Error, try again \n");
  48. }
  49. }
  50. //Insert return to main menu.
  51. }
  52. }