JAVA_HA23.0: Radix Sort Animation Write a program ( RadixSortAnimation.java ) th
ID: 3911672 • Letter: J
Question
JAVA_HA23.0: Radix Sort Animation
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 Stepbutton 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 Stepbutton 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 Stepbutton displays a message to inform the user, as shown in Figure 3. Clicking the Resetbutton creates a new random array for a new start.
Submit the source code file - RadixSortAnimation.java- to the drop box HA23.0.
Explanation / Answer
Ask if there are any doubts in the code. Good Luck!!!
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
public class hello extends JApplet {
private static final long serialVersionUID = 1L;
private RadixSortPanel radixSortPanel = new RadixSortPanel();
private JButton jB1 = new JButton("Step");
public hello() {
setLayout(new BorderLayout());
add(radixSortPanel, BorderLayout.CENTER);
JPanel jP1 = new JPanel();
jP1.add(jB1);
JButton jB2 = new JButton("Reset");
jP1.add(jB2);
add(jP1, BorderLayout.SOUTH);
jB1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
radixSortPanel.nextStep();
}
});
jB2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
radixSortPanel.reset();
jB1.setEnabled(true);
}
});
}
class RadixSortPanel extends JPanel {
private static final long serialVersionUID = 1L;
private int searchSize = 30;
private int maxOrder = 1000;
private int order = 1;
private boolean isCycle = false;
private int iCycle = 0;
private ArrayList<Integer>[] bucket;
int lastNumberCol = 0;
ArrayList<Integer> numbers = new ArrayList<>();
public RadixSortPanel() {
reset();
}
@SuppressWarnings("unchecked")
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 >= maxOrder) {
jB1.setEnabled(false);
order = 1;
}
bucket = null;
}
repaint();
}
public void reset() {
numbers.clear();
for (int i = 1; i <= searchSize; i++) {
numbers.add((int)(Math.random() * maxOrder));
}
order = 1;
isCycle = false;
iCycle = 0;
bucket = null;
lastNumberCol = 0;
repaint();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int numberWidth = getWidth() / (searchSize + 2);
int numberHeight = 20;
for (int i = 0; i < searchSize; 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 i = 0; i < columns; i++) {
g.drawRect(columnWidth * i + 10, 60, columnWidth - 20, columnHeight);
if(bucket != null) {
for (int j = 0; j < bucket[i].size(); j++) {
if((i == lastNumberCol)&&(j == bucket[i].size() - 1)) {
g.setColor(Color.RED);
}
g.drawString(bucket[i].get(j) + "", columnWidth * i + 20, 90 + j * 20);
if((i == lastNumberCol)&&(j == bucket[i].size() - 1)) {
g.setColor(Color.BLACK);
}
}
}
}
}
}
public static void main(String[] args) {
JFrame frame = new JFrame("hello");
hello applet = new hello();
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);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.