Question
Java Program: I need help on Part 2 Create a class MarblesJar that has the following members: - An enum of colors (as many colors
Java Program: I need help on Part 2
Create a class MarblesJar that has the following members:
- An enum of colors (as many colors as you want) called ColorValues and a particular color called 'color' that is of type ColorValues
- A static method that prints out the list of ColorValues called DisplayPossibleColors()
- An array of integers called marbleCounts. It has the same length as the number of items in ColorValues, and each element represents a total. For example marbleCounts[0] represents the number of marbles of color 0 (the enum value with ordinal 0 or in other words the first color ColorValues)
- A constructor that takes no arguments that fills the marble jar with 100 marbles
- A constructor that takes an integer as a number of marbles to fill the jar with
- A private int field 'numMarbles' with the number of marbles and an accessor/mutator for it
- A toString method that displays the color and count of marbles of each color in the marble jar along with the total numMarbles. For example it may look like:
BLUE: 10 RED: 15 GREEN: 5 TOTAL: 30
- In your main method, create a MarblesJar with the default and the overloaded constructor, and call toString() on each by printing them out via System.out.println(objRef) where objRef is the MarblesJar reference.
PART 2:
Implement a version that does all of the above, called MarblesJarAR that is the same, except it uses an ArrayList for marbleCounts instead of a plain array.
[Edit: these paragraphs apply to both parts] For this project you need the enhanced for loop and the to use the values() method of the enum as well as ColorValues.values().length when populating the jar. You need java.util.Random to randomly pick a color for each marble in that for loop. Remember that the nextInteger() method of the Random class goes up to but not including the number you pass, for example nextInteger(3) is 0, 1, or 2, but never 3. We will cover arrays in classes next time, and you will have what you need to complete the project.
In the ArrayList version, create a method called changeNumMarbles() that takes an integer and replaces the existing marbles with the new number of marbles (randomly generated like before, using the same method you used before -- so put your random generation in a separate function that can be called from the constructor or this new method). [Edit: The ArrayList must be initialized to 0 values, and will contain the same number of Integers as the array did, for example 3 values for 3 colors).
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started