diff --git a/APCompSci.code-workspace b/APCompSci.code-workspace index 876a149..5413796 100644 --- a/APCompSci.code-workspace +++ b/APCompSci.code-workspace @@ -4,5 +4,6 @@ "path": "." } ], - "settings": {} + "settings": {}, + "java.home": "/usr/lib/jvm/java-14-oracle/" } \ No newline at end of file diff --git a/Sorting/AlgorithmTester.java b/Sorting/AlgorithmTester.java index 9bdb560..85f567f 100644 --- a/Sorting/AlgorithmTester.java +++ b/Sorting/AlgorithmTester.java @@ -12,15 +12,14 @@ public class AlgorithmTester{ final static Scanner sc = new Scanner(System.in); static int input; + static boolean quit = false; public static void main(String[] args) { System.out.println("SORTING ALGORITHM TESTER\n"); - boolean quit = false; while(!quit){ System.out.println("Please select an option:"); System.out.println("1) Make a file of random numbers"); System.out.println("2) Sort a file of random numbers"); System.out.println("0) Exit") - try{ input = sc.nextInt(); //Using the new switch syntax introduced in JDK 13 @@ -28,9 +27,26 @@ public class AlgorithmTester{ switch(input){ case 1 -> makeFile(); case 2 -> sortFile(); - case 3 -> quit=true; + case 0 -> quit=true; + default -> System.out.println("Invalid input"); } - + /* + non-lambda expresstion version looks like this + switch(input){ + case 1: + makeFile(); + break; + case 2: + sortFiler(); + break; + case 0: + quit=true; + break; + default: + System.out.println("Invalid input"); + break; + } + */ } catch(InputMismatchException ex){ System.out.println("Invalid input"); } @@ -38,4 +54,72 @@ public class AlgorithmTester{ sc.close(); } + private static void makeFile(){ + String filename; + int count; + int min; + int max; + try{ + System.out.print("Please enter a file name: "); + filename = sc.next(); + System.out.println("How many random numbers?"); + count = sc.nextInt(); + System.out.println("Minimum value?"); + min = sc.nextInt(); + System.out.println("Maximum value?"); + max = sc.nextInt(); + var randomMaker = new RandomNumberFileMaker(filename, count, min, max); + randomMaker.writeFile(); + } catch(InputMismatchException ex){ + System.out.println("Invalid input"); + } + } + + private static void sortFile(){ + String filename; + int input; + boolean bubble, selection, insertion, quick, counting, radix, merge, ready; + System.out.print("Please enter file name: "); + filename = sc.nextLine(); + while(!ready){ + try{ + System.out.println("Enter the number of the algorithms you wish to use.") + System.out.println("Enter 0 to start sorting."); + if(bubble){ + System.out.print("*"); + } + System.out.println("1) bubble sort"); + if(selection){ + System.out.print("*"); + } + System.out.println("2) selection sort"); + if(insertion){ + System.out.print("*"); + } + System.out.println("3) insertion sort"); + if(counting){ + System.out.print("*"); + } + System.out.println("4) counting sort"); + if(quick){ + System.out.print("*"); + } + System.out.println("5) quick sort"); + input = sc.nextInt(); + switch(input){ + case 1 -> bubble=!bubble; + case 2 -> selection=!selection; + case 3 -> insertion=!insertion; + case 4 -> counting=!counting; + case 5 -> quick=!quick; + case 0 -> ready=true; + default -> System.out.println("Invalid input"); + } + + } catch(InputMismatchException ex){ + System.out.println("Invalid input"); + } + + } + } } \ No newline at end of file diff --git a/Sorting/RandomNumberFileMaker.java b/Sorting/RandomNumberFileMaker.java index 9acfac0..c8009d8 100644 --- a/Sorting/RandomNumberFileMaker.java +++ b/Sorting/RandomNumberFileMaker.java @@ -12,7 +12,19 @@ import java.lang.Math; */ public class RandomNumberFileMaker { - public RandomNumberFileMaker(String filename, int count, int min, int max){ + private String filename; + private int count; + private int min; + private int max; + + public RandomNumberFileMaker(String f, int c, int m, int M){ + filename=f; + count=c; + min=m; + max=M; + } + + public void writeFile(){ try{ File randomNumbers = new File(filename); if(randomNumbers.createNewFile()==false){ @@ -30,7 +42,6 @@ public class RandomNumberFileMaker { }catch (IOException ex){ System.err.println(ex); } - } } \ No newline at end of file