implemented making of random number files in AlgorithmTester, started sorting code
This commit is contained in:
@@ -4,5 +4,6 @@
|
|||||||
"path": "."
|
"path": "."
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"settings": {}
|
"settings": {},
|
||||||
|
"java.home": "/usr/lib/jvm/java-14-oracle/"
|
||||||
}
|
}
|
||||||
@@ -12,15 +12,14 @@ public class AlgorithmTester{
|
|||||||
|
|
||||||
final static Scanner sc = new Scanner(System.in);
|
final static Scanner sc = new Scanner(System.in);
|
||||||
static int input;
|
static int input;
|
||||||
|
static boolean quit = false;
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
System.out.println("SORTING ALGORITHM TESTER\n");
|
System.out.println("SORTING ALGORITHM TESTER\n");
|
||||||
boolean quit = false;
|
|
||||||
while(!quit){
|
while(!quit){
|
||||||
System.out.println("Please select an option:");
|
System.out.println("Please select an option:");
|
||||||
System.out.println("1) Make a file of random numbers");
|
System.out.println("1) Make a file of random numbers");
|
||||||
System.out.println("2) Sort a file of random numbers");
|
System.out.println("2) Sort a file of random numbers");
|
||||||
System.out.println("0) Exit")
|
System.out.println("0) Exit")
|
||||||
|
|
||||||
try{
|
try{
|
||||||
input = sc.nextInt();
|
input = sc.nextInt();
|
||||||
//Using the new switch syntax introduced in JDK 13
|
//Using the new switch syntax introduced in JDK 13
|
||||||
@@ -28,9 +27,26 @@ public class AlgorithmTester{
|
|||||||
switch(input){
|
switch(input){
|
||||||
case 1 -> makeFile();
|
case 1 -> makeFile();
|
||||||
case 2 -> sortFile();
|
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){
|
} catch(InputMismatchException ex){
|
||||||
System.out.println("Invalid input");
|
System.out.println("Invalid input");
|
||||||
}
|
}
|
||||||
@@ -38,4 +54,72 @@ public class AlgorithmTester{
|
|||||||
sc.close();
|
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");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -12,7 +12,19 @@ import java.lang.Math;
|
|||||||
*/
|
*/
|
||||||
public class RandomNumberFileMaker {
|
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{
|
try{
|
||||||
File randomNumbers = new File(filename);
|
File randomNumbers = new File(filename);
|
||||||
if(randomNumbers.createNewFile()==false){
|
if(randomNumbers.createNewFile()==false){
|
||||||
@@ -30,7 +42,6 @@ public class RandomNumberFileMaker {
|
|||||||
}catch (IOException ex){
|
}catch (IOException ex){
|
||||||
System.err.println(ex);
|
System.err.println(ex);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user