Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java programming Create a class MarblesJar that has the following members: - An

ID: 3875911 • Letter: J

Question

Java programming

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:

- 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.

Explanation / Answer

import java.util.Random;

/**
*
* @author sambh
*/
public class MarblesJar {
  
    public enum ColorValues {

        RED ("red"),
        GREEN ("green"),
        BLUE ("blue");
      
        ColorValues(String color) {
            this.color = color;
        }
      
        public final String color;
    }
  
    public static void DisplayPossibleColors() {
        ColorValues[] colorValues = ColorValues.values();
        for (ColorValues colorValue : colorValues) {
            System.out.println(colorValue.name());
        }
    }

    public MarblesJar() {
        numMarbles = 100;
        marbleCounts = new int[numMarbles];
        int avg = numMarbles/ColorValues.values().length;
        int total = 0;
      
        for (int i = 0; i < ColorValues.values().length; i++) {
            marbleCounts[i] = avg;
            total += avg;
        }
      
        marbleCounts[new Random(System.nanoTime()).nextInt(ColorValues.values().length)] += numMarbles - total;
      
    }

    public MarblesJar(int numMarbles) {
        this.numMarbles = numMarbles;
        marbleCounts = new int[numMarbles];
        int avg = numMarbles/ColorValues.values().length;
        int total = 0;
      
        for (int i = 0; i < ColorValues.values().length; i++) {
            marbleCounts[i] = avg;
            total += avg;
        }
      
        marbleCounts[new Random(System.nanoTime()).nextInt(ColorValues.values().length)] += numMarbles - total;
    }

    public int getNumMarbles() {
        return numMarbles;
    }

    public void setNumMarbles(int numMarbles) {
        this.numMarbles = numMarbles;
        marbleCounts = new int[numMarbles];
        int avg = numMarbles/ColorValues.values().length;
        int total = 0;
      
        for (int i = 0; i < ColorValues.values().length; i++) {
            marbleCounts[i] = avg;
            total += avg;
        }
      
        marbleCounts[new Random(System.nanoTime()).nextInt(ColorValues.values().length)] += numMarbles - total;
    }

    @Override
    public String toString() {
        String str = "";
        ColorValues[] values = ColorValues.values();
      
        for (int i = 0; i < values.length; i++)
            str = str + values[i].color + ":" + marbleCounts[i] + " ";
        str = str + "TOTAL:" + numMarbles;
        return str;
    }
  
    public static void main(String[] args) {
        MarblesJar obj0 = new MarblesJar();
        MarblesJar obj1 = new MarblesJar(5);
        MarblesJar obj2 = new MarblesJar(10);
      
        System.out.println(obj0);
        System.out.println("-----------------------------------");
        System.out.println(obj1);
        System.out.println("-----------------------------------");
        System.out.println(obj2);
        System.out.println("-----------------------------------");
    }
  
    private int numMarbles;
    int[] marbleCounts;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote