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

need help with this java project: what exactly i need help with: 1. I need help

ID: 3817547 • Letter: N

Question

need help with this java project:

what exactly i need help with:

1. I need help with event handlers/listeners for the radio buttons. what i mean by this is that my program right now has 2 sets of radio buttons, 4 buttons that allow the user to select which style of sorting they want to do (insertion,quick,bubble,and selection) i need an action event that shows which is selected by the user so that it can call to that method of sorting and sort the array properly.

2. i need help with the event handlers for another set of radio buttons saying what the array should start as, for example is it random, sorted, or reverse sorted.

3. Need to get the size of the array from the user and have it work with step 1 and 2, because the size of the array will be involved with what kind of sorting needs done and if its in reverse or sorted or random. also this array needs to print to the console.

4. lastly I need help with the 'go button' it needs to take into account which radio button is selected and then what kind of array(random/sortedetc)

and then the numbers/block size from the user and when you click go it does the sort.

ILL add the directions directly from the professor in case of any confusion: here they are below.

my code that I have so far will be at the bottom, please copy it and edit it! :)

directions from professor:

Your task is to write a program that allows the user to experiment with different multithreaded sort algorithms. You should provide the user with an interface that looks very similar to the one shown below.

The user should be able to select the type of sorting algorithm to be used (selection, bubble, insertion, or quick), the size of the array to sort, the type of values to put in the array (already sorted in ascending order, sorted in reverse (descending) order, or random numbers between 0 and 99), and the number of values to sort in each thread. When the user clicks on the Go button, you should validate all of the input.

If the user has not provided required input or if the input is invalid (a negative input size, for example), you should display an alert dialog explaining the problem. Otherwise, collect all of the input and generate an appropriate array of integer values.

Then, “chunk” the array into the desired block size and pass each chunk to a thread that sorts the chunk into ascending order using the algorithm chosen by the user. When all of the chunks have been sorted, put the resulting sorted integer arrays into a queue. Then, poll two sorted chunks at a time from the queue, spawn a thread to merge them, and push the new merged array back onto the queue.

Repeat this process until there is only a single integer array in the queue. This array should now be sorted.

Display the sorted array to the console, and use an alert dialog to show the user how many milliseconds the sort process took.

example of how the gui should look:

example of alert from gui:********* need help making this!!!*!*

example of the output to the console is this: [0, 2, 2, 2, 3, 5, 5, 6, 7, 8, 9, 9, 13, 13, 15, 16, 17, 17, 19, 21, 23, 24, 25, 26, 27, 27, 32, 32, 32, 32, 33, 34, 35, 35, 36, 36, 37, 38, 38, 38, 39, 39, 41, 41, 43, 44, 45, 45, 46, 48, 48, 49, 50, 50, 53, 53, 54, 54, 54, 55, 55, 55, 60, 61, 62, 62, 63, 63, 63, 63, 65, 66, 68, 69, 69, 69, 72, 74, 74, 76, 76, 76, 78, 79, 80, 80, 81, 82, 82, 85, 85, 86, 87, 88, 89, 92, 95, 98, 98, 99]

Your program will be graded according to this rubric (each item is worth one point)- rubric from professor:

• The program displays a user interface with the requested layout

• Pressing the Go button validates the user input and displays an alert dialog if necessary • The program correctly implements selection, bubble, insertion, and quick sort

• The program correctly implements the merge method

• The program spawns threads to sort chunks of the array with the requested size and using the requested algorithm

• The program spawns threads to merge the sorted chunks

• The program displays the number of milliseconds the sorting operation took via an alert dialog

my code:

package project4practice;
import java.util.Random;
import java.util.Scanner;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextField;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;

