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

Please, I\'m stuck, I need help on my Java program Game 2(RandomNumberGenerator)

ID: 3688381 • Letter: P

Question

Please, I'm stuck, I need help on my Java program Game 2(RandomNumberGenerator) and Game 3(PatternOfSixDice) to BE INVOKED from my MAIN game MENU program. Both game 2 and game 3 works perfectly, but it does not get played from the main. Here are my programs;

MAIN

package Game;

import java.util.Scanner;

public class Menu

{

// displays the introduction to the program

private static void intro()

{

System.out.println(

"Welcome to our game program. "

+"This program allows you to play several games requiring random numbers. The computer generates these random numbers. "

+ "Which game would you like to play? "

+ "1. Throw a dice number a certain number of times in a row. "

+ "2. Demonstrate the randomness of the random number generator. "

+ "3. Throw a given pattern of six dice. "

+ "4. Find the percent difference for each dice number from the average, given a certain number of throws. "

+ "5. Tic Tac Toe. " + "6. Guess a five digit number. " + "7. Quit playing these games. "

+ "Please choose one of the 7 choices. " + "------------------------------------- ");

}

// gets the user input

private static int getInput(Scanner in)

{

int x;

while (true)

{

x = in.nextInt();

if (x <= 7&& x >= 1) // checking for user's valid input

{

System.out.println("You have chosen game number " + x);

break;

}

else

{

//informing the user for an invalid input

System.out.println("Invalid input! Please enter a value between 1 and 7");

}

}

return x;

}

// prints the game menu selection

private static void printMenu()

{

System.out.println("------------------------------------- "

+ "Please select from the list below: " + "1. DiceNumberInARow " + "2. RandomNumberGenerator "

+ "3. PatternOfSixDice " + "4. DiceNumberAverage " + "5. Tic Tac Toe " + "6. Guess a five digit number "

+ "7. Exit ");

}

// main function

public static void main(String args[])

{

intro(); // display introduction to program

// while loop allows the user to select a choice until exiting.

while (true)

{

printMenu(); // prints the menu

int x = getInput(new Scanner(System.in)); // choice of the user

switch (x)

{

case 1:

new DiceNumberInARow();

break;

case 2:

new RandomNumberGenerator();

break;

case 3:

new PatternOfSixDice();

break;

case 4:

new DiceNumberAverage();

break;

case 5:

new TicTacToe();

break;

case 6:

new GuessFiveDigitNumber();

break;

case 7:

System.out.println("Thank you for playing the game.");

System.exit(1);

}

}

}

}

GAME 2

package Game;
import java.util.Scanner;
import java.util.Random;
public class RandomNumberGenerator
{
/**
    * Default constructor
    */
     public RandomNumberGenerator() {
     }
    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) {
            //Init
            for (int i = 0; i < diceValues.length; i++) {
                diceValues[i] = 0;
            }
           
            //Ask for number of dice throws
            System.out.println("This game requests a number from the player.");
            System.out.println("The game then throws the dice that number of times.");
            System.out.println("The game then displays a chart showing the number of times "
              + "the six dice faces appeared given the number of throws.");
            System.out.print("Please enter the number of times that you would like the computer to throw the dice: ");
            numberOfTimes = keyboard.nextInt();
           
            System.out.println("The chart showing " + numberOfTimes + " throws of the dice:");
            //Throw dice and count
            while(numberOfTimes > 0) {
                int result = (int)(DICE_FACE_COUNT * Math.random()); //0-5 for easier counting
                diceValues[result]++;
                numberOfTimes--;
            }
         
            //Print the chart
            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();
            }
         
            //Ask for another game
            System.out.println(" Would you like to play the game again?");
            System.out.print("Please enter (yes/no) ");
            response = keyboard.next();
            System.out.println();
        }
    }
}


GAME 3

package Game;

import java.util.Scanner;

public class PatternOfSixDice //public class PatternOfSixDice

{

/**

* Default constructor

*/

public PatternOfSixDice(){

}

public static Scanner keyboard = new Scanner(System.in);

public static String diceSeries() {

String series;

System.out.println("This game requests six dice numbers from the player. ");

System.out.println("The game then throws the dice until that pattern appears. ");

System.out.println("The game then tells the player how many throws it took. ");

System.out.println("Please enter the six number pattern you would like "

+ "the computer to throw, separated by spaces:");

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();

}

}

}

