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...
In Java Develop, test, and execute a graphics application for simulations using Java. Create a Java...
In Java Develop, test, and execute a graphics application for simulations using Java. Create a Java application. Given a set of events, choose the resulting programming actions. Understand the principles behind Java. Understand the basic principles of object-oriented programming including classes and inheritance. Deliverables .java files as requested below. Requirements Create all the panels. Create the navigation between them. Start navigation via the intro screen. The user makes a confirmation to enter the main panel. The user goes to the...
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 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...
It is required to develop an application in Java to perform some operations of a bank....
It is required to develop an application in Java to perform some operations of a bank. The application will have the following classes: Class called Person having following data fields (private): firstName (String), lastName(String), gender(char), cpr(long), mobile(String) and following methods: Constructor having 5 parameters to initialize all data fields, Set and get methods for all data fields, toString method to return String equivalent of all data fields, Abstract class called Account having following data fields(protected): accountHolder(Person), ccountNum(long), balance(double)and following methods:...
Write a JAVA application that asks elementary students a set of 10 math problems First ask...
Write a JAVA application that asks elementary students a set of 10 math problems First ask the user for a level and a problem type. You need to validate the level and problem type and loop until the user enters a correct one. There should be 3 levels. Level 1 operands would have values in the range of 0-9, level 2 operands would have values in the range of 0-99, and level 3 operands would have values in the range...
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...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT