Hello. I am attempting to complete 5.36 of Java How to Program Late Objects edit
ID: 3763091 • Letter: H
Question
Hello. I am attempting to complete 5.36 of Java How to Program Late Objects edition the question is as follows:
Computer-Assisted Instruction: Reducing Student Fatigue) One problem in CAI environments is student fatigue. This can be reduced by varying the computer’s responses
to hold the student’s attention. Modify the program of Exercise 5.35 (your Lab10) so that various comments are displayed for each answer as follows: Possible responses to a correct answer: Very good! Excellent! Nice work! Keep up the good work! Possible responses to an incorrect answer: No. Please try again. Wrong. Try once more. Don't give up! No. Keep trying. Use random-number generation to choose a number from 1 to 4 that will be used to select one of the four appropriate responses to each correct or incorrect answer. Use a switch statement to issue the responses. Use at least two methods.
I have completed question 5.35 and am attaching my working code. The randomization of displayed answers is the part that I have not been able to figure out. Thank you!
import java.util.Random;
import java.util.Scanner;
public class CAI1113 {
public static int[] generateQuestion() {
Random r = new Random ();
int arr [] = new int [2];
arr[0]=r.nextInt(9);
arr[1]=r.nextInt(9);
return arr;
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num [] = new int[2];
int ans;
String choice;
do{
num = generateQuestion();
do{
System.out.print(" How much is "+num [0]+" times "+
num [1]+"? :");
ans = sc.nextInt();
if(ans==(num [0]*num [1])){
System.out.print(" Very Good!");
}else{
System.out.print(" No. Please try again.");
}
}while(ans!= (num [0]*num [1]));
System.out.print(" Do you want more questions? (y/n) :");
sc.nextLine();
choice = sc.nextLine();
}while(choice.equalsIgnoreCase("y"));
}
}
Explanation / Answer
import java.util.Random;
import java.util.Scanner;
public class CAI1113 {
public static int[] generateQuestion()
{
Random r = new Random ();
int arr [] = new int [2];
arr[0]=r.nextInt(9);
arr[1]=r.nextInt(9);
return arr;
}
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int num [] = new int[2];
int ans;
String choice;
do
{
num = generateQuestion();
do
{
System.out.print(" How much is "+num [0]+" times "+num [1]+"? :");
ans = sc.nextInt();
if(ans==(num [0]*num [1]))
{
int randomNum = 0+ (int)(Math.random()*4);
switch(randomNum)
{
case 0:
System.out.print(" Very good!");
break;
case 1:
System.out.print(" Excellent!");
break;
case 2:
System.out.print(" Nice work!");
break;
case 3:
System.out.print(" Keep up the good work!");
break;
}
}
else
{
int randomNum = 0+ (int)(Math.random()*4);
switch(randomNum)
{
case 0:
System.out.print(" No. Please try again!");
break;
case 1:
System.out.print(" Wrong. Try once more!");
break;
case 2:
System.out.print(" Don't give up!");
break;
case 3:
System.out.print(" No. Keep trying");
break;
}
}
}
while(ans!= (num [0]*num [1]));
System.out.print(" Do you want more questions? (y/n) :");
sc.nextLine();
choice = sc.nextLine();
}
while(choice.equalsIgnoreCase("y"));
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.