Question

In: Computer Science

Develop a Java application to simulate a game played in an elementary classroom. In this game,...

Develop a Java application to simulate a game played in an elementary classroom. In this game, the teacher places herself in the center of a circle of students surrounding her. She then distributes an even number of pieces of candy to each student. Not all students will necessarily receive the same number of pieces; however, the number of pieces of candy for each student is even and positive. When the teacher blows a whistle, each student takes half of his or her candy and passes it to the student on the right. After they do this, the teacher checks the candy in each student’s hand, and if there is an odd number, she gives one more piece to the student, so that each student again has an even number of pieces of candy. She continues to blow the whistle in this manner, each time making sure the students all have an even number by adding one to those who don’t after the handoff. The game ends when all the students have the same amount of candy after a move.

  1. Basic Structure

    Your program should have two classes as follows:

    1. The main class which has the main method; this class should be called “Controller”. It should initiate

      an instance of the second class and use that instance to call the methods in that class to perform the

      required actions to play the game.

    2. The second class should be called “CandyGame”. This class should contain the methods required to

      perform the necessary actions to play the game. The methods to be included in this class are described below.

      1. A method to get an integer from the user in the range from a lower limit to an upper limit inclusive. This method should receive the two limits as parameters. The integer returned from this method is the number of students playing the game.

      2. A method to get an even integer in the range from an even (not odd) lower limit to an even upper limit inclusive. Again, the upper and lower limits should be passed to this method.

      3. (The lower and upper limits passed to the method are the limits for the lower even number

        which in this case are 4 and 10.)
        c. A method that will distribute a number of pieces of candy to an array of integers

        (representing the students). The number given to each “student” must be random, even, and between two specified even limits inclusive. You will need to pass the lower and upper limits to this method.

      4. A method to print an array of integers on one line in a field width of size 4.

      5. A method that will pass the candy as described in the game. In essence, you are using an array to represent a circle. Portions (specifically half) of each element in the array are getting added to the element on the right, except for the last element, a portion of which gets

        added to the first.

      6. A method to test whether or not the game is over. It should return true if all values in the

        array are the same, otherwise, it should return false.

      7. The execution of your program should follow the outline below.

      8. The user should enter the number of students in the class, which is a number from 15 to 30 inclusive.

      9. The program will then need an array of integers of that size. Each element of the array will hold information for one student.

      10. The user should also enter the smallest number of pieces of candy that will be distributed, which is an even number in the range 4 to 10 inclusive.

      11. Finally, the user should enter the largest number of pieces of candy that will be distributed, which is an even number greater than the smallest number (obtained in step 3 above) but less than or equal to the smallest plus 50.

      12. a. Note that the same method should be used for steps 3 and 4 but the correct lower and upper limits will need to be passed to the method for each method call.

        5. After these numbers are entered, deal the candy and then indicate that the game is about to be played.

        a. Before playing, ask the user whether or not to print the array after each move. If yes, print it each time. If no, print only the final configuration.

        6. When the game ends, print the final configuration, which should have the same number in all cells of the array.

        The output and interface should be neat and well-described. See the two examples provided in the Testing Phase section. All user input must be done with methods and the methods should clearly specify the range for each integer. Description of what is being requested should be done in the main class (Controller).

      13. Example Output 1:

        Getting the number of students.
        Enter an integer in [15, 30] inclusive:14
        Must be in [15, 30] inclusive! Re-enter:31
        Must be in [15, 30] inclusive! Re-enter:16

        Getting the lower number of starting candy pieces from 4 to 10. Enter an even integer in [4, 10] inclusive:
        6
        Getting the upper number of starting candy pieces.

        Must be even and greater than 6 (the lower number) but less than or equal to 56 (the lower number plus 50).
        Enter an even integer in [8, 56] inclusive:
        25

        Must be EVEN and in [8, 56] inclusive! Re-enter:

        6

        Must be EVEN and in [8, 56] inclusive! Re-enter:

        24

        The original deal is:
        

        8 20 16 16 18 14 10 22 22 14 10 22 18 18 18 10
        We are ready to play the game.
        Do you want to print the status after each move? (1 for yes, 0 for no) Enter an integer in [0, 1] inclusive:
        0

        20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20

Solutions

Expert Solution

