Run the java progam CalculatorGame (given below). In this game, the advanced AI
ID: 664226 • Letter: R
Question
Run the java progam CalculatorGame (given below). In this game, the advanced AI known as JavAttack will test to see if you are sentient or not. Each correct answer increases your score by 10. Each wrong answer decreases your score by 20. Your brain will be considered highlyevolved if you can score 100 points. However, if your score drops to -30 or less, the game ends with JavAttack - less than impressed! Run the program until you win. Notice it's always the same question (for now).
a. Presently, the variables a, b and c are fixed integer constants. Adjust the playGame() method so that 50% of the time, the values {a=1, b=2, c=3} are used, but the other 50% of the time a, b and c are uniformly distributed integers between -10 and +10. (21 different values). Reviews the Random class and the nextInt() method. The game first initializes three integers a, b and c, then inserts them into various expressions, such as (a % b % c) to obtain a myriad of fresh questions. So far it only tests two types of expression of the form: (a % b % c) and (++a + b++ - c++) Above % denotes the remainder operator. Notice the following expression from case 1 in the switch statement. (a %% b %% c) Here we have used two percent symbols %% inside the format string to actually print a single % symbol for the remainder operator. Will using just one % work? Try it and record your answer.
b. Add a case 3 in the switch statement to handle questions of the form: (++a % b++ * c) Then adjust the following stub line so it is equally likely to choose question 1, 2 or 3. Half the time the question will be of the original type, half the time the new type. Use the nextInt() method from the Random class. int choice = 1; // Fix this stub
c. Add a case 4 in the switch statement to pose questions having the form: (++a + b++ + b-- * c--);
e. Add a case 5 in the switch statement to pose questions having the form: (++a/2 + Math.max(++b,c--) )
Be sure to adjust this stub again so all cases have equal probability. int choice = 1; // Fix stub again.
f. Add a case 6 and a case 7 to handle two of your own expressions. Be creative and have fun!
package javareview;
import java.util.Random;
import java.util.Scanner;
public class CalculatorGame {
private int score ;
public CalculatorGame() {score = 0;}
public int getScore() {return score;}
public void setScore(int newScore) { score = newScore; }
public static Random randomGen = new Random(1776);
public static Scanner keyboard = new Scanner(System.in);
public static void main(String[] args) {
CalculatorGame myGame = new CalculatorGame();
myGame.printWelcome();
boolean done = false;
while(!done) {
myGame.playGame();
if (myGame.getScore() >= 100) done = true;
if (myGame.getScore() <= -30) done = true;
}
if(myGame.getScore() >= 100) System.out.println(" JavAttack: Awesome!! ");
else {System.out.println(" JavAttack: ugh! ");}
}
public void printWelcome() {
System.out.println("Greetings! I am the vast cerebral AI known as JavAttack.");
System.out.println("Let's see if you have even a rudimentary intelligence by judging your responses to some skill-testing questions.");
System.out.println("Let's start the testing, my puny, little organic lifeform!");
System.out.println("A score of 100 means you pass. A score of -30 or less and I stop wasting my time! ");
}
public void playGame() {
int [] testNumber = getNumbers(); // Variables a, b, c for each question.
int a = testNumber[0]; int b = testNumber[1]; int c = testNumber[2];
int correctAnswer = 0, userAnswer;
int equationChoice = 1;
switch(equationChoice) {
case 1:
System.out.printf("Find the value of the expression: (a %% b %% c) if a = %d, b = %d and c = %d ", a, b, c );
correctAnswer = a % b % c;
break;
case 2:
System.out.printf("Find the value of the expression: (++a + b++ - c++) if a = %d, b = %d and c = %d ", a, b, c );
correctAnswer = (++a + b++ - c++);
break;
// Add more cases here.
default:
}
System.out.print("Answer: ");
userAnswer = keyboard.nextInt();
if(userAnswer == correctAnswer)
{ this.setScore(this.getScore()+10); System.out.printf("Correct! Your score is now %d. ", this.getScore() ); }
else
{ this.setScore(this.getScore()-20); System.out.printf("Incorrect! Looks like we have an extraordinarily primitive mammal here. Score is now %d. ", this.getScore()); }
}
public static int[] getNumbers() {
int a, b, c;
int [] numbers = new int[3];
boolean useConstants = randomGen.nextInt(1)==0;
a=1; b=2; c=3;
numbers[0] = a; numbers[1] = b; numbers[2] = c;
return numbers;
}
}
Explanation / Answer
Hello there,
Two methods have been modified:
1. playGame:
2. Corrected getNumbers method
The program is running fine now. You can review the changes and use it.
import java.util.Random;
import java.util.Scanner;
public class CalculatorGame {
private int score ;
public CalculatorGame() {score = 0;}
public int getScore() {return score;}
public void setScore(int newScore) { score = newScore; }
public static Random randomGen = new Random(1776);
public static Scanner keyboard = new Scanner(System.in);
public static void main(String[] args) {
CalculatorGame myGame = new CalculatorGame();
myGame.printWelcome();
boolean done = false;
while(!done) {
myGame.playGame();
if (myGame.getScore() >= 100) done = true;
if (myGame.getScore() <= -30) done = true;
}
if(myGame.getScore() >= 100) System.out.println(" JavAttack: Awesome!! ");
else {System.out.println(" JavAttack: ugh! ");}
}
public void printWelcome() {
System.out.println("Greetings! I am the vast cerebral AI known as JavAttack.");
System.out.println("Let's see if you have even a rudimentary intelligence by judging your responses to some skill-testing questions.");
System.out.println("Let's start the testing, my puny, little organic lifeform!");
System.out.println("A score of 100 means you pass. A score of -30 or less and I stop wasting my time! ");
}
public void playGame() {
int [] testNumber = getNumbers(); // Variables a, b, c for each question.
int a = testNumber[0]; int b = testNumber[1]; int c = testNumber[2];
int correctAnswer = 0, userAnswer;
int equationChoice = 1 + randomGen.nextInt(7);
switch(equationChoice) {
case 1:
System.out.printf("Find the value of the expression: (a %% b %% c) if a = %d, b = %d and c = %d ", a, b, c );
correctAnswer = a % b % c;
break;
case 2:
System.out.printf("Find the value of the expression: (++a + b++ - c++) if a = %d, b = %d and c = %d ", a, b, c );
correctAnswer = (++a + b++ - c++);
break;
case 3:
System.out.printf("Find the value of the expression: (++a %% b++ * c) if a = %d, b = %d and c = %d ", a, b, c );
correctAnswer = (++a % b++ * c);
break;
case 4:
System.out.printf("Find the value of the expression: (++a + b++ + b-- * c--) if a = %d, b = %d and c = %d ", a, b, c );
correctAnswer = (++a + b++ + b-- * c--);
break;
case 5:
System.out.printf("Find the value of the expression: (++a/2 + Math.max(++b,c--)) if a = %d, b = %d and c = %d ", a, b, c );
correctAnswer = (++a/2 + Math.max(++b,c--));
break;
case 6:
System.out.printf("Find the value of the expression: (++a + (++b + Math.abs(c--))/2) if a = %d, b = %d and c = %d ", a, b, c );
correctAnswer = (++a + (++b + Math.abs(c--))/2);
break;
case 7:
System.out.printf("Find the value of the expression: (++a/2 + Math.max(++b,c--)) if a = %d, b = %d and c = %d ", a, b, c );
correctAnswer = (a++/2 + Math.abs(++b) + Math.min(a, c++));
break;
default:
}
System.out.print("Answer: ");
userAnswer = keyboard.nextInt();
if(userAnswer == correctAnswer)
{ this.setScore(this.getScore()+10); System.out.printf("Correct! Your score is now %d. ", this.getScore() ); }
else
{ this.setScore(this.getScore()-20); System.out.printf("Incorrect! Looks like we have an extraordinarily primitive mammal here. Score is now %d. ", this.getScore());}
}
public static int[] getNumbers() {
int [] numbers = {1, 2, 3};
//Random integer is chosen from 0 to n-1 in a call to nextInt(n)
boolean useConstants = randomGen.nextInt(2)==0;
if(!useConstants) {
//nextInt returns uniformly distributed number from 0 to 21, which is added to -10
numbers[0] = -10 + randomGen.nextInt(21);
numbers[1] = -10 + randomGen.nextInt(21);
numbers[2] = -10 + randomGen.nextInt(21);
}
return numbers;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.