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

Menus Class and six games // all these projects are linked // **Check for static

ID: 3765103 • Letter: M

Question

Menus Class and six games

// all these projects are linked // **Check for static and non-static** Assignment #7 The next 7 projects work together and when finished run as one program. One of the important objectives in these programs is to solve the problem using methods to break the problem up. Please keep this objective in mind. Write a simple main class that creates an instance of the Menu class (described next) and call the Menu class’s one public method. Write a Menu class that runs 6 game classes. The Menu class has three methods: One for introducing the games program to the client, one that reads the input guaranteeing that the input is between 1 and 7 inclusive, and one for printing the menu. Please use a switch statement to choose one of the 7 choices, and a while loop to allow the client to continue playing games if he wishes. Create 6 more classes that are “stubs” of the six games that you will write as solutions for the next six projects. These stubs can be called from the Menu program, and can be used to test the syntax of the Menu program. Once the Menu program is written and debugged you can start replacing each stub with a game that satisfies the next 6 projects. Below is the sample output of the menu: This program allows you to play several games requiring random numbers. The computer generates these random numbers. Which game would you like to play? Throw a dice number a certain number of times in a row. Demonstrate the randomness of the random number generator. Throw a given pattern of six dice. Find the percent difference for each dice number from the average, given a certain number of throws. Tic Tac Toe. Guess a five digit number. Quit playing these games. Please choose one of the 7 choices. Look at post and edit: package Game; import java.util.Scanner; public class Menu{ public static void main(String args[]){ intro(); printMenu(); int x=getInput(new Scanner(System.in)); switch (x){ case 1: DieFaceInARow(); break; case 2: new RandomNumberGenarator(); break; case 3: new PatternOfSix(); break; case 4: new RandomDistribution(); break; case 5: new Game5(); break; case 6: new Game6(); break; case 7: break; } System.out.println("Thank you for playing the game. See you next time!"); } private static void intro (){ System.out.println("Welcome to our game program. " + "This program allows you to select a number of games to play"); } private static int getInput (Scanner in){ int x; while(true){ x=in.nextInt(); if(x<=7 && x>=1){ System.out.println("Your choice " +x); break; } else{ System.out.println("Wrong input! Please enter a value between 1 and 7"); } } return x; } private static void printMenu (){ System.out.println("Select from the list below: " + "1. DieFaceInARow " + "2. RandomNumberGenerator " + "3. PatternOfSix " + "4. RandomNumberDistribution " + "5. Game 5 " + "6. Game 6 "); } } Assignment #8: DiceFaceInARow class Write a game program that throws a die (singular for dice) until a certain number appears a given number of times in a row. A random die number can be generated with the following code: int diceFaceNumber = (int)((Math.random() * 6) + 1). The game should first prompt the client for a die face number he would like to appear in a row. Then the program prompts the client for the number of times he would like that die face number to appear that many time in a row. The game then throws the die until that die face number appears that many times in a row. The game reports the number of throws it took to get that die face number to appear the requested number of times in a row. Allow the client to repeat the game as many times as she wishes. Use several methods: one public method that is invoked from the main, a private method to introduce the game, two private methods that guarantee proper input, one for each input value, a private method that does the computing, and a private method that prints the output. A sample output is shown below: This game throws a dice until a particular dice face number appears in a row a certain number of times. Please enter the dice face number that you would like to be repeated. The number must be 1 through 6 inclusive. Please enter the number of times that you would like to appear in a row. . It took 44,095,944 throws to get the number 6 to appear 10 times in a row. Would you like to play the game again? Please enter (yes/no) answer: package Game; /* * Dice Face Game which shows numbers that could appear at random. */ import java.util.*; public class Game1{ public static int throwDice(){ int diceFaceNumber=(int)(Math.random()*6+1); //System.out.println(diceFaceNumber); return diceFaceNumber; } public static boolean isDiceFaceNumber(int n){ if(throwDice()==n) return true; return false; } public static long getCount(int faceNumber,int numberOfTime){ int count=0; int numThrow=0; while(count=10 && numofdays<30) charges=creditamount*0.2; System.out.println("charges paid "+charges); //Payments initialize the cash } public void payments(int cash){ int balance; int creditamount = 0; balance=creditamount-cash; System.out.println("Balance amount ="+balance); } } Would you like to play the game again? Please enter (yes/no) private int MaxAcceptedValue; private String output; public Value(int min,int max) { MinAcceptedValue=min; MaxAcceptedValue=max; //Define Scanner Scanner keyboard=new Scanner(System.in); int n = keyboard.nextInt(); System.out.println("Enter an integer variable: "+n); keyboard.close(); } public int getValue() { Scanner keyboard=new Scanner(System.in); System.out.println("Enter an integer value"); int n=keyboard.nextInt(); while(nMaxAcceptedValue) { System.out.println("Error: given number not is in range: Try again"); System.out.println("Enter an integer value"); n=keyboard.nextInt(); } return n; } public static void main(String[] args) { Value v=new Value(10,1000); v.getValue(); System.out.println(); } } Print Random: introMessage(); boolean playAgain = true; while(playAgain == true){ int throwCount = getThrowCount(reader); for(int i=0; i

