Trig.java 2.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import java.util.Scanner;
  2. /**
  3. * Trig class to perform trig function for our Graphing Calculator
  4. * (sin, cos, tan, inverses, degree to radian)
  5. * Lauren Green
  6. * 10/19/18
  7. */
  8. public class Trig
  9. {
  10. int input = 0;
  11. double answer;
  12. public Trig()
  13. {
  14. boolean validInput = false;
  15. while(!validInput) {
  16. //Get input of type of function
  17. Scanner in1 = new Scanner(System.in);
  18. System.out.println("Which trignometric function would you like to run? \n sin, cos, tan, arcsin, arccos, arctan");
  19. String operation = in1.nextLine();
  20. operation = operation.toLowerCase();
  21. //Get input of number
  22. Scanner in2 = new Scanner(System.in);
  23. System.out.println("What number would you like to find the " + operation + " of?");
  24. int input = in2.nextInt();
  25. //Do the math depending on the input.
  26. if (operation.equals("sin")) {
  27. answer = Math.sin(input);
  28. System.out.println(answer);
  29. validInput = true;
  30. } else if (operation.equals("cos")) {
  31. answer = Math.cos(input);
  32. System.out.println(answer);
  33. validInput = true;
  34. } else if (operation.equals("tan")) {
  35. answer = Math.tan(input);
  36. System.out.println(answer);
  37. validInput = true;
  38. } else if (operation.equals("arcsin")) {
  39. answer = Math.asin(input);
  40. System.out.println(answer);
  41. validInput = true;
  42. } else if (operation.equals("arccos")) {
  43. answer = Math.acos(input);
  44. System.out.println(answer);
  45. validInput = true;
  46. } else if (operation.equals("arctan")) {
  47. answer = Math.atan(input);
  48. System.out.println(answer);
  49. validInput = true;
  50. } else {
  51. System.out.println("Error, try again \n");
  52. }
  53. }
  54. //Ask if they would like to convert.
  55. System.out.println("Would you like to convert your answer from radian to degrees? Y or N");
  56. Scanner in3 = new Scanner(System.in);
  57. String response = in3.nextLine();
  58. response = response.toLowerCase();
  59. if (response.equals("y")) {
  60. toDegrees(answer);
  61. } else {
  62. System.out.println("Have a nice day!");
  63. //Insert Return to Main Menu
  64. }
  65. }
  66. public void toDegrees(double answer)
  67. {
  68. double conversion = Math.toDegrees(answer);
  69. System.out.println(conversion);
  70. //Insert Return to Main Menu
  71. }
  72. }