Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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:

image text in transcribed

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

image text in transcribed

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 -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 pivot) { j--; } if (i

return i; } //quick sort void quickSort(int arr[], int left, int right) { int index = partition(arr, left, right); if (left 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 0; i--) { array[size - i] = i; } } return array; } public static void print(int[] array) {

for (int i = 0; i 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

Step by Step Solution

There are 3 Steps involved in it

Step: 1

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

More Books

Students also viewed these Databases questions