import java.util.Arrays; import java.util.ArrayList; import java.util.HashMap; public class Homework7S{ /* Complete the bodies of the main functions below. Note that To check your answers, compile this file with: javac Homework4.java and then run with: java Homework7 This will score thexception.problems you've attempted so far, and allow you to edit them to get them to pass all their test cases. */ /* Problem 1: ****************************************************************************/ /* Declare an empty ArrayList of Strings (using generics), and return it */ public static boolean test1 = true; public static ArrayList main1() { // write the code below, and return the correct result return new ArrayList(); } /* Problem 2: ****************************************************************************/ /* Declare an ArrayList of Strings (using generics), and add the strings "CS", "1111", "course" to it in that order, then return it */ public static boolean test2 = true; public static ArrayList main2() { // write the code below, and return the correct result return null; } /* Problem 3: ****************************************************************************/ /* Declare an ArrayList of Integers (using generics), and add the values 1, 2, and 3 to it in that order, then return it */ public static boolean test3 = true; public static ArrayList main3() { // write the code below, and return the correct result return null; } /* Problem 4: ****************************************************************************/ /* Return the second element at index 2 of the incoming ArrayList of string, or return the empty string if it doesn't exist */ public static boolean test4 = true; public static String main4(ArrayList strings) { // write the code below, and return the correct result return null; } /* Problem 5: ****************************************************************************/ /* Declare a HashMap object of Strings mapping to Integers (using generics), add the three name value pairs below to it, and return the result: cat, 22 dog, 13 bird, 11 */ public static boolean test5 = true; public static HashMap main5() { // write the code below, and return the correct result return null; } /* Problem 6: ****************************************************************************/ /* Modify the incoming HashMap of Strings to Integers to put the specified key value pair into the object; overwrite the old value if the key exists, or add a new key-value pair if the key is not in the HashMap. This method is void. */ public static boolean test6 = true; public static void main6(HashMap mappings, String key, Integer value) { // write the code below } /************************************************************************************************************** /* HELPER CODE -- you can ignore this for now! /*************************************************************************************************************/ public static boolean testChecker(String problem, String test, HashMap result, String inputs, HashMap expected) { System.out.println(problem + ", " + test + ", did it pass? " + (result.equals(expected))); if (!result.equals(expected)) { System.out.print("\tOops! Check your " + problem + " for the inputs " + inputs); String formatInputs = "by plugging your " + problem + " into the visualizer for those inputs."; System.out.println(formatInputs); return false; } return true; } public static boolean testChecker(String problem, String test, ArrayList result, String inputs, ArrayList expected) { System.out.println(problem + ", " + test + ", did it pass? " + (result.equals(expected))); if (!result.equals(expected)) { System.out.print("\tOops! Check your " + problem + " for the inputs " + inputs); String formatInputs = "by plugging your " + problem + " into the visualizer for those inputs."; System.out.println(formatInputs); return false; } return true; } public static boolean testChecker(String problem, String test, String result, String inputs, String expected) { System.out.println(problem + ", " + test + ", did it pass? " + (result.equals(expected))); if (!result.equals(expected)) { System.out.print("\tOops! Check your " + problem + " for the inputs " + inputs); String formatInputs = "by plugging your " + problem + " into the visualizer for those inputs."; System.out.println(formatInputs); return false; } return true; } /************************************************************************************************************** /* TEST CASES -- see what the inputs and outputs were to your code /*************************************************************************************************************/ public static void main(String[] args) { boolean completed = true; int score = 0; // Problem 1 test cases if (test1 == true) { completed = true; try { ArrayList result = new ArrayList(); completed = testChecker("main1", "test1", main1(), "empty list", result) && completed; } catch (Exception exception) { System.out.println("Oops! Check your main1, test1, on inputs empty list -- it raised an exception."); exception.printStackTrace(); } if (completed == true) { System.out.println("Problem 1 finished, great work!"); score++; } else System.out.println("Check the errors above for Problem 1, and try again!"); } else System.out.println("skipping test1"); // Problem 2 test cases if (test2 == true) { completed = true; try { ArrayList result = new ArrayList(); result.add("CS"); result.add("1111"); result.add("course"); completed = testChecker("main2", "test1", main2(), "as specified", result) && completed; } catch (Exception exception) { System.out.println("Oops! Check your main2, test1, -- it raised an exception."); exception.printStackTrace(); } if (completed == true) { System.out.println("Problem 2 finished, great work!"); score++; } else System.out.println("Check the errors above for Problem 2, and try again!"); } else System.out.println("skipping test2"); // Problem 3 test cases if (test3 == true) { completed = true; try { ArrayList result = new ArrayList(); result.add(1); result.add(2); result.add(3); completed = testChecker("main3", "test1", main3(), "as specified", result) && completed; } catch (Exception exception) { System.out.println("Oops! Check your main3, test1, on inputs as specified -- it raised an exception."); exception.printStackTrace(); } if (completed == true) { System.out.println("Problem 3 finished, great work!"); score++; } else System.out.println("Check the errors above for Problem 3, and try again!"); } else System.out.println("skipping test3"); // Problem 4 test cases if (test4 == true) { completed = true; try { ArrayList input1 = new ArrayList(); input1.add("CS"); input1.add("1111"); input1.add("course"); completed = testChecker("main4", "test1", main4(input1), "[CS, 1111, course]", "1111") && completed; } catch (Exception exception) { System.out.println("Oops! Check your main4, test1, on inputs [CS, 1111, course] -- it raised an exception."); exception.printStackTrace(); } try { ArrayList input1 = new ArrayList(); input1.add("CS"); completed = testChecker("main4", "test2", main4(input1), "[CS]", "") && completed; } catch (Exception exception) { System.out.println("Oops! Check your main4, test2, on inputs [CS] -- it raised an exception."); exception.printStackTrace(); } if (completed == true) { System.out.println("Problem 4 finished, great work!"); score++; } else System.out.println("Check the errors above for Problem 4, and try again!"); } else System.out.println("skipping test4"); // Problem 5 test cases if (test5 == true) { completed = true; try { HashMap map = new HashMap(); map.put("cat", 22); map.put("dog", 13); map.put("bird", 11); completed = testChecker("main5", "test1", main5(), "as specified", map) && completed; } catch (Exception exception) { System.out.println("Oops! Check your main5, test1, on inputs as specified -- it raised an exception."); exception.printStackTrace(); } if (completed == true) { System.out.println("Problem 5 finished, great work!"); score++; } else System.out.println("Check the errors above for Problem 5, and try again!"); } else System.out.println("skipping test5"); // Problem 6 test cases if (test6 == true) { completed = true; try { HashMap map = new HashMap(); map.put("cat", 22); map.put("dog", 13); map.put("bird", 11); HashMap result = new HashMap(); result.put("cat", 22); result.put("dog", 13); result.put("bird", 11); result.put("snake", 12); main6(map, "snake", 12); completed = testChecker("main6", "test1", map, "on line 266-276 in this file", result) && completed; } catch (Exception exception) { System.out.println("Oops! Check your main6, test1, on line 266-276 in this file -- it raised an exception."); exception.printStackTrace(); } try { HashMap map = new HashMap(); map.put("cat", 22); map.put("dog", 13); map.put("bird", 11); HashMap result = new HashMap(); result.put("cat", 22); result.put("dog", 13); result.put("bird", 11); result.put("bird", 15); main6(map, "bird", 15); completed = testChecker("main6", "test2", map, "on line 283-293 in this file", result) && completed; } catch (Exception exception) { System.out.println("Oops! Check your main6, test2, on line 283-293 in this file -- it raised an exception."); exception.printStackTrace(); } if (completed == true) { System.out.println("Problem 6 finished, great work!"); score++; } else System.out.println("Check the errors above for Problem 6, and try again!"); } else System.out.println("skipping test6"); if (!(test1 == true && test2 == true && test3 == true && test4 == true && test5 == true && test6 == true)) System.out.println("Please set all test cases to true before submitting your " + "file called Homework7.java to Blackboard."); //style checking System.out.println("\n***Next steps: perform style checking (you will be graded on it) by making sure that"); System.out.println("\tcheckstyle-9.2.1-all.jar"); System.out.println("\t\tand"); System.out.println("\tCS1111_checks.xml"); System.out.println("are in the current directory, and running the style checker with the following command:"); System.out.println("\tjava -jar checkstyle-9.2.1-all.jar -c ./CS1111_checks.xml Homework7.java"); //scoring System.out.println("\n***YOUR SCORE ON THIS ASSIGNMENT: ( " + score + " + [up to 4 points for style] ) out of 10 total***\n"); } }