Question: need help with this javaFx project i have written code below that I need you to fix/edit and make changes too. heres the changes i
need help with this javaFx project
i have written code below that I need you to fix/edit and make changes too. heres the changes i need.
1. need to have the action event say how many milliseconds it took to do the sort.
To do this:
| you need to chunk the array into blocks of the specified block size. So you probably want some kind of list of arrays that stores these chunks. So if the user put they want a sorted array, of size 100, and block size of 11, you should create 10 chunks, the first 9 have size 11, and the last has size 1. You can use something like Arrays.copyOfRange(..) 2. the randomSort doesn't work correctly, its supposed to output the sorted array after the sorting has been done to the console but right now it just sends the array before it sorted to the console. |
my ultimate goal is to have the user select a radio button, for example insertion, and then select random/sorted/reverse and then set the array size to lets say 10 and the block size to say 10 and click go and then it sorts the array out and tells you how many miliseconds it took to sort that while sending the sorted array to the console.
| |
please only make changes/edit/add/remove items to the code given to you
here is my code:
package project4practice;
import java.util.Random; 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 {
@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();
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();
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); selection.setSelected(true); bubble.setToggleGroup(tg); insertion.setToggleGroup(tg); quick.setToggleGroup(tg); ToggleGroup tg1 = new ToggleGroup(); sorted.setToggleGroup(tg1); sorted.setSelected(true); reverse.setToggleGroup(tg1); random.setToggleGroup(tg1);
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) -> { //selection sorted if (selection.isSelected() && sorted.isSelected()) { int arraySize = Integer.parseInt(inputText.getText()); // int chunk = Integer.parseInt(block.getText());//block size user input // for(int i=0;i public static void 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--; } 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 public 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); } //return index; } //bubble sort public static void 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; } } } //return loopCount; } //selection sort public static void selectionSort(int[] arr) { 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; } } int smallerNumber = arr[index]; arr[index] = arr[i]; arr[i] = smallerNumber; } } 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 int[] getReverse(int[] arrayw) { int[] array = new int[arrayw.length]; for (int i = 0,j = array.length-1; i 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); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
