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

Your job in this question is to rewrite the IntArrayBag class so that it is gene

ID: 3532679 • Letter: Y

Question

Your job in this question is to rewrite the IntArrayBag class so that it is generic and can hold any type of object. This means you need to change the data datafield and then correct all of the methods that use it (12 marks). Then, when this is done, write a GUI that allows you to create a bag of dice objects. This means your GUI has the following functionality:

a. Create an empty ArrayBag. It is ok to have it pre-set to use dice as in ArrayBag<Die>.

b. (3 marks) The GUI should have inputs so that you can create dice and add them to the bag. You can use text fields but this might be a good place for radio buttons or a drop down menu since there are only a few valid dice configurations.

c. (3 marks) The GUI should allow you to grab a random die from the bag and roll it. This can be done with a single click

Explanation / Answer

import javax.swing.*;


import java.awt.*;

import java.awt.event.*;


public class GUI extends JPanel implements ActionListener

{

/**

*

*/

private static final long serialVersionUID = 1L;

IntArrayBag<Die> bag=new IntArrayBag<Die>();

JButton jbAdd=new JButton("AddDice"),

grabNRole=new JButton("Grab And Role"),

removeDie=new JButton("Remove a Die"),

printAllDiceFromBag=new JButton("Show Dies from Bag"),

showCurrCap=new JButton("Show Curent Capacity"),

countNo=new JButton("Count");

JLabel addDice=new JLabel("Create and Add Dice");

JLabel rem=new JLabel("Enter value of NumSides:");

JLabel count=new JLabel("Enter value of NumSides to count from the bag:");

JTextField addDiceTF=new JTextField(),

removeDieCfg=new JTextField(),

countTf=new JTextField();

JLabel jlAnswer=new JLabel("Answer");

GUI(){

jbAdd.addActionListener(this);

grabNRole.addActionListener(this);

printAllDiceFromBag.addActionListener(this);

removeDie.addActionListener(this);

showCurrCap.addActionListener(this);

countNo.addActionListener(this);

setLayout(new GridLayout(10, 4));

add(addDice);add(addDiceTF);

add(jbAdd);add(new JLabel());

add(grabNRole);add(new JLabel());

add(rem);add(removeDieCfg);

add(removeDie);add(new JLabel());

add(count);add(countTf);

add(countNo);add(new JLabel());

add(printAllDiceFromBag);add(showCurrCap);



}

public void actionPerformed(ActionEvent ae){

if(ae.getSource()==jbAdd){

try{

int die=Integer.parseInt(addDiceTF.getText());

//create Dice and add to the bag

bag.add(new Die(die));

JOptionPane.showMessageDialog(this, "Dice Added with Parameter = "+die);

}catch(Exception e){e.printStackTrace();


}

}else if(ae.getSource()==grabNRole){

try{

Die die=(Die)bag.grab();

JOptionPane.showMessageDialog(this, "Dice Grabbed = "+die);

}catch(Exception e){e.printStackTrace();}

}

else if(ae.getSource()==removeDie){

try

{

System.out.println("here");

int num=Integer.parseInt(removeDieCfg.getText());

bag.remove(num);

JOptionPane.showMessageDialog(this, "Dice Removed with Configuration = "+num);

}catch(Exception e){e.printStackTrace();


}

}

else if(ae.getSource()==printAllDiceFromBag){

try

{

JOptionPane.showMessageDialog(this, bag.toString());

}catch(Exception e){e.printStackTrace();


}

}

else if(ae.getSource()==countNo){

try

{

JOptionPane.showMessageDialog(this, "No of Occurrence : "+bag.countOccurrences(Integer.parseInt(countTf.getText())));

}catch(Exception e){e.printStackTrace();


}

}

else if(ae.getSource()==showCurrCap){

try

{

JOptionPane.showMessageDialog(this, "No of Dies : "+bag.size()+" " +

"Capacity : "+bag.getCapacity());

}catch(Exception e){e.printStackTrace();


}

}

else

{System.out.println("kjsd");}



}

public static void main(String[] args)

{

JFrame jf=new JFrame();

jf.setDefaultCloseOperation

(JFrame.EXIT_ON_CLOSE);

GUI s=new GUI();

jf.add(s);

jf.pack();

jf.setVisible(true);



}


}