I have written the following program: Math Quiz. It is a programming exercise number 5 chapter 6 from the book "Art and Science of Java".
The program seems to be working. However, I would be grateful for any suggestions on any areas that I can improve on (any bad habits etc.).
I am also intrested if there are any more efficient or elegant ways of randomly choosing two numbers which meet the criteria listed beneath.
The program should meet these requirements:
• It should ask a series of five questions - arithmetic problems - coded as a named constant.
• Each question should consist of a single addition or subtraction problem involving just two numbers.
• The problem — addition or subtraction — should be chosen randomly for each question.
• None of the numbers involved, including the answer, should be less than 0 or greater than 20 - within these constraints, the program should choose the numbers randomly.
• The program should give the student three chances to answer each question. If the student gives the correct answer, the program should indicate that fact in some properly congratulatory way.
• If the student does not get the answer in three tries, the program should give the answer and go on to another problem.
/*
* File: MathQuiz.java
* ---------------------
*/
import acm.program.*;
import acm.util.*;
public class MathQuiz extends ConsoleProgram {
private static final int NUMBER_OF_QUESTIONS = 5;
public void run() {
println("Welcome to MathQuiz");
// Starts a function which will ask questions
for (int i=0; i < NUMBER_OF_QUESTIONS; i++) {
// Draws a random number between 0 and 20
int x = getRandomX();
// Draws a random sign - plus or minus with a probability of 50 per cent
String sign = getPlusOrMinus();
// Initializes a second variable which will be used for second random number
int y = 0;
// Initializes a variable where the good answer to the arithmetic problem will be kept
int goodanswer = 0;
// Draws a second random number between 0 and 20-x (method getRandomYPlus) if the drawn sign is + or between 0 and x (method getRandomYMinus) if the drawn sign is -
// I have created two methods because that numbers have to be such so that the answer will not be greater than 20 or less than zero
if (sign == "+") {
y = getRandomYPlus(x);
goodanswer = x + y;
} else {
y = getRandomYMinus(x);
goodanswer = x-y;
}
// Poses an arithmetic problem with the two random numbers and a random sign
int answer = readInt("What is " + x + sign + y + "? ");
// Initializes a variable which will count the number of answers
int count = 0;
// Students gets three chances to give a correct answer
while (answer != goodanswer && count !=3) {
answer = readInt("That`s incorrect - try a different answer: ");
count ++;
}
if (answer == goodanswer) {
println("You got it!");
// After three wrong answers program gives a correct answer and moves to another problem
} else {
println ("No, the answer is " + goodanswer + ".");
}
}
}
// Method which draws a random number between 0 and 20
private int getRandomX() {
int x = 0;
x = rgen.nextInt(0,20);
return x;
}
// Method which draws a random sign - plus or minus - with a probability of 50 per cent
private String getPlusOrMinus() {
String sign = null;
sign = rgen.nextBoolean() ? "+" : "-";
return sign;
}
// Draws a second random number between 0 and 20-x (method getRandomYPlus) if the drawn sign is + or between 0 and x (method getRandomYMinus) if the drawn sign is -
// I have created two methods because that numbers have to be such so that the answer will not be greater than 20 or less than zero
private int getRandomYPlus(int x) {
int y = 0;
y = rgen.nextInt(0,20-x);
return y;
}
private int getRandomYMinus(int x) {
int y = 0;
y = rgen.nextInt(0,x);
return y;
}
// Creates an instance variable for random number generator
private RandomGenerator rgen = new RandomGenerator();
}