Explanation / Answer

I have modiifed the code a bit, by removing the unnecessary calls to other functions. Just kept the required two classes and it's working fine for me. Pasted my code below and to outline the chnags done,

a)instead of having a main in each and evry method, I renamed that to another function and called it inits' own constructor.

import java.util.Scanner;
import java.util.Random;

public class HelloWorld

{

// displays the introduction to the program

private static void intro()

{

System.out.println(

"Welcome to our game program. "

+"This program allows you to play several games requiring random numbers. The computer generates these random numbers. "

+ "Which game would you like to play? "

+ "1. Throw a dice number a certain number of times in a row. "

+ "2. Demonstrate the randomness of the random number generator. "

+ "3. Throw a given pattern of six dice. "

+ "4. Find the percent difference for each dice number from the average, given a certain number of throws. "

+ "5. Tic Tac Toe. " + "6. Guess a five digit number. " + "7. Quit playing these games. "

+ "Please choose one of the 7 choices. " + "------------------------------------- ");

}

// gets the user input

private static int getInput(Scanner in)

{

int x;

while (true)

{

x = in.nextInt();

if (x <= 7&& x >= 1) // checking for user's valid input

{

System.out.println("You have chosen game number " + x);

break;

}

else

{

//informing the user for an invalid input

System.out.println("Invalid input! Please enter a value between 1 and 7");

}

}

return x;

}

// prints the game menu selection

private static void printMenu()

{

System.out.println("------------------------------------- "

+ "Please select from the list below: " + "1. DiceNumberInARow " + "2. RandomNumberGenerator "

+ "3. PatternOfSixDice " + "4. DiceNumberAverage " + "5. Tic Tac Toe " + "6. Guess a five digit number "

+ "7. Exit ");

}

// main function

public static void main(String args[])

{

intro(); // display introduction to program

// while loop allows the user to select a choice until exiting.

while (true)

{

printMenu(); // prints the menu

int x = getInput(new Scanner(System.in)); // choice of the user

switch (x)

{
case 2:

new RandomNumberGenerator();

break;

case 3:

new PatternOfSixDice();

break;


case 7:

System.out.println("Thank you for playing the game.");

System.exit(1);

}

}

}

}

class RandomNumberGenerator
{
/**
    * Default constructor
    */
     public RandomNumberGenerator() {
         RandomNumberGeneratorMain();
     }
    public static void RandomNumberGeneratorMain(){
        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) {
            //Init
            for (int i = 0; i < diceValues.length; i++) {
                diceValues[i] = 0;
            }
         
            //Ask for number of dice throws
            System.out.println("This game requests a number from the player.");
            System.out.println("The game then throws the dice that number of times.");
            System.out.println("The game then displays a chart showing the number of times "
              + "the six dice faces appeared given the number of throws.");
            System.out.print("Please enter the number of times that you would like the computer to throw the dice: ");
            numberOfTimes = keyboard.nextInt();
         
            System.out.println("The chart showing " + numberOfTimes + " throws of the dice:");
            //Throw dice and count
            while(numberOfTimes > 0) {
                int result = (int)(DICE_FACE_COUNT * Math.random()); //0-5 for easier counting
                diceValues[result]++;
                numberOfTimes--;
            }
       
            //Print the chart
            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();
            }
       
            //Ask for another game
            System.out.println(" Would you like to play the game again?");
            System.out.print("Please enter (yes/no) ");
            response = keyboard.next();
            System.out.println();
        }
    }
}

class PatternOfSixDice //public class PatternOfSixDice

{

/**

* Default constructor

*/

public PatternOfSixDice(){
    PatterOfSixDiceMain();

}

public static Scanner keyboard = new Scanner(System.in);

public static String diceSeries() {

String series;

System.out.println("This game requests six dice numbers from the player. ");

System.out.println("The game then throws the dice until that pattern appears. ");

System.out.println("The game then tells the player how many throws it took. ");

System.out.println("Please enter the six number pattern you would like "

+ "the computer to throw, separated by spaces:");

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 PatterOfSixDiceMain() {

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();

}

}

}