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

I get an eclipse error that the method play() is undefined for my game classes a

ID: 3642522 • Letter: I

Question

I get an eclipse error that the method play() is undefined for my game classes and I do not know how to add the method play to them. Also if you spot anything wrong with my demo could you let me know, I haven't run it yet because of the errors so I'm not sure if it would run them.

Here is my demo:


import java.util.Scanner;
public class GameDemo {

static Scanner scan = new Scanner(System.in);

public static void main(String[] args) {
int GameChoice = 0;
DiceGame Game1 = new DiceGame();
DiceGame2 Game2 = new DiceGame2();
DiceGame3 Game3 = new DiceGame3();
DiceGame4 Game4 = new DiceGame4();
RandomGuess Game5 = new RandomGuess();
SkillGuess Game6 = new SkillGuess();

while(GameChoice != 7) {
menu();
GameChoice = input();

switch(GameChoice) {
case 1:
Game1.play();
break;
case 2:
Game2.play();
break;
case 3:
Game3.play();
break;
case 4:
Game4.play();
break;
case 5:
Game5.play();
break;
case 6:
Game6.play();
break;
case 7:
System.out.println("Closing the demo.");
break;
default:
break;
}
}
}

public static int input() {
int gameChoice = 0;
while(true) {
gameChoice = scan.nextInt();
if(gameChoice == 0)
menu();
if(gameChoice > 0 && gameChoice <= 7)
break;
else
System.out.println("Try again, 1 through 7 only. 0 for the menu again.");
}
return gameChoice;
}

public static void menu() {
System.out.println("1) Throw a particular die number a certain number of times in a row.");
System.out.println("2) Build a chart displaying the randomness of the 6 die numbers.");
System.out.println("3) Throw a given pattern of six dice.");
System.out.println("4) Find the percent difference for each die number from the average, given a certain number of throws.");
System.out.println("5) Guess the number between 1 and 100");
System.out.println("6) Guess a five digit number.");
System.out.println("7) Quit playing.");
System.out.println("Please choose one of the 7 choices.");
}
}






Here is DiceGame2:

import java.util.Scanner;
public class DiceGame2 {
public static void main(String[] args) {

final int DICE_FACE_COUNT = 6;
Scanner keyboard = new Scanner (System.in);
int numberOfTimes;
int [] diceValues = new int[DICE_FACE_COUNT];
String response = "yes";
while (response.compareTo("yes") == 0) {
for (int i = 0; i < diceValues.length; i++) {
diceValues[i] = 0;
}
System.out.println("Please enter the number of times that");
System.out.print("you would like to throw the dice: ");
numberOfTimes = keyboard.nextInt();
System.out.println("The chart showing " + numberOfTimes + " throws of the dice:");
while(numberOfTimes > 0) {
int result = (int)(DICE_FACE_COUNT * Math.random());
diceValues[result]++;
numberOfTimes--;
}
for (int i = 0; i < diceValues.length; i++) {
System.out.print(i + 1 + " ");
for (int astr = 0; astr < diceValues[i]; astr++) {
System.out.print("*");

}
System.out.println();
}
System.out.println(" Would you like to play the game again?");
System.out.print("Please enter (yes/no) ");
response = keyboard.next();
System.out.println();



}
}
}



Here is DiceGame 3:

import java.util.Scanner;
public class DiceGame3 {
public static Scanner keyboard = new Scanner(System.in);
public static String diceSeries() {
String series;
System.out.println("Please enter a series of 6 dice that "
+ "you would like the computer to duplicate");
series = keyboard.next();
return series;
}

public static String throwPattern() {
String pattern = "";
for (int i = 0; i < 6; i++) {
pattern += "" + (int)(Math.random() * 6 + 1);
}
return pattern;
}

public static int numberOfThrows(String pattern) {
int count = 0;
while (!pattern.equals(throwPattern())) {
count++;
}
return count;
}

public static void printResult(int numberOfThrows, String pattern) {
System.out.println("It took " + numberOfThrows
+ " throws to get the pattern " + pattern + " to appear");
}


public static void main(String[] args) {
String pattern;
int throwsNumber;
String again = "yes";

while (again.equals("yes")) {
pattern = diceSeries();
throwsNumber = numberOfThrows(pattern);
printResult(throwsNumber, pattern);

System.out.println(" Would you like to play the game again?");
System.out.print("Please enter (yes/no) ");
again = keyboard.next();
System.out.println();
}
}
}





Here is DiceGame4:


