Below is the Java code for the computer-assisted instruction (CAI) application.
import java.util.Scanner;
import java.util.Random;
public class CAI {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Random random = new Random();
int correctAnswers = 0;
int attempts = 0;
while (true) {
System.out.print("Enter the type of arithmetic problem you want to study (1 for addition, 2 for subtraction, 3 for multiplication, 4 for division, 5 for remainder, or 0 to exit): ");
int choice = input.nextInt();
if (choice == 0) {
break;
}
int num1 = random.nextInt(100);
int num2 = random.nextInt(100);
int correctAnswer = 0;
int attemptsForQuestion = 0;
switch (choice) {
case 1:
correctAnswer = num1 + num2;
System.out.print("How much is " + num1 + " + " + num2 + " ? ");
break;
case 2:
correctAnswer = num1 - num2;
System.out.print("How much is " + num1 + " - " + num2 + " ? ");
break;
case 3:
correctAnswer = num1 * num2;
System.out.print("How much is " + num1 + " x " + num2 + " ? ");
break;
case 4:
if (num2 == 0) {
System.out.println("Error: Cannot divide by zero.");
continue;
}
correctAnswer = num1 / num2;
System.out.print("How much is " + num1 + " / " + num2 + " ? ");
break;
case 5:
if (num2 == 0) {
System.out.println("Error: Cannot divide by zero.");
continue;
}
correctAnswer = num1 % num2;
System.out.print("What is the remainder when " + num1 + " is divided by " + num2 + " ? ");
break;
default:
System.out.println("Invalid choice. Please enter a valid option.");
continue;
}
int userAnswer = input.nextInt();
attemptsForQuestion++;
if (userAnswer == correctAnswer) {
attempts++;
correctAnswers++;
if (correctAnswers == 1) {
System.out.println("Very good!");
} else if (correctAnswers == 2) {
System.out.println("Excellent!");
} else {
System.out.println("Keep up the good work!");
}
} else {
attempts++;
if (attemptsForQuestion == 1) {
System.out.println("No. Please try again.");
} else if (attemptsForQuestion == 2) {
System.out.println("Wrong. Try once more.");
} else if (attemptsForQuestion == 3) {
System.out.println("Don't give up!");
} else if (attemptsForQuestion == 4) {
System.out.println("No, keep trying.");
} else {
System.out.println("Do you want to try another problem or continue? (y/n): ");
char decision = input.next().charAt(0);
if (decision != 'y') {
break;
}
}
}
}
System.out.println("You scored " + correctAnswers + " out of 10 in " + attempts + " attempts.");
}
}
So, the code above is written in Java and uses the Scanner class to handle user input and the Random class to make random numbers. The main loop of the program repeatedly gives the user to select a type of arithmetic problem to study or exit the application.
So, Based on the user's choice, the program makes two random numbers and presents the right arithmetic problem to the user. The user's answer is checked against the correct answer, and feedback is provided accordingly.