In: Computer Science
Your Tasks
Step 1 Write the comments in the SnapCracklePop.java using the java doc style in zyBooks 3.8. See TODOa to TODOh.
Step 2 Complete the class definition, variable declarations, constructor, and all the methods by reading the instructions in SnapCracklePop.java. See TODO1 to TODO7. (DONE I NEED HELP WITH STEP 1)
1 //TODOa complete this javadoc comment
2 /**
3 * [The class description]
4 * @author [Your Name]
5 * @version 1.0
6 */
7
8 //TODO1: declare the SnapCracklePop class
9 class SnapCracklePop{
10 //TODOb Complete Comments
11 /**
12 * [Instance Variable Descriptions]
13 */
14
15 //TODO2 Declare private instance variables
16 //to store Snap, Crackle, and Pop values
17 private int snap;
18 private int crackle;
19 private int pop;
20
21 //TODOc complete comments
22 /**
23 * [Constructor Description]
24 * @param [param name] [what the param represents]
25 * @param [param name] [what the param represents]
26 * @param [param name] [what the param represents]
27 */
28
29 /* The constructor takes in three ints,
30 * which must be assigned to their instance variables and
initialized.
31 */
32
33 //TODO3 Write the constructor
34
35
36 //TODOe complete comments
37 /**
38 * [Method Description]
39 * @param [param name] [What the parameter represents]
40 * @return [What the method returns]
41 */
42 public SnapCracklePop(int snap, int crackle, int pop){
43 this.snap = snap;
44 this.crackle = crackle;
45 this.pop = pop;
46 }
47
48 /* playRound() is a helper method for playGame().
49 * It takes an int parameter representing the
50 * current round of play, and returns the
51 * String result for that specific round only.
52 */
53
54 //TODO4 implement the playRound method
55 String playRound(int round){
56 //set the string to empty
57 String output = "";
58 //if the current round number is multiple of snap
59 if(round % this.snap == 0){
60 output += "snap";
61 }
62 //if the current round number is multiple of crackle
63 if(round % this.crackle == 0){
64 output += "crackle";
65 }
66 //if the current round number is multiple of pop
67 if(round % this.pop == 0){
68 output += "pop";
69 }
70 //if the output is empty
71 if(output == ""){
72 output += round;
73 }
74
75 return output;
76 }
77 //TODOd complete comments
78 /**
79 * [Method Description]
80 * @param [param name] [What the parameter represents]
81 * @return [What the method returns]
82 */
83
84 /* playGame() takes a single parameter representing the rounds
and returns
85 * a String representing the result of the entire game. The
helper method
86 * playRound() may be useful here, so you may want to complete it
first.
87 */
88
89 //TODO5 implement the playGame method
90 String playGame(int rounds){
91 String result = "";
92 //set the number of snaps,crackles,pops to 0
93 int snaps = 0;
94 int crackles = 0;
95 int pops = 0;
96 for(int i = 1;i <= rounds;i++) {
97 //call the play round function
98 String current_result = playRound(i);
99 //if the current result string has snap in it
100 if(current_result.indexOf("snap") != -1){
101 // to get total number of snaps at last-increment it
102 snaps++;
103 }
104 //if the current result string has crackle in it
105 if(current_result.indexOf("crackle") != -1){
106 //to get total number of crackles at last-increment
crackles
107 crackles++;
108 }
109 //if the current result string has pop in it
110 if(current_result.indexOf("pop") != -1){
111 //to get total number of pops at last-increment pops
112 pops++;
113 }
114 result += current_result + "\n";
115 }
116 //add the values to the result with description
117 result += "Number of Snaps: " + snaps + "\n";
118 result += "Number of Crackles: " + crackles + "\n";
119 result += "Number of Pops: " + pops + "\n";
120 return result;
121 }
122
123 //Loop through the rounds of the game
124 //call playRound to handle the specific round
125 //return the total aggregated game results
126
127 //TODOf complete comments
128 /**
129 * [Method Description]
130 * @return [What this method returns]
131 */
132
133 //TODO6 implement the getSnap method
134 int getSnap(){
135 return this.snap;
136 }
137
138 //TODOg complete comments
139 /**
140 * [Method Description]
141 * @return [What this method returns]
142 */
143
144 //TODO7 implement the getCrackle method
145 int getCrackle(){
146 return this.crackle;
147 }
148 //TODOh complete comments
149 /**
150 * [Method Description]
151 * @return [What this method returns]
152 */
153
154 //TODO8 implement the getPop method
155 int getPop(){
156 return this.pop;
157 }
158 }
159
160
161
162 //end class
I finished to do 1-7 but i need help with to do a-h with the comments. please help! thanks!
I've completed all the comments from a-h. I would make my changes bold and italic for better understanding. Please leave an upvote if you like this answer. Thank you.
Code :
Important - Please delete the name the name alex and write your name.
//TODOa complete this javadoc comment
/**
* The class SnapCracklePop is a class which plays game with the
help with the help of snap, crackle and pop
* @author Alex(Suppose if your name is Alex, you have to write your
name here.)
* @version 1.0
*/
//TODO1: declare the SnapCracklePop class
class SnapCracklePop{
//TODOb Complete Comments
/**
* snap - private int used to store the value of snap.
* crackle - private int used to store the value of crackle.
* pop - private int used to store the value of 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
/**
* The constructor of this class takes three arguments and assigns
them to the fields snap, crackle and pop
* @param snap value of snap
* @param crackle value of crackle
* @param pop 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 is a helper method for playGame().
* It takes an int parameter representing the curround round of
play
* and retruns the result for that round
* @param round the current round of the play
* @return output the string result for that specefic 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() takes a single parameter representing the rounds and
returns
* a String representing the result of the entire game.
* @param rounds number of rounds in the game.
* @return result the complete result of the entire 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){
String result = "";
//set the number of snaps,crackles,pops to 0
int snaps = 0;
int crackles = 0;
int pops = 0;
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){
// to get total number of snaps at last-increment it
snaps++;
}
//if the current result string has crackle in it
if(current_result.indexOf("crackle") != -1){
//to get total number of crackles at last-increment crackles
crackles++;
}
//if the current result string has pop in it
if(current_result.indexOf("pop") != -1){
//to get total number of pops at last-increment pops
pops++;
}
result += 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
/**
* This method returns the value of the variable snap.
* @return snap
*/
//TODO6 implement the getSnap method
int getSnap(){
return this.snap;
}
//TODOg complete comments
/**
* This method returns the value of the variable crackle.
* @return crackle
*/
//TODO7 implement the getCrackle method
int getCrackle(){
return this.crackle;
}
//TODOh complete comments
/**
* This method returns the value of the variable pop.
* @return pop
*/
//TODO8 implement the getPop method
int getPop(){
return this.pop;
}
}
//end class
Solution ends.
Please comment and let me know if you have any further doubts. Please upvote this answer if you like it.
Thank you.
Have a good day.