From a5b6cc57f4ac442643c26b3f2e2cf6f647918a5e Mon Sep 17 00:00:00 2001 From: Ye-Eun Kim Date: Thu, 20 Oct 2016 21:27:18 -0400 Subject: [PATCH 1/2] Fixed the spelling of 'quadradic' and added feature to check validity of inputted name of algorithm --- src/edu/allegheny/expose/BigOh.java | 6 ++--- src/edu/allegheny/expose/ComplexityClass.java | 2 +- src/edu/allegheny/expose/ReverseEngineer.java | 24 +++++++++---------- .../allegheny/expose/ReverseEngineerTest.java | 4 ++-- .../examples/sort/SortingExperiment.java | 12 ++++++++++ .../sort/SortingExperimentRepeated.java | 6 ++--- .../examples/unique/UniqueExperiment.java | 12 ++++++++++ 7 files changed, 44 insertions(+), 22 deletions(-) diff --git a/src/edu/allegheny/expose/BigOh.java b/src/edu/allegheny/expose/BigOh.java index 58cc662..35bfb95 100644 --- a/src/edu/allegheny/expose/BigOh.java +++ b/src/edu/allegheny/expose/BigOh.java @@ -27,7 +27,7 @@ public void setCompClass(ComplexityClass compClass) { this.compClass = compClass; - if (compClass == ComplexityClass.QUADRADIC) + if (compClass == ComplexityClass.QUADRATIC) // changed "QUADRADIC"to "QUADRATIC" exponent = 2; else if (compClass == ComplexityClass.CUBIC) exponent = 3; @@ -70,7 +70,7 @@ public String toString(){ else return compClass.toString(); } - + public boolean equals(BigOh o){ if (o.compClass == this.compClass && o.exponent == this.exponent){ return true; @@ -80,5 +80,3 @@ public boolean equals(BigOh o){ } } - - diff --git a/src/edu/allegheny/expose/ComplexityClass.java b/src/edu/allegheny/expose/ComplexityClass.java index 81d2487..d3e28d7 100644 --- a/src/edu/allegheny/expose/ComplexityClass.java +++ b/src/edu/allegheny/expose/ComplexityClass.java @@ -6,7 +6,7 @@ public enum ComplexityClass{ LOGARITHMIC, LINEAR, LINEARITHMIC, - QUADRADIC, + QUADRATIC, // changed "QUADRADIC"to "QUADRATIC" CUBIC, POWER, EXPONENTIAL; diff --git a/src/edu/allegheny/expose/ReverseEngineer.java b/src/edu/allegheny/expose/ReverseEngineer.java index 182284f..7069db5 100644 --- a/src/edu/allegheny/expose/ReverseEngineer.java +++ b/src/edu/allegheny/expose/ReverseEngineer.java @@ -14,14 +14,14 @@ public class ReverseEngineer{ * It will have already been sorted by times doubled */ private ExperimentResults data; - + /** * Data aggregated by times doubled */ private List avg; /** - * Percentage that min / max of a dataset must be within to be considered + * Percentage that min / max of a dataset must be within to be considered * consistant */ private static double spreadTolerance = .10; @@ -75,7 +75,7 @@ public void loadData(String fileName){ data.readCSV(fileName); } - + /** * Analyze the results of a doubling experiment. * @return BigOh determined from data @@ -105,7 +105,7 @@ public BigOh analyzeData(){ BigOh result = new BigOh(); - // now in the case of N^0 or N^1, get more specific + // now in the case of N^0 or N^1, get more specific if ( exp == 0 ){ // check for constant time if (checkConstant()){ @@ -120,7 +120,7 @@ public BigOh analyzeData(){ result.setCompClass(ComplexityClass.LINEARITHMIC); } }else if (exp == 2){ - result.setCompClass(ComplexityClass.QUADRADIC); + result.setCompClass(ComplexityClass.QUADRATIC); // changed "QUADRADIC"to "QUADRATIC" }else if (exp == 3){ result.setCompClass(ComplexityClass.CUBIC); }else{ @@ -167,7 +167,7 @@ protected boolean checkWithinRange(double[] input, double tolerance){ * @return true if the data is deemed O(n) */ protected boolean checkConstant(){ - + aggregate(); // fetch last 3 elements @@ -182,7 +182,7 @@ protected boolean checkConstant(){ * @return true if the data is deemed O(nlog(n)) */ protected boolean checkLinear(){ - + aggregate(); // fetch last 3 elements @@ -191,13 +191,13 @@ protected boolean checkLinear(){ // divide by n double[] div = {last[0] / 4, last[1] / 2, last[2] }; - // check to see that these values are within tolerance of each other + // check to see that these values are within tolerance of each other return checkWithinRange(div, spreadTolerance); } - - - - + + + + } diff --git a/src/edu/allegheny/expose/ReverseEngineerTest.java b/src/edu/allegheny/expose/ReverseEngineerTest.java index 6700a21..bed8f81 100644 --- a/src/edu/allegheny/expose/ReverseEngineerTest.java +++ b/src/edu/allegheny/expose/ReverseEngineerTest.java @@ -15,7 +15,7 @@ public void testWBubble(){ eng.loadData("tests/bubblesort.csv"); BigOh res = eng.analyzeData(); ComplexityClass ans = res.getCompClass(); - Assert.assertEquals(ans, ComplexityClass.QUADRADIC); + Assert.assertEquals(ans, ComplexityClass.QUADRATIC); // changed "QUADRADIC"to "QUADRATIC" } @@ -55,5 +55,5 @@ public void testWLinear(){ Assert.assertEquals(ans, ComplexityClass.LINEAR); } - + } diff --git a/src/edu/allegheny/expose/examples/sort/SortingExperiment.java b/src/edu/allegheny/expose/examples/sort/SortingExperiment.java index 3fb3c29..fe93d4e 100644 --- a/src/edu/allegheny/expose/examples/sort/SortingExperiment.java +++ b/src/edu/allegheny/expose/examples/sort/SortingExperiment.java @@ -12,6 +12,7 @@ public class SortingExperiment extends DoublingExperiment{ protected int alg; protected String name; private int[] n; + private static boolean validAlg; // to check validity of algorithm name input public static String[] algs = {"quick", "insertion", "merge", "selection", "bubble"}; @@ -25,6 +26,17 @@ public static void main(String[] args){ SortingExperiment exp = new SortingExperiment(nargs); exp.name = args[0]; + validAlg = false; // input assumed to be invalid algorithm + for (int i = 0; i < algs.length; i++) { // for each valid algorithm option... + if (algs[i].equals(exp.name)) { // check if input equals valid algorithm option + validAlg = true; // true when input matches at least one valid algorithm + } + } + if (!validAlg) { // if input is invalid... + System.out.println("Sorry, that is not a valid \"Sorting\" algorithm."); + return; // exit main method + } + switch (exp.name){ case "quick": exp.alg = 1; diff --git a/src/edu/allegheny/expose/examples/sort/SortingExperimentRepeated.java b/src/edu/allegheny/expose/examples/sort/SortingExperimentRepeated.java index 3e7d1cd..510b62d 100644 --- a/src/edu/allegheny/expose/examples/sort/SortingExperimentRepeated.java +++ b/src/edu/allegheny/expose/examples/sort/SortingExperimentRepeated.java @@ -7,17 +7,17 @@ public class SortingExperimentRepeated{ - private static final BigOh quadratic = new BigOh(ComplexityClass.QUADRADIC); + private static final BigOh quadratic = new BigOh(ComplexityClass.QUADRATIC); // changed "QUADRADIC"to "QUADRATIC" private static final BigOh linearithmic = new BigOh(ComplexityClass.LINEARITHMIC); public static void main(String[] args){ - + int experiments = 0; int correct = 0; for (int count = 0; count < Integer.parseInt(args[0]); count++){ - + for (String a : SortingExperiment.algs) { BigOh ans = SortingExperiment.doubleExp(a); diff --git a/src/edu/allegheny/expose/examples/unique/UniqueExperiment.java b/src/edu/allegheny/expose/examples/unique/UniqueExperiment.java index edf591d..d10b9dd 100644 --- a/src/edu/allegheny/expose/examples/unique/UniqueExperiment.java +++ b/src/edu/allegheny/expose/examples/unique/UniqueExperiment.java @@ -12,6 +12,7 @@ public class UniqueExperiment extends DoublingExperiment { protected int alg; protected String name; private int[] n; + private static boolean validAlg; // to check validity of algorithm name input public static String[] algs = {"unique1", "unique2"}; @@ -26,6 +27,17 @@ public static void main(String[] args) { UniqueExperiment exp = new UniqueExperiment(nargs); exp.name = args[0]; + validAlg = false; // input assumed to be invalid algorithm + for (int i = 0; i < algs.length; i++) { // for each valid algorithm option... + if (algs[i].equals(exp.name)) { // check if input equals valid algorithm option + validAlg = true; // true when input matches at least one valid algorithm + } + } + if (!validAlg) { // if input is invalid... + System.out.println("Sorry, that is not a valid \"Unique\" algorithm."); + return; // exit main method + } + switch (exp.name) { case "unique1": exp.alg = 1; From 06b9b81a63345d89d28ae1d18c689ce4e8693970 Mon Sep 17 00:00:00 2001 From: Maria Kim Date: Fri, 21 Oct 2016 16:09:58 -0400 Subject: [PATCH 2/2] changing validAlg to local variable, adding print of algorithm options --- src/edu/allegheny/expose/examples/sort/SortingExperiment.java | 4 ++-- .../allegheny/expose/examples/unique/UniqueExperiment.java | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/edu/allegheny/expose/examples/sort/SortingExperiment.java b/src/edu/allegheny/expose/examples/sort/SortingExperiment.java index fe93d4e..261f702 100644 --- a/src/edu/allegheny/expose/examples/sort/SortingExperiment.java +++ b/src/edu/allegheny/expose/examples/sort/SortingExperiment.java @@ -12,7 +12,6 @@ public class SortingExperiment extends DoublingExperiment{ protected int alg; protected String name; private int[] n; - private static boolean validAlg; // to check validity of algorithm name input public static String[] algs = {"quick", "insertion", "merge", "selection", "bubble"}; @@ -26,7 +25,7 @@ public static void main(String[] args){ SortingExperiment exp = new SortingExperiment(nargs); exp.name = args[0]; - validAlg = false; // input assumed to be invalid algorithm + boolean validAlg = false; // input assumed to be invalid algorithm for (int i = 0; i < algs.length; i++) { // for each valid algorithm option... if (algs[i].equals(exp.name)) { // check if input equals valid algorithm option validAlg = true; // true when input matches at least one valid algorithm @@ -34,6 +33,7 @@ public static void main(String[] args){ } if (!validAlg) { // if input is invalid... System.out.println("Sorry, that is not a valid \"Sorting\" algorithm."); + System.out.println("Supported algorithms are: " + Arrays.toString(algs)); return; // exit main method } diff --git a/src/edu/allegheny/expose/examples/unique/UniqueExperiment.java b/src/edu/allegheny/expose/examples/unique/UniqueExperiment.java index d10b9dd..1336497 100644 --- a/src/edu/allegheny/expose/examples/unique/UniqueExperiment.java +++ b/src/edu/allegheny/expose/examples/unique/UniqueExperiment.java @@ -12,7 +12,6 @@ public class UniqueExperiment extends DoublingExperiment { protected int alg; protected String name; private int[] n; - private static boolean validAlg; // to check validity of algorithm name input public static String[] algs = {"unique1", "unique2"}; @@ -27,7 +26,7 @@ public static void main(String[] args) { UniqueExperiment exp = new UniqueExperiment(nargs); exp.name = args[0]; - validAlg = false; // input assumed to be invalid algorithm + boolean validAlg = false; // input assumed to be invalid algorithm for (int i = 0; i < algs.length; i++) { // for each valid algorithm option... if (algs[i].equals(exp.name)) { // check if input equals valid algorithm option validAlg = true; // true when input matches at least one valid algorithm @@ -35,6 +34,7 @@ public static void main(String[] args) { } if (!validAlg) { // if input is invalid... System.out.println("Sorry, that is not a valid \"Unique\" algorithm."); + System.out.println("Supported algorithms are: " + Arrays.toString(algs)); return; // exit main method }