In: Computer Science
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.
Basic Structure
Your program should have two classes as follows:
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.
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.
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.
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.
(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.
A method to print an array of integers on one line in a field width of size 4.
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.
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.
The execution of your program should follow the outline below.
The user should enter the number of students in the class, which is a number from 15 to 30 inclusive.
The program will then need an array of integers of that size. Each element of the array will hold information for one student.
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.
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.
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).
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
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();
}
}