Code for CandyGame.java

import java.io.Console;

import java.util.Random;

import java.util.Scanner;

import java.util.Set;

import java.util.HashSet;

import java.util.*;

class CandyGame {

    private int[] totalStudents;

    private boolean printArray = false;

    private Scanner myScanner;

    public CandyGame() {

        myScanner = new Scanner(System.in);

    }

    public int getNumberOfStudents(int lowerLimit, int upperLimit) {

        int randomNumber;

        System.out.println("Getting the number of Students");

        System.out.println("Enter an integer in [" + lowerLimit + ", " + upperLimit + "] inclusive:");

        while (true) {

            randomNumber = myScanner.nextInt();

            if (randomNumber >= lowerLimit && randomNumber <= upperLimit) {

                break;

            } else {

                System.out.println("Must be in [" + lowerLimit + ", " + upperLimit + "] inclusive! Re-enter:");

            }

        }

        totalStudents = new int[randomNumber];

        return randomNumber;

    }

    public int getRangeOfCandy(int lowerLimit, int upperLimit) {

        int randomNumber;

        boolean flag = true;

        System.out.println("Getting the lower number of starting candy pieces:");

        System.out.println("Enter an even integer in [" + lowerLimit + ", " + upperLimit + "] inclusive:");

        do {

            randomNumber = myScanner.nextInt();

            if (randomNumber <= lowerLimit || randomNumber >= upperLimit) {

                System.out.println("Must be in [" + lowerLimit + ", " + upperLimit + "] inclusive:");

            } else if (randomNumber % 2 != 0) {

                System.out.println("Must be Even and in [" + lowerLimit + ", " + upperLimit + "] inclusive:");

            } else {

                flag = false;

            }

        } while (flag == true);

        // myScanner.close();

        return randomNumber;

    }

    public void distributeCandies(int lowerLimit, int upperLimit) {

        Random randomGenerator = new Random();

        for (int i = 0; i < totalStudents.length; i++) {

            totalStudents[i] = randomGenerator.nextInt(upperLimit) + lowerLimit;

            if (totalStudents[i] % 2 != 0) {

                i--;

                continue;

            }

        }

    }

    public void printTheOrigionalDeal() {

        for (int i = 0; i < totalStudents.length; i++) {

            System.out.print(totalStudents[i] + " ");

        }

        System.out.println();

        System.out.println("We are ready to play the game.");

        System.out.println(

                "Do you want to print the status after each move? (1 for yes, 0 for no) Enter an integer in [0, 1] inclusive:");

        Scanner myScanner = new Scanner(System.in);

        int temp;

        temp = myScanner.nextInt();

        if (temp == 0) {

            printArray = false;

        } else {

            printArray = true;

        }

        // myScanner.close();

    }

    public void startGame() {

        while (!isGameOver()) {

            int temp = totalStudents[totalStudents.length - 1] / 2;

            totalStudents[totalStudents.length - 1] = temp;

            for (int i = totalStudents.length - 2; i >= 0; i--) {

                totalStudents[i + 1] += totalStudents[i] / 2;

                totalStudents[i] = totalStudents[i] / 2;

            }

            totalStudents[0] += temp;

            for (int i = 0; i < totalStudents.length; i++) {

                if (totalStudents[i] % 2 != 0) {

                    totalStudents[i] += 1;

                }

            }

            if(printArray == true)

            {

                for (int i = 0; i < totalStudents.length; i++) {

                    System.out.print(totalStudents[i] + " ");

                }

                System.out.println();

            }

        }

    }

    public boolean isGameOver() {

        int temp  = totalStudents[0];

        boolean flag = false;

        for(int i = 0 ; i < totalStudents.length  ; i++)

        {

            if(temp != totalStudents[i])

            {

                flag = true;

                break;

            }

        }

        if(flag == false)

        {

            return true;

        }

        else

        {

            return false;

        }

    }

}

Code for Controller.java

import java.io.Console;

public class Controller

{

    public static void main(String args[])

    {

        CandyGame newGame = new CandyGame();

        int totalStudents = newGame.getNumberOfStudents(2, 6 );

        int lowerCandyLimit = newGame.getRangeOfCandy(4, 10);

        int upperCandyLimit = newGame.getRangeOfCandy(lowerCandyLimit + 2, lowerCandyLimit + 50 );

        newGame.distributeCandies(lowerCandyLimit, upperCandyLimit);

        newGame.printTheOrigionalDeal();

        newGame.startGame();

    }

}


