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

Use Java 5 to create a generic class DrawingBox with a type parameter that simul

ID: 441714 • Letter: U

Question

Use Java 5 to create a generic class DrawingBox with a type parameter that simulates drawing an item at random out of a box. This class could be used for simulating a random drawing. For example, the box might contain Strings representing names written on a slip of paper, or the box might contain Integers representing a random drawing for a lottery based on numeric lottery picks. Create an add method that allows the user of the class to add an object of the specified type along with an isEmpty method that determines whether or not the box is empty. Finally, your class should have a drawItem method that randomly selects an object from the box and returns it. If the user attempts to draw an item out of an empty box, return null. Write a main method that tests your class. To generate a random number x, where 0  x  1, use x = Math.random();. The size of the box should be 100. 2

Explanation / Answer

box.add(new CustomComponents12()); box.add(Box.createVerticalGlue()); add(box); pack(); setTitle("Custom Component Test / BoxLayout"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setMaximumSize(getMinimumSize()); setMinimumSize(getMinimumSize()); setPreferredSize(getPreferredSize()); setLocation(150, 150); setVisible(true); } public static void main(String[] args) { Runnable r = new Runnable() { @Override public void run() { CustomComponent12 main = new CustomComponent12(); } }; javax.swing.SwingUtilities.invokeLater(r); } } class CustomComponents12 extends JPanel { private static final long serialVersionUID = 1L; @Override public Dimension getMinimumSize() { return new Dimension(100, 100); } @Override public Dimension getMaximumSize() { return new Dimension(100, 100); } @Override public Dimension getPreferredSize() { return new Dimension(100, 100); } @Override public void paintComponent(Graphics g) { int margin = 10; Dimension dim = getSize(); super.paintComponent(g); g.setColor(Color.red); g.fillRect(margin, margin, dim.width - margin * 2, dim.height - margin * 2); } }

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