need help with this java project: what exactly i need help with: 1. I need help
ID: 3817611 • 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);
}
}
Explanation / Answer
open class BinTree {
BNode theBTRootNode;
open BinTree()/constructor
{
theBTRootNode = invalid;
}
/ - - Addition of the hub to the BST - -
secured BNode insertAB(BNode theRootNode, BNode myNewNode) {
in the event that (theRootNode == invalid) {
theRootNode = myNewNode;
/checks if the username is littler than
/the root protest, if littler adds to one side
} else if (myNewNode.anyClass.surname.compareTo(
theRootNode.anyClass.surname) < 0) {
theRootNode.leftBNode = insertAB(theRootNode.leftBNode, myNewNode);
} else {
/else if greater annexes to one side
theRootNode.rightBNode =
insertAB(theRootNode.rightBNode, myNewNode);
}
return theRootNode;
}
open void insertBST(AnyClass anyClass) {
BNode anyClassBTNode = new BNode(anyClass);
/calls embed previously
theBTRootNode = insertAB(theBTRootNode, anyClassBTNode);
}
/ - - InOrder traversal - -
ensured void inorder(BNode theRootNode) {
on the off chance that (theRootNode != invalid) {
inorder(theRootNode.leftBNode);
theRootNode.show();
inorder(theRootNode.rightBNode);
}
}
/calls the technique to do all together
open void inorderBST() {
inorder(theBTRootNode);
}
/ - Search for key name and returns ref.
/to BNode or invalid if not found -
ensured BNode search(BNode theRootNode, String keyName) {
/if the root is invalid returns invalid
on the off chance that (theRootNode == invalid) {
return invalid;
} else {
/checks on the off chance that they are equivalent
on the off chance that (keyName.compareTo(theRootNode.anyClass.surname) == 0) {
return theRootNode;
/checks id the key is littler than the current
/record if littler navigates to one side
} else if (keyName.compareTo(theRootNode.anyClass.surname) < 0) {
return search(theRootNode.leftBNode, keyName);
} else {
/if greater navigates to one side
return search(theRootNode.rightBNode, keyName);
}
}
}
/returns invalid if no outcome else returns
/the AnyClass question coordinated with the keyName
open AnyClass searchBST(String keyName) {
BNode temp = search(theBTRootNode, keyName);
in the event that (temp == invalid) {
/noresults found
return invalid;
} else {
/result found
return temp.anyClass;
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.