Related Solutions

Develop a Java application that plays a "guess the number" game as described below. a) The...
Develop a Java application that plays a "guess the number" game as described below. a) The user interface is displayed and the user clicks the “Start Game” button to begin the game. b) Your application then gets a random number in the range 1-1000 inclusive (you might want to use Math.random or the Random class). c) The application then displays the following prompt (probably via a JLabel): I have a number between 1 and 1000 can you guess my number?...
One file java program that will simulate a game of Rock, Paper, Scissors. One of the...
One file java program that will simulate a game of Rock, Paper, Scissors. One of the two players will be the computer. The program will start by asking how many winning rounds are needed to win the game. Each round will consist of you asking the user to pick between rock, paper, and scissors. Internally you will get the computers choice by using a random number generator. Rock beats Scissors, Paper beats Rock, and Scissors beats Paper. You will report...
Write a Java program to simulate the rolling of two dice. The application should use an...
Write a Java program to simulate the rolling of two dice. The application should use an object of class Random once to roll the first die and again to roll the second die. The sum of the two values should then be calculated. Each die can show an integer value from 1 to 6, so the sum of the values will vary from 2 to 12. Your application should roll the dice 36,000,000 times. Store the results of each roll...
Develop a Java application which implements an application for a store chain that has three types...
Develop a Java application which implements an application for a store chain that has three types of stores which are Book, Music, and Movie stores. Your application should have an Item abstract class which should be extended by the Book and Multimedia classes. Item class has abstract priceAfterTax method, you need to implement this method in derived classes. Multimedia class is a superclass for Music and Movie classes. Your project should also include the IPromotion interface, which should be implemented...
Write a C++ console application to simulate a guessing game. Generate a random integer between one...
Write a C++ console application to simulate a guessing game. Generate a random integer between one and 100 inclusive. Ask the user to guess the number. If the user’s number is lower than the random number, let the user know. If the number is higher, indicate that to the user. Prompt the user to enter another number. The game will continue until the user can find out what the random number is. Once the number is guessed correctly, display a...
Develop a Java application that determines the gross pay for each of three employees.
(Salary Calculator) Develop a Java application that determines the gross pay for each of three employees. The company pays straight time for the first 40 hours worked by each employee and time and a half for all hours worked in excess of 40. You’re given a list of the employees, their number of hours worked last week and their hourly rates. Your program should input this information for each employee, then determine and display the employee’s gross pay. Use class...
Design and implement an Android application that plays the Rock-Paper-Scissors game against the computer. When played...
Design and implement an Android application that plays the Rock-Paper-Scissors game against the computer. When played between two people, each person picks one of three options (usually shown by a hand gesture) at the same time, and a winner is determined. In the game, Rock beats Scissors, Scissors beats Paper, and Paper beats Rock. The program should randomly choose one of the three options (without revealing it) and then seek for the user’s selection (using your choice of an object...
Create a project plan on the game or application you are creating. Using java programming. The...
Create a project plan on the game or application you are creating. Using java programming. The project plan should include the following: A description of the game or application The IDE or game engine your plan to use to create the game or app and information on how you are going to develop the game or app If you choose to create a game, how are you going to approach the game design and game development process or if you...
Monshimout is a game from the Cheyenne people and played by women. It could be played...
Monshimout is a game from the Cheyenne people and played by women. It could be played by two or more players, and if played by more than two, then the players divided into two equal teams. Game equipment consisted of five plum stones and a basket made of woven grass or willow twigs. The basket measured 3-4 inches deep, 8 inches across at the top, and almost 1/2 inch thick. The plum stones were left plain on one side, but...
Develop a Java application using Parboiled library to write aparser for a customer form. Your...
Develop a Java application using Parboiled library to write a parser for a customer form. Your program should include a grammar for the parser and display the parse tree with no errors.The customer form should include the following structure:First name, middle name (optional), last nameStreet address, city, state (or province), countryPhone numberRules• First name, middle name and last name should start with an uppercase letter followed by lower case letters. Middle name is an optional input meaning such an input...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT