Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.text.DecimalFormat;
- import java.util.Random;
- public class MathClass {
- // creating the variables
- private int x;
- private int y;
- private int OperandNumber;
- private static double CorrectAnswer;
- private int Maximum;
- private int Minimum;
- public MathClass(int Maximum, int Minimum)
- {
- this.Maximum = Maximum;
- this.Minimum = Minimum; // initialize
- }
- public void RandomNumbers() {
- // making the number generator
- Random generator1 = new Random();
- x = generator1.nextInt(Maximum) + Minimum; // making sure the numbers are within the min and max
- y = generator1.nextInt(Maximum) + Minimum;
- }
- public void RandomOperand() {
- // making the operand generator
- Random generator2 = new Random();
- OperandNumber = generator2.nextInt(4);
- }
- public String Question(){
- if (OperandNumber == 0){
- RandomNumbers();
- CorrectAnswer =(double) x + y;
- return "What is " + x + "+" + y + "?"; // addition
- }
- else if (OperandNumber == 1){
- RandomNumbers();
- CorrectAnswer = (double)x - y;
- return "What is " + x + "-" + y + "?"; // subtraction
- }
- else if (OperandNumber == 2){
- RandomNumbers();
- CorrectAnswer = (double)x * y;
- return "What is " + x + "*" + y + "?"; // multiplication
- }
- else if (OperandNumber == 3){
- RandomNumbers();
- CorrectAnswer = (double)x / y;
- return "What is " + x + "/" + y + "?"; // division
- }
- else {
- return "The program isn't generating numbers!";
- }
- }
- Problem3 myProblem3 = new Problem3(CorrectAnswer); // pass the answer to Problem3
- public static String IncorrectAnswer() {
- DecimalFormat fmt = new DecimalFormat ("0.###");
- return "That is incorrect. The correct answer is: " + fmt.format(CorrectAnswer);
- }
- public static String CorrectAnswer() {
- DecimalFormat fmt = new DecimalFormat ("0.###");
- return "That is correct!";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement