186k views
2 votes
Assume that class MM exists and has a default constructor. Within the constructor, the MM is assigned a random color ("red","orange","yellow","green","brown",or "blue") that is stored as a String. The MM class has a getter method, getColor(), which returns the color of the MM. You do not need to write the MM class.

You are in the process of developing a guessing game where the user will guess how many MMs there are in a jar. Write a main method. It will: use an ArrayList to represent the jar, fill the jar with a random number of MMs(Max 5000), remove all of the blue MMs from the jar, and print out the original number of MMs in the jar, the number of blue MMs removed from the jar, and the number of MMs remaining in the jar. In this version, the user will not make any guesses.

User Autodesk
by
8.2k points

1 Answer

3 votes

Final answer:

To create a guessing game where the user will guess how many MMs there are in a jar, you can write a main method. Start by creating an ArrayList to represent the jar. Then, generate a random number between 1 and 5000 to determine the number of MMs in the jar. Add this many MMs to the ArrayList. To remove all the blue MMs from the jar, you can use a for loop to iterate through the ArrayList and check the color of each MM using the getColor() method. If the color is 'blue', remove the MM from the ArrayList using the remove() method. Finally, you can print out the original number of MMs in the jar using the size() method, the number of blue MMs removed from the jar using a counter variable, and the number of MMs remaining in the jar using the size() method again.

Step-by-step explanation:

To create a guessing game where the user will guess how many MMs there are in a jar, you can write a main method. Start by creating an ArrayList to represent the jar. Then, generate a random number between 1 and 5000 to determine the number of MMs in the jar. Add this many MMs to the ArrayList.

To remove all the blue MMs from the jar, you can use a for loop to iterate through the ArrayList and check the color of each MM using the getColor() method. If the color is 'blue', remove the MM from the ArrayList using the remove() method.

Finally, you can print out the original number of MMs in the jar using the size() method, the number of blue MMs removed from the jar using a counter variable, and the number of MMs remaining in the jar using the size() method again.

User Geekidharsh
by
8.4k points