In: Computer Science
Question: you will be making a game called “Snap, Crackle, Pop”, a game typically played with a group of 3 persons. Each person chooses one number and it is assigned to either 'snap', 'crackle', or 'pop'. Then the number of rounds is decided (e.g. 12). The players play the game for the predetermined number of rounds (here 12). In each round:
● if the number of the round is a multiple of the snap number, the person who chose the number will say 'snap' instead of the number of the round.
● if it is a multiple of the crackle number, they will say 'crackle' instead.
● if it is a multiple of the pop number, they will say 'pop' instead.
● if it is any combination of the three, they will say 'snapcrackle', 'snappop', 'cracklepop', or 'snapcracklepop' instead of the number of the round, depending on which of the snap, crackle, and pop numbers the current number of the round is a multiple of.
● if the number is not a multiple of snap, crackle, or pop, then the number of the round is said instead.
code to edit:
//TODOa complete this javadoc comment
/**
* [The class description]
* @author [Your Name]
* @version 1.0
*/
//TODO1: declare the SnapCracklePop class
//TODOb Complete Comments
/**
* [Instance Variable Descriptions]
*/
//TODO2 Declare private instance variables
//to store Snap, Crackle, and Pop values
//TODOc complete comments
/**
* [Constructor Description]
* @param [param name] [what the param represents]
* @param [param name] [what the param represents]
* @param [param name] [what the param represents]
*/
/* The constructor takes in three ints,
* which must be assigned to their instance variables and
initialized.
*/
//TODO3 Write the constructor
//TODOe complete comments
/**
* [Method Description]
* @param [param name] [What the parameter represents]
* @return [What the method returns]
*/
/* playRound() is a helper method for playGame().
* It takes an int parameter representing the
* current round of play, and returns the
* String result for that specific round only.
*/
//TODO4 implement the playRound method
//TODOd complete comments
/**
* [Method Description]
* @param [param name] [What the parameter represents]
* @return [What the method returns]
*/
/* playGame() takes a single parameter representing the rounds and
returns
* a String representing the result of the entire game. The helper
method
* playRound() may be useful here, so you may want to complete it
first.
*/
//TODO5 implement the playGame method
//Loop through the rounds of the game
//call playRound to handle the specific round
//return the total aggregated game results
//TODOf complete comments
/**
* [Method Description]
* @return [What this method returns]
*/
//TODO6 implement the getSnap method
//TODOg complete comments
/**
* [Method Description]
* @return [What this method returns]
*/
//TODO7 implement the getCrackle method
//TODOh complete comments
/**
* [Method Description]
* @return [What this method returns]
*/
//TODO8 implement the getPop method
//end class
code not to edit:
import java.util.*;
public class SnapCracklePopMain{
public static void main(String[] args){
// The scanner we will be using to accept user input
Scanner scan = new Scanner(System.in);
/* Initializing snap, crackle, pop, and round to invalid values to
cause them
* to enter their respective User Input loops.
*/
int snap = -1;
int crackle = -1;
int pop = -1;
int rounds = -1;
// Our SnapCracklePop object
SnapCracklePop scp;
// Capture user input for snap, while also ensuring
correctness
System.out.println("Enter an integer for your snap number.");
while(snap < 0){
System.out.println("Please enter a number greater than
zero:");
try{
snap = Integer.parseInt(scan.nextLine());
} catch (Exception e) {
System.out.println("That is not a valid number. Please enter an
integer greater than zero.");
}
}
// Capture user input for crackle, while also ensuring
correctness
System.out.println("Enter an integer for your crackle
number.");
while(crackle < 0){
System.out.println("Please enter a number greater than
zero:");
try{
crackle = Integer.parseInt(scan.nextLine());
} catch(Exception e) {
System.out.println("That is not a valid number. Please enter an
integer greater than zero.");
}
}
// Capture user input for pop, while also ensuring
correctness
System.out.println("Enter an integer for your pop number.");
while(pop < 0){
System.out.println("Please enter a number greater than
zero:");
try{
pop = Integer.parseInt(scan.nextLine());
} catch(Exception e) {
System.out.println("That is not a valid number. Please enter an
integer greater than zero.");
}
}
// Initializing our SnapCracklePop object with the values we just
obtained from the user
scp = new SnapCracklePop(snap, crackle, pop);
// Capture user input for rounds, while also ensuring
correctness
System.out.println("How many rounds would you like to
play?");
while(rounds < 0){
System.out.println("Please enter a number greater than
zero:");
try{
rounds = Integer.parseInt(scan.nextLine());
} catch(Exception e) {
System.out.println("That is not a valid number. Please enter an
integer greater than zero.");
}
}
// Printing results
System.out.println("Results:");
System.out.println(scp.playGame(rounds));
}
}
Code to Copy:
//TODOa complete this javadoc comment
/**
* [The class description]
* @author [Your Name]
* @version 1.0
*/
import java.util.*;
//TODO1: declare the SnapCracklePop class
class SnapCracklePop
{
//TODOb Complete Comments
/**
* [private variables snap, crackle and pop]
*/
//TODO2 Declare private instance variables
//to store Snap, Crackle, and Pop values
private int snap;
private int crackle;
private int pop;
//TODOc complete comments
/**
* [SnapCracklePop]
* @param [snap] [store the integer value of snap]
* @param [crackle] [store the integer value of crackle]
* @param [pop] [store the integer value of pop]
*/
/* The constructor takes in three ints,
* which must be assigned to their instance variables and
initialized.
*/
//TODO3 Write the constructor
public SnapCracklePop(int snap, int crackle, int pop)
{
this.snap = snap;
this.crackle = crackle;
this.pop = pop;
}
//TODOe complete comments
/**
* [playRound]
* @param [round] [The integer value of current round]
* @return [returns the result of current round]
*/
/* playRound() is a helper method for playGame().
* It takes an int parameter representing the
* current round of play, and returns the
* String result for that specific round only.
*/
//TODO4 implement the playRound method
String playRound(int round)
{
//set the string to empty
String output = "";
//if the current round number is multiple of snap
if(round % this.snap == 0)
{
output += "snap";
}
//if the current round number is multiple of crackle
if(round % this.crackle == 0)
{
output += "crackle";
}
//if the current round number is multiple of pop
if(round % this.pop == 0)
{
output += "pop";
}
//if the output is empty
if(output == "")
{
output += round;
}
return output;
}
//TODOd complete comments
/**
* [playGame]
* @param [rounds] [integer value of number of rounds]
* @return [returns the result of the game]
*/
/* playGame() takes a single parameter representing the rounds
and returns
* a String representing the result of the entire game. The helper
method
* playRound() may be useful here, so you may want to complete it
first.
*/
//TODO5 implement the playGame method
String playGame(int rounds)
{
//set the result to empty string
String result = "";
//set the number of snaps to 0
int snaps = 0;
//set the number of crackles to 0
int crackles = 0;
//set the number of pops to 0
int pops = 0;
//iterate over the rounds
for(int i = 1;i <= rounds;i++)
{
//call the play round function
String current_result = playRound(i);
//if the current result string has snap in it
if(current_result.indexOf("snap") != -1)
{
//increment snaps
snaps++;
}
//if the current result string has crackle in it
if(current_result.indexOf("crackle") != -1)
{
//increment crackles
crackles++;
}
//if the current result string has pop in it
if(current_result.indexOf("pop") != -1)
{
//increment pops
pops++;
}
result += "Round " + i + ": " + current_result + "\n";
}
//add the values to the result with description
result += "Number of Snaps: " + snaps + "\n";
result += "Number of Crackles: " + crackles + "\n";
result += "Number of Pops: " + pops + "\n";
return result;
}
//Loop through the rounds of the game
//call playRound to handle the specific round
//return the total aggregated game results
//TODOf complete comments
/**
* [getSnap]
* @return [return the value of snap]
*/
//TODO6 implement the getSnap method
int getSnap()
{
return this.snap;
}
//TODOg complete comments
/**
* [getCrackle]
* @return [return the value of crackle]
*/
//TODO7 implement the getCrackle method
int getCrackle()
{
return this.crackle;
}
//TODOh complete comments
/**
* [getPop]
* @return [returns the value of pop]
*/
//TODO8 implement the getPop method
int getPop()
{
return this.pop;
}
}
//end class
public class SnapCracklePopMain{
public static void main(String[] args){
// The scanner we will be using to accept user input
Scanner scan = new Scanner(System.in);
/* Initializing snap, crackle, pop, and round to invalid values to
cause them
* to enter their respective User Input loops.
*/
int snap = -1;
int crackle = -1;
int pop = -1;
int rounds = -1;
// Our SnapCracklePop object
SnapCracklePop scp;
// Capture user input for snap, while also ensuring
correctness
System.out.println("Enter an integer for your snap number.");
while(snap < 0){
System.out.println("Please enter a number greater than
zero:");
try{
snap = Integer.parseInt(scan.nextLine());
} catch (Exception e) {
System.out.println("That is not a valid number. Please enter an
integer greater than zero.");
}
}
// Capture user input for crackle, while also ensuring
correctness
System.out.println("Enter an integer for your crackle
number.");
while(crackle < 0){
System.out.println("Please enter a number greater than
zero:");
try{
crackle = Integer.parseInt(scan.nextLine());
} catch(Exception e) {
System.out.println("That is not a valid number. Please enter an
integer greater than zero.");
}
}
// Capture user input for pop, while also ensuring
correctness
System.out.println("Enter an integer for your pop number.");
while(pop < 0){
System.out.println("Please enter a number greater than
zero:");
try{
pop = Integer.parseInt(scan.nextLine());
} catch(Exception e) {
System.out.println("That is not a valid number. Please enter an
integer greater than zero.");
}
}
// Initializing our SnapCracklePop object with the values we just
obtained from the user
scp = new SnapCracklePop(snap, crackle, pop);
// Capture user input for rounds, while also ensuring
correctness
System.out.println("How many rounds would you like to
play?");
while(rounds < 0){
System.out.println("Please enter a number greater than
zero:");
try{
rounds = Integer.parseInt(scan.nextLine());
} catch(Exception e) {
System.out.println("That is not a valid number. Please enter an
integer greater than zero.");
}
}
// Printing results
System.out.println("Results:");
System.out.println(scp.playGame(rounds));
}
}
Sample Output: