From 37ad93f37048386cfcd798becab018ccc21522a1 Mon Sep 17 00:00:00 2001 From: eugenefischer Date: Sat, 30 May 2020 11:33:43 -0500 Subject: [PATCH] Writing body of AlgorithmTester and RandomNumberFileMaker --- Sorting/AlgorithmTester.java | 28 +++++++++++++++++++++++++++- Sorting/RandomNumberFileMaker.java | 26 ++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/Sorting/AlgorithmTester.java b/Sorting/AlgorithmTester.java index e5291ea..9bdb560 100644 --- a/Sorting/AlgorithmTester.java +++ b/Sorting/AlgorithmTester.java @@ -1,5 +1,7 @@ package Sorting; +import java.util.Scanner; +import java.util.InputMismatchException; /** * This class contains the main method and presents an interface for the user to * compare sorting algorithm efficiencies. The user will be able to generate files @@ -8,8 +10,32 @@ package Sorting; */ public class AlgorithmTester{ + final static Scanner sc = new Scanner(System.in); + static int input; 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 + //this is a switch expression (lambda expression) rather than a switch statement + switch(input){ + case 1 -> makeFile(); + case 2 -> sortFile(); + case 3 -> quit=true; + } + + } catch(InputMismatchException ex){ + System.out.println("Invalid input"); + } + } + sc.close(); } } \ No newline at end of file diff --git a/Sorting/RandomNumberFileMaker.java b/Sorting/RandomNumberFileMaker.java index 0d7fe22..9acfac0 100644 --- a/Sorting/RandomNumberFileMaker.java +++ b/Sorting/RandomNumberFileMaker.java @@ -1,5 +1,10 @@ package Sorting; +import java.io.File; +import java.io.IOException; +import java.io.FileWriter; +import java.lang.Math; + /** * This class will allow the user to make a file of random integers. * The user will be able to select a file name, how many numbers are in the file, @@ -7,4 +12,25 @@ package Sorting; */ public class RandomNumberFileMaker { + public RandomNumberFileMaker(String filename, int count, int min, int max){ + try{ + File randomNumbers = new File(filename); + if(randomNumbers.createNewFile()==false){ + System.out.println("File already exists."); + } + else{ + FileWriter randomWriter = new FileWriter(randomNumbers); + for(int i=0;i