From 9cb34501b4056ac51f1b7e0e5356847f88eab551 Mon Sep 17 00:00:00 2001 From: Joshua Ashton Date: Tue, 6 Feb 2024 21:28:05 -0700 Subject: [PATCH] Challenge, tests, input file, etc. are setup. --- spring24/week4/Challenge.java | 72 ++++++++++++++++++++++++++++++----- spring24/week4/Example.txt | 1 + 2 files changed, 63 insertions(+), 10 deletions(-) create mode 100644 spring24/week4/Example.txt diff --git a/spring24/week4/Challenge.java b/spring24/week4/Challenge.java index 9473482..b112f51 100644 --- a/spring24/week4/Challenge.java +++ b/spring24/week4/Challenge.java @@ -10,15 +10,15 @@ public class Challenge { /** * CHALLENGE 1: - * Read and store the contents of a file into a String array for further processing. + * Read and store the contents of a file into a String or other type for further processing. * * @param f * @return */ - public static String[] readFile(File f) { + public static String readFile(File f) { if(f == null) return null; // Catch if the File does not exist. - // TODO: Create a variable, potentially an object, to store Strings that are read in from the file. + // TODO: Create a variable, potentially an object like ArrayList or StringBuilder or even the humble String, to store Strings that are read in from the file. try (Scanner s = new Scanner(f)){ // TODO: Populate the variable from above with Strings from the file. @@ -26,19 +26,21 @@ public class Challenge { e.printStackTrace(); } + // TODO: Convert your variable to a String variable. + return null; // TODO: Return the variable. } /** * CHALLENGE 2: - * Given an input String array, store each token in an ArrayList. + * Given an input String, store each token in an ArrayList. * * A token is a single word, symbol, or character, surrounded on both sides by a space. * * @param s * @return */ - public static ArrayList split(String[] s) { + public static ArrayList split(String s) { ArrayList list = new ArrayList(); // TODO: Take the input String array s and add each separate word (separated by a space) to the ArrayList. @@ -49,7 +51,8 @@ public class Challenge { /** * CHALLENGE 3: * Given an input ArrayList of tokens (assume all are valid integers, doubles, or operations ('+','-','*','/','%','(', and ')')), - * calculate the result using correct order of operations. + * calculate the result using correct order of operations. The output should only contain 4 decimal places, rounding up if the 5th place + * is greater than 5. * * HINT: Think of a data structure that might be useful for this. * @@ -59,19 +62,68 @@ public class Challenge { return 0.0; // TODO: Implement this method. } - public static boolean testChallenge1(String[] s) { - return false; // TODO + public static boolean testChallenge1(String s) { + return s.equals("( ( 5 + 5 ) * 5 * 3 ) / ( 5 * 3 + ( 8 * 4 ) )"); } public static boolean testChallenge2(ArrayList list) { - return false; // TODO + // Don't do this to answer Challenge 2. This is the only way to not give away a solution. + ArrayList expected = new ArrayList<>(); + expected.add("("); + expected.add("("); + expected.add("5"); + expected.add("+"); + expected.add("5"); + expected.add(")"); + expected.add("*"); + expected.add("5"); + expected.add("*"); + expected.add("3"); + expected.add("/"); + expected.add("("); + expected.add("5"); + expected.add("*"); + expected.add("3"); + expected.add("+"); + expected.add("("); + expected.add("8"); + expected.add("*"); + expected.add("4"); + expected.add(")"); + expected.add(")"); + + if(expected.size() != list.size()) { + System.out.println("Your solution for Challenge 2 does not correctly tokenize! You have too few or too many elements."); + return false; + } + + for(int i = 0; i < expected.size(); i++) + if(!(expected.get(i).equals(list.get(i)))) { + System.out.println("Your solution for Challenge 2 does not correctly tokenize! At index " + i + " (and maybe others) your output does not match the expected output."); + return false; + } + + return true; } public static boolean testChallenge3(double result) { - return false; // TODO + return result == 3.1915; } + // Test driver for the three coding challenges. public static void main(String[] args) { + File f = new File("Example.txt"); + String s = readFile(f); + ArrayList list = split(s); + double result = calculate(list); + System.out.print("Challenge 1 - "); + System.out.println(testChallenge1(s) ? "Correct!\n" : "Incorrect.\n"); + + System.out.print("Challenge 2 - "); + System.out.println(testChallenge2(list) ? "Correct!\n" : "Incorrect.\n"); + + System.out.print("Challenge 3 - "); + System.out.println(testChallenge3(result) ? "Correct!\n" : "Incorrect.\n"); } } \ No newline at end of file diff --git a/spring24/week4/Example.txt b/spring24/week4/Example.txt new file mode 100644 index 0000000..33b241a --- /dev/null +++ b/spring24/week4/Example.txt @@ -0,0 +1 @@ +( ( 5 + 5 ) * 5 * 3 ) / ( 5 * 3 + ( 8 * 4 ) ) \ No newline at end of file