Question
Write a program (RadixSortAnimation.java) that animates the radix sort algorithm. Create an array that consists of 20 random integers from 0 to 999, inclusive. The
Write a program (RadixSortAnimation.java) that animates the radix sort algorithm. Create an array that consists of 20 random integers from 0 to 999, inclusive. The array elements are displayed, as shown in Figure 0. Clicking the Step button causes the program to place a number in a bucket. The number that has just been placed is displayed in red, as shown in Figure 1. Once all the numbers are placed in the buckets, clicking the Step button collects all the numbers from the buckets and moves them back to the array, as shown in Figure 2. When the algorithm is finished, clicking the Step button displays a message to inform the user, as shown in Figure 3. Clicking the Reset button creates a new random array for a new start.
***I have the program written except I need to display a message to the user once the array has been sorted and also display labels at the bottom of each bucket. Just need to modify the code given below.***
Code I have so far is below:
import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.ArrayList;
public class RadixSortAnimation extends JApplet {
private static final long serialVersionUID = 1L; private RadixSortPanel radixSortPanel = new RadixSortPanel(); private JButton b1 = new JButton("Step"); public RadixSortAnimation() { setLayout(new BorderLayout()); add(radixSortPanel, BorderLayout.CENTER); JPanel pane = new JPanel(); pane.add(b1); JButton b2 = new JButton("Reset"); pane.add(b2); add(pane, BorderLayout.SOUTH); b1.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { radixSortPanel.nextStep(); } }); b2.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { radixSortPanel.reset(); b1.setEnabled(true); } }); }
class RadixSortPanel extends JPanel { private static final long serialVersionUID = 1L; private int numInts = 20; private int maxNum = 1000; private int order = 1; private boolean isCycle = false; private int iCycle = 0; private ArrayList
public void nextStep() { if(!isCycle) { bucket = new ArrayList[10]; for (int i = 0; i < bucket.length; i++) { bucket[i] = new java.util.ArrayList<>(); } isCycle = true; iCycle = 0; }
if(isCycle) { bucket[(numbers.get(iCycle) / order) % 10].add(numbers.get(iCycle)); lastNumberCol = (numbers.get(iCycle) / order) % 10; iCycle++; if(iCycle >= numbers.size()) { isCycle = false; } }
if(isCycle==false) { int k = 0; for (int i = 0; i < bucket.length; i++) { if (bucket[i] != null) { for (int j = 0; j < bucket[i].size(); j++) numbers.set(k++, bucket[i].get(j)); } } order *= 10; if(order >= maxNum) { b1.setEnabled(false); order = 1; } bucket = null; } repaint(); } public void reset() { numbers.clear(); for (int i = 1; i <= numInts; i++) { numbers.add((int)(Math.random() * maxNum)); } order = 1; isCycle = false; iCycle = 0; bucket = null; lastNumberCol = 0; repaint(); } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); int numberWidth = getWidth() / (numInts + 2); int numberHeight = 20; for (int i = 0; i < numInts; i++) { g.drawRect(numberWidth * (i + 1), 20, numberWidth, numberHeight); g.drawString(numbers.get(i) + "", numberWidth * (i + 1) + 5, 35); }
int columns = 10; int columnWidth = getWidth() / columns; int columnHeight = getHeight() - 80; for (int index = 0; index < columns; index++) { g.drawRect(columnWidth * index + 10, 60, columnWidth - 20, columnHeight); if(bucket != null) { for (int j = 0; j < bucket[index].size(); j++) { if((index == lastNumberCol)&&(j == bucket[index].size() - 1)) { g.setColor(Color.RED); } g.drawString(bucket[index].get(j) + "", columnWidth * index + 20, 90 + j * 20); if((index == lastNumberCol)&&(j == bucket[index].size() - 1)) { g.setColor(Color.BLACK); } } } } } }
public static void main(String[] args) { JFrame frame = new JFrame("RadixSortAnimation"); RadixSortAnimation applet = new RadixSortAnimation(); frame.add(applet); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(960, 500); frame.setMinimumSize(new Dimension(frame.getWidth(), frame.getHeight())); frame.setLocationRelativeTo(null); frame.setVisible(true); } }
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