Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Modify the application so that it performs a selection sort or bubble sort. The Clear button's label should be changed to Toggle Sort and the

Modify the application so that it performs a "selection sort" or "bubble sort". The Clear button's label should be changed to Toggle Sort and the action performed when it is clicked should be changed to toggle the type of sort performed.

Note: The status label should indicate the number of swaps for either sort. The Sort button's label should change to indicate the current type of sort ("Selection" or "Bubble").
A button's label can be changed with its setLabel method. e.g.
mybutton.setLabel("Wombat");

public class SortPanel extends JPanel implements ActionListener {
private final int pauseInterval = 10; // ms larger number slower animation
private ArrayList original = new ArrayList<>();
private void bubbleSort() {
showStatus("Sorting ...");
boolean swap = true;
int count=0;
int travel=1;
while (swap) {
swap = false;
for (int i = 0; i
if (greaterThan(items[i + 1], items[i])) {
swapItems(items[i], items[i + 1]);
swap = true;
count++;
}
}//for
travel++;
} // while
showStatus("Sort complete, number of swaps = "+count);
} // bubbleSort
/**
* Method that handles the button presses.
*
* @parame The ActionEvent
*/
@Override
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
switch (command) {
case "Clear" -> {
for (TextField item : items) {
item.setText("");
}
}
case "Sort" -> {
// original.removeAll(original);
for (TextField item : items) {
original.add(item.getText());
}
bubbleSort();
}
case "Reset" -> {
int i=0;
for (TextField item : items) {
item.setText(original.get(i));
i++;
}
//initItems();
}
default -> showStatus("Unrecognised button: " + e);
}
}

...

public class SortDriver {
public static void main(String[] args) {
JFrame frame = new JFrame("Sorting Algorithms");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800, 200);
frame.setResizable(false);
SortPanel panel = new SortPanel();
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
}

Step by Step Solution

3.56 Rating (167 Votes )

There are 3 Steps involved in it

Step: 1

Answer Code package sort1 import javaawt import javaawtevent import javautil import javaxswing public class Sort1 extends JFrame implements ActionListener private final int pauseInterval 100 private A... blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Discrete and Combinatorial Mathematics An Applied Introduction

Authors: Ralph P. Grimaldi

5th edition

201726343, 978-0201726343

More Books

Students also viewed these Programming questions

Question

Explain the Hawthorne effect.

Answered: 1 week ago