Advertisement
Guest User

Untitled

a guest
Nov 26th, 2014
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.85 KB | None | 0 0
  1. import java.text.DecimalFormat;
  2. import java.util.Random;
  3.  
  4. public class MathClass {
  5. // creating the variables
  6. private int x;
  7. private int y;
  8. private int OperandNumber;
  9. private static double CorrectAnswer;
  10. private int Maximum;
  11. private int Minimum;
  12.  
  13. public MathClass(int Maximum, int Minimum)
  14. {
  15. this.Maximum = Maximum;
  16. this.Minimum = Minimum; // initialize
  17. }
  18. public void RandomNumbers() {
  19. // making the number generator
  20. Random generator1 = new Random();
  21. x = generator1.nextInt(Maximum) + Minimum; // making sure the numbers are within the min and max
  22. y = generator1.nextInt(Maximum) + Minimum;
  23. }
  24. public void RandomOperand() {
  25. // making the operand generator
  26. Random generator2 = new Random();
  27. OperandNumber = generator2.nextInt(4);
  28. }
  29. public String Question(){
  30. if (OperandNumber == 0){
  31. RandomNumbers();
  32. CorrectAnswer =(double) x + y;
  33. return "What is " + x + "+" + y + "?"; // addition
  34. }
  35.  
  36. else if (OperandNumber == 1){
  37. RandomNumbers();
  38. CorrectAnswer = (double)x - y;
  39. return "What is " + x + "-" + y + "?"; // subtraction
  40. }
  41.  
  42. else if (OperandNumber == 2){
  43. RandomNumbers();
  44. CorrectAnswer = (double)x * y;
  45. return "What is " + x + "*" + y + "?"; // multiplication
  46. }
  47.  
  48. else if (OperandNumber == 3){
  49. RandomNumbers();
  50. CorrectAnswer = (double)x / y;
  51. return "What is " + x + "/" + y + "?"; // division
  52. }
  53.  
  54. else {
  55. return "The program isn't generating numbers!";
  56. }
  57. }
  58.  
  59. Problem3 myProblem3 = new Problem3(CorrectAnswer); // pass the answer to Problem3
  60.  
  61. public static String IncorrectAnswer() {
  62. DecimalFormat fmt = new DecimalFormat ("0.###");
  63. return "That is incorrect. The correct answer is: " + fmt.format(CorrectAnswer);
  64. }
  65.  
  66. public static String CorrectAnswer() {
  67. DecimalFormat fmt = new DecimalFormat ("0.###");
  68. return "That is correct!";
  69. }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement