Guest User

Untitled

a guest
Jan 22nd, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.08 KB | None | 0 0
  1. import java.util.Scanner;
  2. public class MathTest_UsingMethod
  3. {
  4. public static void main(String[] args)
  5. {
  6. String choice;
  7. while(true)
  8. {
  9. choice = showMainMenuAndGetChoice();
  10. if(isChoiceQuit(choice))
  11. break;
  12. processChoice(choice);
  13. }
  14. showByebyeMessage();
  15. }
  16. public static String showMainMenuAndGetChoice()
  17. {
  18. showMainMenu();
  19. String choicemade=getChoice();
  20. return choicemade;
  21. }
  22. public static void showMainMenu()
  23. {
  24. System.out.println("-------------------------");
  25. System.out.println("Select mode:");
  26. System.out.println("\'m\' for a manually-set problem.");
  27. System.out.println("\'r\' for a random problem.");
  28. System.out.println("\'q\' to quit the problem.");
  29. System.out.println("-------------------------");
  30. System.out.print(">>");
  31. }
  32. public static String getChoice()
  33. {
  34. Scanner sc=new Scanner(System.in);
  35. String ch = sc.next();
  36. return ch;
  37. }
  38. public static boolean isChoiceQuit(String choice)
  39. {
  40. return choice.equals("q");
  41. }
  42. public static void processChoice(String choice)
  43. {
  44. if(choice.equals("m"))
  45. mathtestInputByYourself();
  46. else if(choice.equals("r"))
  47. mathtestInputByRandom();
  48. }
  49. public static void mathtestInputByYourself()
  50. {
  51. Scanner sc=new Scanner(System.in);
  52. double x,y,myans,realans=0;
  53.  
  54. System.out.println("Enter x >> ");
  55. x=sc.nextDouble();
  56. System.out.println("Enter y >> ");
  57. y=sc.nextDouble();
  58.  
  59. realans=realAns(x,y);
  60. myans=myAns(x,y);
  61. System.out.println(checkAns(myans,realans));
  62. }
  63. public static void mathtestInputByRandom()
  64. {
  65. double x,y,myans,realans=0,x1,y1;
  66.  
  67. x1=Math.random()*100;
  68. y1=Math.random()*100;
  69. x=(double)(Math.round(x1*10))/10;
  70. y=(double)(Math.round(y1*10))/10;
  71. while(y==0)
  72. {
  73. y=(double)(Math.round(y1*10))/10;
  74. }
  75. realans=realAns(x,y);
  76. myans=myAns(x,y);
  77. System.out.println(checkAns(myans,realans));
  78. }
  79. public static double myAns(double x,double y)
  80. {
  81. Scanner sc=new Scanner(System.in);
  82. double myanswer;
  83. System.out.println("What is the value of "+x+"/"+y+" ?");
  84. myanswer=sc.nextDouble();
  85. return myanswer;
  86. }
  87. public static double realAns(double x,double y)
  88. {
  89. return x/y;
  90. }
  91. public static String checkAns(double myans,double realans)
  92. {
  93. String a="";
  94. double diff=0;
  95. final double MAX_DIFF=1e-6;
  96.  
  97. String result;
  98. diff=Math.abs(myans-realans);
  99. if(diff<MAX_DIFF)
  100. result = "MathTest says\"Your answer is correct!\"";
  101. else
  102. {
  103. if(myans>realans)
  104. a="high";
  105. else if(myans<realans)
  106. a="low";
  107. result = "Your answer is "+diff+" too "+a;
  108. }
  109. result = result+"\nCorrect answer is "+realans;
  110. return result;
  111. }
  112. public static void showByebyeMessage()
  113. {
  114. System.out.println("\n\n---------------Good Luck----------------");
  115. }
  116.  
  117. }
Add Comment
Please, Sign In to add comment