Question
This is a computer science II course, where all we have learnt up to are basic generic types and linear data structures. Please don't use
This is a computer science II course, where all we have learnt up to are basic generic types and linear data structures. Please don't use non-linear data structures such as HashSets or HashMaps for your solution.
I have completed most of the programming in this question. I have completed most of the programming in this question. All i need, is for you to make the"Count Ocurrences" button work. It should be:
the DiceArrayBagTester.java class. You MAY NOT modify Die but CAN MODIFY the IntArrayBag.java. A sample output is shown below. Note that in this sample output the ocurrences of a certain dice does not work. THIS IS WITHOUT THE OCURRENCES:
Below are the following classes:
Die.java:
/** * class to simulate a die (singular of dice). * It can simulate a die with 4,6,20, or 100 sides * */ public class Die{
//data fields that describe the state of a die. private int numSides; private int value;
/** * Zero-argument constructor. Creates 6-sided Die with random (rolled) value. */ public Die(){ this(6); }
/** * Argumented constructor. Die will be rolled to have a random value. * @param num number of sides on the Die. */ public Die(int num){ if(num == 4 || num == 6 || num == 20 || num == 100) numSides = num; else numSides = 6; this.roll(); }
/** * Mutator that rolls the Die. Value should be legal for the number of sides * on the Die. */ public void roll(){ value = (int)(Math.random()*numSides) + 1; } /** * Accessor * @return the side up - the value on the Die. */ public int getValue() { return value; } /** * Accessor * @return number of sides on the Die. */ public int getNumSides() { return numSides; } /** * Return the String representation of a Die. */ public String toString() { return "Die["+numSides+" sides, value=" + value +"]"; }
/** * Equals method that checks the number of sides but ignores the value. * @param in a die to check. * @return true if equal (same number of sides), else false. */ @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Die other = (Die) obj; if (numSides != other.numSides) return false; return true; }
}
Then we have the IntArrayBag.java:
package question2;
// File: IntArrayBag.java from the package edu.colorado.collections // Additional javadoc documentation is available from the IntArrayBag link in: // http://www.cs.colorado.edu/~main/docs
/****************************************************************************** * An IntArrayBag is a collection of int numbers. * The same number may appear multiple times in a bag. * * @note * (1) The capacity of one of these bags can change after it's created, but * the maximum capacity is limited by the amount of free memory on the * machine. The constructor, addItem, clone, * and union will result in an OutOfMemoryError * when free memory is exhausted. *
* (2) A bag's capacity cannot exceed the maximum integer 2,147,483,647 * (Integer.MAX_VALUE). Any attempt to create a larger capacity * results in a failure due to an arithmetic overflow. *
* (3) Because of the slow linear algorithms of this * class, large bags will have poor performance. * * @see * * Java Source Code for this class * (www.cs.colorado.edu/~main/edu/colorado/collections/IntArrayBag.java) * * * @author Michael Main * (main@colorado.edu) * * @version * Jul 5, 2005 * * @see ArrayBag * @see IntLinkedBag ******************************************************************************/ public class IntArrayBag implements Cloneable { // Invariant of the IntArrayBag class: // 1. The number of elements in the bag is in the instance variable // manyItems, which is no more than data.length. // 2. For an empty bag, we do not care what is stored in any of data; // for a non-empty bag, the elements in the bag are stored in data[0] // through data[manyItems-1], and we don?t care what?s in the // rest of data. private Object[ ] data; private int manyItems; /** * Initialize an empty bag with an initial capacity of 10. Note that the * addItem method works efficiently (without needing more * memory) until this capacity is reached. * @param - none * @postcondition * This bag is empty and has an initial capacity of 10. * @exception OutOfMemoryError * Indicates insufficient memory for: * new int[10]. **/ public IntArrayBag( ) { final int INITIAL_CAPACITY = 10; manyItems = 0; data = new Object[INITIAL_CAPACITY]; }
/** * Initialize an empty bag with a specified initial capacity. Note that the * addItem method works efficiently (without needing more * memory) until this capacity is reached. * @param initialCapacity * the initial capacity of this bag * @precondition * initialCapacity is non-negative. * @postcondition * This bag is empty and has the given initial capacity. * @exception IllegalArgumentException * Indicates that initialCapacity is negative. * @exception OutOfMemoryError * Indicates insufficient memory for: new int[initialCapacity]. **/ public IntArrayBag(int initialCapacity) { if (initialCapacity
data[manyItems] = element; manyItems++; }
/** * Add new elements to this bag. If the new elements would take this * bag beyond its current capacity, then the capacity is increased * before adding the new elements. * @param elements * (a variable-arity argument) * one or more new elements that are being inserted * @postcondition * A new copy of the element has been added to this bag. * @exception OutOfMemoryError * Indicates insufficient memory for increasing the bag's capacity. * @note * An attempt to increase the capacity beyond * Integer.MAX_VALUE will cause the bag to fail with an * arithmetic overflow. **/ //Shift responsibility to user to use this method correctly @SuppressWarnings("unchecked") public void addMany(T... elements) { if (manyItems + elements.length > data.length) { // Ensure twice as much space as we need. ensureCapacity((manyItems + elements.length)*2); }
System.arraycopy(elements, 0, data, manyItems, elements.length); manyItems += elements.length; }
/** * Accessor method to count the number of occurrences of a particular element * in this bag. * @param target * the element that needs to be counted * @return * the number of times that target occurs in this bag **/ public int countOccurrences(T target) { int answer; int index; answer = 0; for (index = 0; index
/** * Change the current capacity of this bag. * @param minimumCapacity * the new capacity for this bag * @postcondition * This bag's capacity has been changed to at least minimumCapacity. * If the capacity was already at or greater than minimumCapacity, * then the capacity is left unchanged. * @exception OutOfMemoryError * Indicates insufficient memory for: new int[minimumCapacity]. **/ public void ensureCapacity(int minimumCapacity) { Object[ ] biggerArray; if (data.length
/** * Accessor method to get the current capacity of this bag. * The add method works efficiently (without needing * more memory) until this capacity is reached. * @param - none * @return * the current capacity of this bag **/ public int getCapacity( ) { return data.length; }
/** * Remove one copy of a specified element from this bag. * @param target * the element to remove from the bag * @postcondition * If target was found in the bag, then one copy of * target has been removed and the method returns true. * Otherwise the bag remains unchanged and the method returns false. **/ public boolean remove(T target) { int index; // The location of target in the data array. // First, set index to the location of target in the data array, // which could be as small as 0 or as large as manyItems-1; If target // is not in the array, then index will be set equal to manyItems; for (index = 0; (index
for(int i = 0; i if(i == manyItems - 1) result += data[i]; else { result += data[i] + ", "; } return result; } }
And finally the point of interest, the DiceArrayBagTester.java. Here please only make it so that the ocurrences method works. Thanks!:
import javax.swing.*; import java.awt.*; import java.awt.event.*;
public class DiceArrayBagTester extends JFrame { private JPanel createPanel, removePanel, printPanel,randomPanel; private Button createButton, removeButton,printButton, randomButton; private JComboBox dieOptions1, dieOptions2, printOptions; private IntArrayBag diceBag = new IntArrayBag(0); private int[] dice = {4,6,20,100}; private JTextArea output; private String[] printOps;
public DiceArrayBagTester(){ String[] dieTypes = { "4 Sides", "6 Sides", "20 Sides", "100 Sides"}; //Create Panel createPanel = new JPanel(); createButton = new Button("Create Die"); createButton.addActionListener(new createButtonHandler()); dieOptions1= new JComboBox(dieTypes); createPanel.add(dieOptions1); createPanel.add(createButton); createPanel.setBorder( BorderFactory.createEtchedBorder()); //Remove Panel removePanel = new JPanel(); removeButton = new Button("Remove Die"); removeButton.addActionListener( new removeButtonHandler()); dieOptions2= new JComboBox(dieTypes); removePanel.add(dieOptions2); removePanel.add(removeButton); removePanel.setBorder(BorderFactory.createEtchedBorder()); //Roll random die randomPanel= new JPanel(); randomButton = new Button("Roll Random Die"); randomButton.addActionListener(new randomButtonHandler()); randomPanel.add(randomButton); //Print Panel printPanel = new JPanel(); printButton = new Button("Print"); printButton.addActionListener(new printButtonHandler()); output = new JTextArea(20,20); output.setEditable(false); output.setSize(output.getMinimumSize()); printOps= new String[] {"Print current dice in the bag","Print Capacity of dice bag", "Occurences of 4 sided die","Occurences of 6 sided die", "Occurences of 20 sided die", "Occurences of 100 sided die" }; printOptions= new JComboBox(printOps);
printPanel.setLayout(new GridBagLayout()); GridBagConstraints c = new GridBagConstraints(); c.gridx=0;c.gridy=0; printPanel.add(printButton,c); c.gridx=0;c.gridy=0; printPanel.add(printOptions);
c.gridx=0; c.gridy=1; c.gridwidth=GridBagConstraints.CENTER; printPanel.add(output,c); printPanel.setBorder(BorderFactory.createEtchedBorder()); //Main Window Layout this.setLayout(new GridBagLayout()); c.gridx=0; c.gridy=0; c.gridwidth=1; c.gridheight=1; this.add(createPanel,c); c.gridx=1; c.gridy=0; this.add(randomPanel, c); c.gridx=0; c.gridy=2; this.add(removePanel,c); c.gridx=0; c.gridy=3; c.gridwidth=4; c.gridheight=1; this.add(printPanel,c); } public class createButtonHandler implements ActionListener{
@Override public void actionPerformed(ActionEvent e) { int selected = dice[dieOptions1.getSelectedIndex()]; Die target = new Die(selected); diceBag.add(target); output.setText("A die with "+target.getNumSides()+" has been made"); } } public class randomButtonHandler implements ActionListener{
@Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub Die target =diceBag.grab(); target.roll(); output.setText("You rolled a "+ target.toString()+" " + "You rolled a "+target.getValue()); } } public class removeButtonHandler implements ActionListener{
@Override public void actionPerformed(ActionEvent arg0) { int selected = dice[dieOptions2.getSelectedIndex()]; Die target = new Die(selected); System.out.println(diceBag.countOccurrences(target)); if (diceBag.remove(target)){ output.setText("A die with "+target.getNumSides()+" sides has been removed"); } else{ output.setText("No die with "+ target.getNumSides()+" sides have been found in the bag"); } } } public class printButtonHandler implements ActionListener{
@Override public void actionPerformed(ActionEvent e) { int selected = printOptions.getSelectedIndex(); int occ =0; String base= "There are %d configurations of that die"; if (selected ==0) output.setText(diceBag.toString()); if (selected ==1) output.setText("The dice bag's current capacity is: "+diceBag.getCapacity()); if (selected==2||selected ==3||selected ==4|| selected ==5){ occ= diceBag.countOccurrences(new Die(dice[selected-2])); output.setText(String.format(base,occ)); } } } public static void main(String[] args) { DiceArrayBagTester win = new DiceArrayBagTester(); win.setVisible(true); win.setSize(600,600); win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }
The GUI should display (on demand) the number of occurrences of a particular Die configuration (ask the user for the configuration of interest) The GUI should display (on demand) the number of occurrences of a particular Die configuration (ask the user for the configuration of interest)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