Explanation / Answer

Solution to Assingnment 7:

1. Menu.java

-------------------------------------------------------------------

/**
*
*/
package game;

/**
* Class Menu. Display list of games and allows to play a game as per choice
*
*/

import java.util.Scanner;

public class Menu {

   // displays introduction to program
   private static void intro() {
       System.out.println(
               "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 user input
   private static int getInput(Scanner in) {
       int x;
       while (true) {
           x = in.nextInt();
           if (x <= 7 && x >= 1) {
               System.out.println("Your choice " + x);
               break;
           } else {
               System.out.println("Wrong input! Please enter a value between 1 and 7");
           }
       }
       return x;
   }

   // prints menu
   private static void printMenu() {
       System.out.println("Select from the list below: " + "1. DieFaceInARow " + "2. RandomNumberGenerator "
               + "3. PatternOfSix " + "4. RandomNumberDistribution " + "5. Game 5 " + "6. Game 6 "
               + "7. Exit ");
   }

   // main function
   public static void main(String args[]) {
       intro(); // display introduction to program
       // while loop allowing user to select a choice until exiting.
       while (true) {
           printMenu(); // prints menu
           int x = getInput(new Scanner(System.in)); // choice of user
           switch (x) {
           case 1:
               new DieFaceInARow();
               break;
           case 2:
               new RandomNumberGenarator();
               break;
           case 3:
               new PatternOfSix();
               break;
           case 4:
               new RandomDistribution();
               break;
           case 5:
               new TicTacToe();
               break;
           case 6:
               new GuessFiveDigitNumber();
               break;
           case 7:
               System.out.println("Thank you for playing the game. See you next time!");
               System.exit(1);
           }
       }
   }
}

--------------------------------------------

2. DieFaceInARow.java: Stub for DieFaceInARow class

----------------------------------------

package game;

/**
* Class DieFaceInARow
*
*/
class DieFaceInARow {

   /**
   * Default constructor
   */
   public DieFaceInARow() {
       System.out.println("Calling DieFaceInARow constructor...");
   }

}

---------------------------------------------------

3. RandomNumberGenerator.java: Stub for RandomNumberGenerator class

---------------------------------------------------

package game;

/**
* Class RandonNumberGenarator
*/
class RandomNumberGenarator {

   /**
   * Default constructor
   */
   public RandomNumberGenarator() {
       System.out.println("Calling RandomNumberGenarator constructor...");
   }

}

---------------------------------------------------------

4. PatternOfSix.java: Stub for PatternOfSix class

--------------------------------------------

package game;

/**
* Class PatternOfSix
*/
class PatternOfSix {

   /**
   * Default constructor
   */
   public PatternOfSix() {
       System.out.println("Calling PatternOfSix constructor...");
   }

}

-------------------------------------------------------------

5. RandomDistribution.java: Stub for RandomDistribution class

------------------------------------------------------------

package game;

/**
* Class RandonDistribution *
*/
class RandomDistribution {

   /**
   * Default constructor
   */
   public RandomDistribution() {
       System.out.println("Calling RandonDistribution constructor...");
   }

}

--------------------------------------------------------------------------

6. TicTacToe.java: Stub for TicTacToe.java

------------------------------------------------------------------

package game;

/**
* Class TicTacToe
*/
class TicTacToe {

   /**
   * Default constructor
   */
   public TicTacToe() {
       System.out.println("Calling TicTacToe constructor...");
   }

}

-----------------------------------------------

7. GuessFiveDigitNumber.java: Stub for GuessFiveDigitNumber class

-----------------------------------------------

package game;

/**
* Class GuessFiveDigitNumber
*/
class GuessFiveDigitNumber {

   /**
   * Default constructor
   */
   public GuessFiveDigitNumber() {
       System.out.println("Calling GuessFiveDigitNumber constructor...");
   }

}

-------------------------------------------------------------------

Note: Problem statement of Assignment 8 is not complete.