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

Java Processing: Purpose: Demonstrate the ability to implement a simple sorting

ID: 3822121 • Letter: J

Question

Java Processing:

Purpose: Demonstrate the ability to implement a simple sorting routine. Demonstrate the ability to implement a graphical user interface that displays the intermediate steps involved in the sort.

Assignment: This is a fresh project that is not based on any previous homework. You should start with a new empty project. You are to implement the bubble sort:

1. You must start out by creating an array that has length N (start out with N=6). You will populate the array with random positive values between 5 and 100.

2. You will change the fill color to green and you will display N circles in a row with each circle holding the value of its associated slot in the array.

3. You will change the fill color to yellow.

4. You will call your bubble sort routine. This routine makes 1 pass through the array and swaps values as with the bubble sort. It returns true if it swapped any values and false otherwise.

a. While the function returns true, you will move down to start a new row and you will display N circles in a row with each circle holding the value of its associated slot in the array.

b. If the function returns false, you will exit the loop

5. When done, the last row of circles displayed should be in sorted order.

Homework5 84 13 55 44 43 96 13 55 44 43 84 96 13 44 43 55 84 96 13 43 44 55 84 96

Explanation / Answer

import java.awt.*;

import javax.swing.*;

public class bbl_sort extends JPanel implements ActionListener {
private static final long sv_ID = -5548803322850070957L;
private static final String bbl_sort = "bbl sort";
private static final String SEPARATOR = ",";
private static final String NO_RESULT = "no result";

private static final Dimension TEXT_INPUT_DIM = new Dimension(200, 30);

private static final JMenuBar menu = new JMenuBar();
private static final JMenu sort = new JMenu("sorting Algorithms");
private static final JMenuItem bbl = new JMenuItem(bbl_sort);
  

private final JTextField txtIp = new JTextField();
private final Jchkbox chkbox = new Jchkbox();
private final JLabel ord_lbl = new JLabel("sort in ascending order");

private final JLabel lab_no_result = new JLabel("sorted no_result : ");
private final JLabel no_res = new JLabel(NO_no_resULT);

private static String selectedMenu;

public bbl_sort() {
setLayout(new GridLayout(2, 1));
txtIp.setPreferredSize(TEXT_INPUT_DIM);

JPanel suptp = new JPanel();
suptp.setLayout(new FlowLayout(10));
suptp.add(txtIp);
suptp.add(createPanelchkbox());
suptp.add(createsBtn());
add(suptp);

add(createPanelno_result());
}

private Component createsBtn() {
JButton sBtn = new JButton("Launch");
sBtn.addActionListener(this);
return sBtn;
}

private final JPanel Panel_res() {
JPanel jp1 = new JPanel();
jp1.setLayout(new BorderLayout());
jp1.add(lab_no_result, BorderLayout.WEST);
jp1.add(no_res, BorderLayout.CENTER);
return jp1;
}

private final JPanel createPanelchkbox() {
JPanel jp1 = new JPanel();
jp1.setLayout(new FlowLayout(10));
jp1.add(chkbox);
jp1.add(ord_lbl);
return jp1;
}

public static void main(String[] args) {
JFrame jf1 = new JFrame();
jf1.setSize(500, 300);
jf1.setLocationRelativeTo(null);
jf1.add(new bbl_sort());
jf1.setJMenuBar(createMenu());
jf1.setVisible(true);
jf1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

private static JMenuBar createMenu() {
bbl.addActionListener(new MenuItemListener());
sort.add(bbl);
menu.add(sort);
return menu;
}

static class MenuItemListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
selectedMenu = e.getActionCommand();
}
}

@Override
public void actionPerformed(ActionEvent e) {
String data = txtIp.getText();
String[] par_no = data.split(SEPARATOR);
if (dbl_check(par_no) && par_no.length < 10) {
int[] no_res = null;

if(selectedMenu==null)
selectedMenu = bbl_sort;

switch (selectedMenu) {
case bbl_sort:
no_res = bblsort(chkbox.isSelected());
break;

}
no_res.setText(no_res == null ? NO_no_resULT : fillno_resultLabel(no_res));
}
}

private String fillno_resultLabel(int[] no_res) {
StringBuilder no_res = new StringBuilder();
for (int nb : no_res) {
no_res.append(nb + " ");
}
return no_res.toString();
}

private final boolean dbl_check(String[] numbers) {
return true;
}

private int[] bblsort(boolean srt_ascen) {
return new int[]{18,2,32,4};
}

private int[] insertionsort(boolean srt_ascen) {
return new int[]{5,63,7,28};
}

private int[] quicksort(boolean srt_ascen) {
return new int[]{9,102,11,132};
}
}

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