In: Computer Science
6.35 (Computer-Assisted Instruction) The use of computers in education is referred to as computer-assisted instruction (CAI). Write a program that will help an elementary school student learn multiplication. Use a SecureRandom object to produce two positive one-digit integers. The program should then prompt the user with a question, such as
How much is 6 times 7?
The student then inputs the answer. Next, the program checks the student’s answer. If it’s correct, display the message "Very good!" and ask another multiplication question. If the answer is wrong, display the message "No. Please try again." and let the student try the same question repeatedly until the student finally gets it right. A separate method should be used to generate each new question. This method should be called once when the application begins execution and each time the user answers the question correctly.
(code in java please)
Here is the sulution of the given assignment. Feel free to comment for any doubt and please give thumbs up!
Additionally, I have added comments for your better understanding.
Code:
import java.security.SecureRandom;
import java.util.Scanner;
public class ComputerAssistedInstruction {
// variables for generating random numbers and reading input from the user
private static SecureRandom rand = new SecureRandom();
private static Scanner sc = new Scanner(System.in);
// method to ask question
static void askQuestion() {
// generating two random one digit numbers
int numberOne = rand.nextInt(10);
int numberTwo = rand.nextInt(10);
// asking question from the user
System.out.println("How much is " + numberOne + " times " + numberTwo + "?");
// reading answer from user
int answer = sc.nextInt();
// checking the answer
// if answer is not correct the while loop will run until correct answer is given by the user
while(answer != numberOne * numberTwo){
// asking for try again.
System.out.println("No. Please try again.");
// reading number next time
answer = sc.nextInt();
}
// Correct answer received
System.out.println("Very Good!");
}
// main method
public static void main(String[] args) {
// calling different method to ask question
// this loop will run infinite.
while(true){
askQuestion();
}
}
}
Output screenshot
Thank You!