Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(it must be a Java file) Write a program that administers a basic addition quiz

ID: 3908003 • Letter: #

Question

(it must be a Java file)

Write a program that administers a basic addition quiz to the user. There should be ten questions. Each question is a simple addition problem such as 17 + 42, where the numbers in the problem are chosen at random (and are not too big). The program should ask the user all ten questions and get the user's answers. After asking all the questions, the user should print each question again, with the user's answer. If the user got the answer right, the program should say so; if not, the program should give the correct answer. At the end, tell the user their score on the quiz, where each correct answer counts for ten points.

The program should use three subroutines, one to create the quiz, one to administer the quiz, and one to grade the quiz. It can use three arrays, with three global variables of type int[], to refer to the arrays. The first array holds the first number from every question, the second holds the second number from every questions, and the third holds the user's answers.

Explanation / Answer

/**

* This program administers a ten-question addition quiz to the user. The numbers

* for the problem are chosen at random. The numbers and the answers are one or

* two digits. After asking the user the ten questions, the computer grades the

* quiz, telling the user the correct answer for any problem they got wrong.

*/

public class AdditionQuiz {

  

private static int[] firstNumbers; // The first numbers in all ten questions.

private static int[] secondNumbers; // The second numbers in all ten questions.

private static int[] userAnswers; // The user's answers to the ten questions.

  

  

public static void main(String[] args) {

System.out.println();

System.out.println("Welcome to the addition quiz!");

System.out.println();

createQuiz();

administerQuiz();

gradeQuiz();

}

  

  

/**

* Creates the arrays that hold the numbers for the questions and fills

* them with random numbers.  

*/

private static void createQuiz() {

firstNumbers = new int[10];

secondNumbers = new int[10];

for ( int i = 0; i < 10; i++ ) {

firstNumbers[i] = (int)(Math.random() * 50 + 1); // in the range 1 to 50

secondNumbers[i] = (int)(Math.random() * 50); // in the range 0 to 49

}

}

  

  

/**

* Asks the user each of the ten quiz questions and gets the user's answers.

* The answers are stored in an array, which is created in this subroutine.

*/

private static void administerQuiz() {

userAnswers = new int[10];

for (int i = 0; i < 10; i++) {

int questionNum = i + 1;

System.out.printf("Question %2d: What is %2d + %2d ? ",

questionNum, firstNumbers[i], secondNumbers[i]);

userAnswers[i] = TextIO.getlnInt();

}

}

  

  

/**

* Shows all the questions, with their correct answers, and computes a grade

* for the quiz. For each question, the user is told whether they got

* it right.

*/

private static void gradeQuiz() {

System.out.println();

System.out.println("Here are the correct answers:");

int numberCorrect = 0;

int grade;

for (int i = 0; i < 10; i++) {

int questionNum = i + 1;

int correctAnswer = firstNumbers[i] + secondNumbers[i];

System.out.printf(" Question %2d: %2d + %2d = %2d. ",

questionNum, firstNumbers[i], secondNumbers[i], correctAnswer);

if ( userAnswers[i] == correctAnswer ) {

System.out.println("You were CORRECT.");

numberCorrect++;

}

else {

System.out.println("You said " + userAnswers[i] + ", which is INCORRECT.");

}

}

grade = numberCorrect * 10;

System.out.println();

System.out.println("You got " + numberCorrect + " questions correct.");

System.out.println("Your grade on the quiz is " + grade);

System.out.println();

}

} // end class AdditionQuiz

Here is a version that uses parameters and no global variables:

/**

* This program administers a ten-question addition quiz to the user. The numbers

* for the problem are chosen at random. The numbers and the answers are one or

* two digits. After asking the user the ten questions, the computer grades the

* quiz, telling the user the correct answer for any problem they got wrong.

*/

public class AdditionQuizNoGlobals {

  

  

public static void main(String[] args) {

int[] firstNums = new int[10]; // The first numbers in the ten problems

int[] secondNums = new int[10]; // The second numbers in the ten problems

int[] answers = new int[10]; // The user's answers.

System.out.println();

System.out.println("Welcome to the addition quiz!");

System.out.println();

createQuiz(firstNums,secondNums);

administerQuiz(firstNums,secondNums,answers);

gradeQuiz(firstNums,secondNums,answers);

}

  

  

/**

* Creates the arrays that hold the numbers for the questions and fills

* them with random numbers. The parameters are arrays that will hold

* the random numbers for the first and second operands of each addition

* problem. The arrays must have already been created when this subroutine

* is called!

*/

private static void createQuiz(int[] firstNumbers, int[] secondNumbers) {

for ( int i = 0; i < 10; i++ ) {

firstNumbers[i] = (int)(Math.random() * 50 + 1); // in the range 1 to 50

secondNumbers[i] = (int)(Math.random() * 50); // in the range 0 to 49

}

}

  

  

/**

* Asks the user each of the ten quiz questions and gets the user's answers.

* The answers are stored in an array, which is created in this subroutine.

* The first two parameters hold the operands for the quiz questions. The user's

* answers to the ten problems will be stored in the third array.

*/

private static void administerQuiz(int[] firstNumbers, int[] secondNumbers, int[] userAnswers) {

for (int i = 0; i < 10; i++) {

int questionNum = i + 1;

System.out.printf("Question %2d: What is %2d + %2d ? ",

questionNum, firstNumbers[i], secondNumbers[i]);

userAnswers[i] = TextIO.getlnInt();

}

}

  

  

/**

* Shows all the questions, with their correct answers, and computes a grade

* for the quiz. For each question, the user is told whether they got

* it right. The first two parameters hold the operands for the quiz questions,

* and the third parameter holds the answers that the user gave to the quiz.

*/

private static void gradeQuiz(int[] firstNumbers, int[] secondNumbers, int[] userAnswers) {

System.out.println();

System.out.println("Here are the correct answers:");

int numberCorrect = 0;

int grade;

for (int i = 0; i < 10; i++) {

int questionNum = i + 1;

int correctAnswer = firstNumbers[i] + secondNumbers[i];

System.out.printf(" Question %2d: %2d + %2d = %2d. ",

questionNum, firstNumbers[i], secondNumbers[i], correctAnswer);

if ( userAnswers[i] == correctAnswer ) {

System.out.println("You were CORRECT.");

numberCorrect++;

}

else {

System.out.println("You said " + userAnswers[i] + ", which is INCORRECT.");

}

}

grade = numberCorrect * 10;

System.out.println();

System.out.println("You got " + numberCorrect + " questions correct.");

System.out.println("Your grade on the quiz is " + grade);

System.out.println();

}

} // end class AdditionQuizNoGlobals