import java.util.Scanner;
public class DiceGame4 {
public static Scanner keyboard = new Scanner(System.in);

public static int throwTimes() {
System.out.println("Please enter the number of times that you would "
+ "like the computer to throw the dice.");
int times = keyboard.nextInt();
return times;
}

public static void processDiceFrequencies(int times, int[] diceCounts,
double[] diceFrequencies) {
int average = times / 6;
for (int i = 0; i < times; i++) {
int dieValue = (int)(Math.random() * 6 + 1);
diceCounts[dieValue - 1]++;
}
for (int i = 0; i < 6; i++) {
diceFrequencies[i] = (double)(diceCounts[i] - average) / average;
}

}

public static void printDiceFrequencies(int times, int[] diceCounts,
double[] diceFrequencies) {
System.out.println(" The number of throws is " + times
+ " with the average of " + times / 6 + ".");
System.out.println("The percentage from the average for each dice"
+ "number is:");
for (int i = 0; i < 6; i++) {
System.out.print("Dice number " + (i + 1) + " landed "
+ diceCounts[i] + " times and is ");
if (diceFrequencies[i] != 0.0) {
System.out.printf("%.1f", Math.abs(diceFrequencies[i] * 100));
System.out.println("% " + (diceFrequencies[i] > 0.0 ?
"above" : "below") + " the average.");
}
else {
System.out.println("equal to the average");
}
}
}

public static void main(String[] args) {
int times;
int[] diceCounts;
double[] diceFrequencies;
String again = "yes";

System.out.println("This game requests a number from the player.");
System.out.println("The game then throws the die that number of "
+ "times.");
System.out.println("The game then displays the percentage difference "
+ "between the number for each die face value and the average");
while (again.equals("yes")) {
diceCounts = new int[6];
diceFrequencies = new double[6];

times = throwTimes();
processDiceFrequencies(times, diceCounts, diceFrequencies);
printDiceFrequencies(times, diceCounts, diceFrequencies);

System.out.println(" Would you like to play the game again?");
System.out.print("Please enter (yes/no) ");
again = keyboard.next();
System.out.println();
}
}
}





Here is RandomGuess:

import java.util.Scanner;
public class RandomGuess {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
String cont = "yes";
int guess;
int secret;
int count;
{
while (cont.equals("yes")) {
System.out.println("This program allows you to guess a random integer");
System.out.println("between 1 and 100 inclusive. The computer creates");
System.out.println("a random number and tells you if your guess was");
System.out.println("correct, hi or low. ");

secret = (int)(Math.random() * 100) + 1;

System.out.print(" Please guess the number ");
guess = keyboard.nextInt();
count = 1;

while (guess != secret) {
System.out.print("You were "
+ (guess > secret ? "high" : "low")
+ ", please guess again ");
guess = keyboard.nextInt();
count++;
}
System.out.println("Good job! You got the number with " + count + " tries.");

System.out.println(" Would you like to play the game again?");
System.out.print("Please enter (yes/no) ");
cont = keyboard.next();
System.out.println();
}
}
}
}






Here is SkillGuess:


import java.util.Scanner;
public class SkillGuess {
public static void main(String[] args) {
System.out.println("Using the random number generator, this game creates a");
System.out.println("five-digit number. The player guesses the number.");
System.out.println("The computer returns two numbers, the first is the number");
System.out.println("of digits that are in the proper position and the second");
System.out.println("is the sum of the correct digits guessed so far.");
System.out.println("The player continues guessing until she guesses the correct number");
Scanner keyboard = new Scanner(System.in);
String cont = "yes";

while (cont.equals("yes")) {
String secret = String.valueOf((int)(Math.random() * 90000) + 10000);
String answer = keyboard.next();
int guesses = 1;
while (!answer.equals(secret)) {
int count = 0;
int sum = 0;
for (int i = 0; i < 5; i++) {
if (answer.charAt(i) == secret.charAt(i)) {
count++;
sum += Integer.parseInt(answer.substring(i, i + 1));
}
}
System.out.println("You have found " + count + " digits so far with a sum of " + sum + ". ");
guesses++;
answer = keyboard.next();
}
System.out.println("It took " + guesses + " guesses to get the number");

System.out.println("Would you like to play the game again?");
System.out.print("Please enter (yes/no) ");
cont = keyboard.next();
}
}

}

Explanation / Answer

So your switch block will become:
switch(GameChoice) {
case 1:
Game1.main(null);
break;
case 2:
Game2.main(null);
break;
case 3:
Game3.main(null);
break;
case 4:
Game4.main(null);
break;
case 5:
Game5.main(null);
break;
case 6:
Game6.main(null);
break;
case 7:
System.out.println("Closing the demo.");
break;
default:
break;
}
}
}

Please rate one answer lifesaver, and the other, whatever you like.