public class Project4practice extends Application {
private static Scanner in = new Scanner(System.in);
@Override
public void start(Stage primaryStage) {
BorderPane rootPane = new BorderPane();
GridPane gp = new GridPane();
rootPane.setCenter(gp);
gp.setVgap(5);
gp.setHgap(5);
rootPane.prefWidth(700);
rootPane.prefHeight(400);
gp.prefWidth(400);
gp.prefHeight(400);
Label sort = new Label(" Sorting Algorithm ");
RadioButton selection = new RadioButton("selection ");
RadioButton bubble = new RadioButton("bubble ");
RadioButton insertion = new RadioButton("insertion");
RadioButton quick = new RadioButton("Quick ");
Label inputType = new Label(" Input Type ");
RadioButton sorted = new RadioButton("Already Sorted ");
RadioButton reverse = new RadioButton("Reverse ");
RadioButton random = new RadioButton("Random ");
Label inputSize = new Label(" Input Size: ");
TextField inputText = new TextField();
//String inputText1 = inputText.getText();
// System.out.println(inputText1);
inputText.setOnAction((ActionEvent inputText1) -> {
String inputText2 = inputText.getText();
double inputText3 = Double.parseDouble(inputText2);
System.out.println(inputText3);
});
Label blockSize = new Label(" Block Size: ");
TextField block = new TextField();
// String block1 = block.getText();
// System.out.println(block1);
block.setOnAction((ActionEvent block1)-> {
String block2 = block.getText();
double block3 = Double.parseDouble(block2);
System.out.println(block3);
});
  
Button go = new Button("Go ");
ToggleGroup tg = new ToggleGroup();
selection.setToggleGroup(tg);
bubble.setToggleGroup(tg);
insertion.setToggleGroup(tg);
quick.setToggleGroup(tg);
ToggleGroup tg1 = new ToggleGroup();
sorted.setToggleGroup(tg1);
reverse.setToggleGroup(tg1);
random.setToggleGroup(tg1);
//selection sort action event
selection.setOnAction((ActionEvent selection1)->{
int[] array = getSorted(100,true);
System.out.println(array);
});
//bubble sort action event
bubble.setOnAction((ActionEvent bubble1)->{
  
});
//insertion sort action event
insertion.setOnAction((ActionEvent insertion1)->{
  
});
//quick sort action event
quick.setOnAction((ActionEvent quick1)-> {
  
});
gp.add(sort, 0, 0);
gp.add(selection, 0, 1);
gp.add(bubble, 0, 2);
gp.add(insertion, 0, 3);
gp.add(quick, 0, 4);
gp.add(inputType, 0, 7);
gp.add(sorted, 0, 8);
gp.add(reverse, 0, 9);
gp.add(random, 0, 10);
gp.add(inputSize, 0, 12);
gp.add(inputText, 1, 12);
gp.add(blockSize, 0, 13);
gp.add(block, 1, 13);
gp.add(go, 0, 16);
  
go.setOnAction((ActionEvent go1) -> {
  
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("Thread Sorted!");
alert.setHeaderText("Finished");
alert.setContentText("Sort completed in milliseconds ");
alert.showAndWait();
// int array[] = getSorted(10,true);
});
Scene scene = new Scene(rootPane, 500, 350);
primaryStage.setTitle("Project 4");
primaryStage.setScene(scene);
primaryStage.show();
}
//insertion sort
public static int insertionSort(int array[]) {
int loopCount = 0;
int n = array.length;
for (int j = 1; j < n; j++) {
int key = array[j];
int i = j - 1;
while ((i > -1) && (array[i] > key)) {
array[i + 1] = array[i];
i--;
loopCount++;
}
array[i + 1] = key;
}
return loopCount;
}
//quick sort
int partition(int arr[], int left, int right) {
int i = left, j = right;
int tmp;
int pivot = arr[(left + right) / 2];

while (i <= j) {
while (arr[i] < pivot) {
i++;
}
while (arr[j] > pivot) {
j--;
}
if (i <= j) {
tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
i++;
j--;
}
};

return i;
}
//quick sort
void quickSort(int arr[], int left, int right) {
int index = partition(arr, left, right);
if (left < index - 1) {
quickSort(arr, left, index - 1);
}
if (index < right) {
quickSort(arr, index, right);
}
}
//bubble sort
public static int bubbleSort(int[] arr) {
int n = arr.length;
int loopCount = 0;
int temp = 0;
for (int i = 0; i < n; i++) {
for (int j = 1; j < (n - i); j++) {
if (arr[j - 1] > arr[j]) {
temp = arr[j - 1];
arr[j - 1] = arr[j];
arr[j] = temp;
}
loopCount++;
}
}
return loopCount;
}
//selection sort
public static int selectionSort(int[] arr) {
int loopCount = 0;
for (int i = 0; i < arr.length - 1; i++) {
int index = i;
for (int j = i + 1; j < arr.length; j++) {
if (arr[j] < arr[index]) {
index = j;
}
loopCount++;
}
int smallerNumber = arr[index];
arr[index] = arr[i];
arr[i] = smallerNumber;
}
return loopCount;
}
public static int[] getRandom(int size) {
Random rand = new Random();
int[] array = new int[size];
for (int i = 1; i <= size; i++) {
array[i - 1] = Math.abs(rand.nextInt()) % 100;
}
return array;
}
public static int[] getSorted(int size, boolean accending) {
int[] array = new int[size];
if (accending) {
for (int i = 1; i <= size; i++) {
array[i - 1] = i;
}
} else {
for (int i = size; i > 0; i--) {
array[size - i] = i;
}
}
return array;
}
public static void print(int[] array) {

for (int i = 0; i < array.length; i++) {
System.out.print(" " + array[i]);
}
System.out.println();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}

Project 4 Sorting Algorithm Selection Bubble Insertion Quick Input Type Already sorted Reverse order Random Input Size 100 Block Size 10 Go The output to the console is this:

Explanation / Answer

import java.awt.BorderLayout; import java.awt.Component; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JCheckBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JPanel; import javax.swing.JTextField; public class Sorter extends JPanel implements ActionListener { private static final long serialVersionUID = -5548803322850070957L; private static final String BUBBLE_SORT = "bubble sort"; private static final String INSERTION_SORT = "insertion sort"; private static final String QUICK_SORT = "quick 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 BUBBLE = new JMenuItem(BUBBLE_SORT); private static final JMenuItem INSERTION = new JMenuItem(INSERTION_SORT); private static final JMenuItem QUICK_S = new JMenuItem(QUICK_SORT); private final JTextField textfieldInput = new JTextField(); private final JCheckBox checkbox = new JCheckBox(); private final JLabel orderLabel = new JLabel("sort in ascending order"); private final JLabel labRes = new JLabel("sorted result : "); private final JLabel res = new JLabel(NO_RESULT); private static String selectedMenu; public Sorter() { setLayout(new GridLayout(2, 1)); textfieldInput.setPreferredSize(TEXT_INPUT_DIM); JPanel top = new JPanel(); top.setLayout(new FlowLayout(10)); top.add(textfieldInput); top.add(createPanelCheckbox()); top.add(createSortButton()); add(top); add(createPanelResult()); } private Component createSortButton() { JButton sortButton = new JButton("Launch"); sortButton.addActionListener(this); return sortButton; } private final JPanel createPanelResult() { JPanel p = new JPanel(); p.setLayout(new BorderLayout()); p.add(labRes, BorderLayout.WEST); p.add(res, BorderLayout.CENTER); return p; } private final JPanel createPanelCheckbox() { JPanel p = new JPanel(); p.setLayout(new FlowLayout(10)); p.add(checkbox); p.add(orderLabel); return p; } public static void main(String[] args) { JFrame f = new JFrame(); f.setSize(500, 300); f.setLocationRelativeTo(null); f.add(new Sorter()); f.setJMenuBar(createMenu()); f.setVisible(true); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } private static JMenuBar createMenu() { BUBBLE.addActionListener(new MenuItemListener()); SORT.add(BUBBLE); INSERTION.addActionListener(new MenuItemListener()); SORT.add(INSERTION); QUICK_S.addActionListener(new MenuItemListener()); SORT.add(QUICK_S); 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 content = textfieldInput.getText(); String[] parsedNumbers = content.split(SEPARATOR); if (isSortable(parsedNumbers) && parsedNumbers.length < 10) { int[] result = null; if(selectedMenu==null) selectedMenu = BUBBLE_SORT; switch (selectedMenu) { case BUBBLE_SORT: result = bubbleSort(checkbox.isSelected()); break; case INSERTION_SORT: result = insertionSort(checkbox.isSelected()); break; case QUICK_SORT: result = quickSort(checkbox.isSelected()); break; } res.setText(result == null ? NO_RESULT : fillResultLabel(result)); } } private String fillResultLabel(int[] result) { StringBuilder res = new StringBuilder(); for (int nb : result) { res.append(nb + " "); } return res.toString(); } private final boolean isSortable(String[] numbers) { return true; } private int[] bubbleSort(boolean sortAscending) { return new int[]{1,2,3,4}; } private int[] insertionSort(boolean sortAscending) { return new int[]{5,6,7,8}; } private int[] quickSort(boolean sortAscending) { return new int[]{9,10,11,12}; } }