import java.util.Arrays; import java.util.ArrayList; public class Homework4{ /* 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 Homework4 This will score the problems you've attempted so far, and allow you to edit them to get them to pass all their test cases. Please turn on all the test case flags before submission to the submitserver. */ /* Problem 1: ****************************************************************************/ /* Write a loop that returns the sum of all numbers that are within ten numbers of a seed number entered by the user (i.e. if the users enter 1, sum the numbers 1 through 9 inclusive). */ public static boolean test1 = false; public static int main1(int seed) { // write the code below, and return the correct result return -9999999; } /* Problem 2: ****************************************************************************/ /* Using the visualizer link below, debug the provided code so that it correctly calculates the factorial of a seed number entered by the user. Then, paste the correct solution inside the main2 method below. https://cscircles.cemc.uwaterloo.ca/java_visualize/#code=public+class+Problem2+%7B%0A+++%0A%09public+static+int+main2(int+num)+%7B%0A%0A++++++int+factorial+%3D+0%3B%0A++++++for(int+i+%3D+0%3B+i+%3C+num%3B+i%2B%2B)%7B%0A+++++++++factorial+%3D+factorial+*+num%3B%0A++++++%7D%0A++++++%0A++++++return+factorial%3B%0A%09%7D%0A+++%0A+++public+static+void+main(String%5B%5D+args)+%7B%0A++++++int+result+%3D+main2(6)%3B%0A++++++if+(result+%3D%3D+720)%0A+++++++++System.out.println(%22found+the+bug!%22)%3B%0A++++++else%0A+++++++++System.out.println(%22keep+working+on+it,+you+haven't+found+the+bug+yet.%22)%3B%0A+++%7D%0A%7D&mode=edit */ public static boolean test2 = false; public static int main2(int seed) { // write the code below, and return the correct result return -9999999; } /* Problem 3: ****************************************************************************/ /* Write a loop that returns the sum of every natural number that is divisible by 7 and less than 30. */ public static boolean test3 = false; public static int main3() { return -99999999; } /* Problem 4: ****************************************************************************/ /* Write a loop that sums up all the factors of a natural number entered by the user. */ public static boolean test4 = false; public static int main4(int num) { // write the code below, and return the correct result return -9999999; } /* Problem 5: ****************************************************************************/ /* Using the visualizer link below, debug the code to find the minimum value of a list of integers entered by the user. Then, paste the correct solution into the template below. https://cscircles.cemc.uwaterloo.ca/java_visualize/#code=public+class+Problem5+%7B%0A+++%0A%09public+static+int+main5(int%5B%5D+list)+%7B%0A++++++int+min+%3D+0%3B%0A++++++%0A++++++for(int+i+%3D+0%3B+i+%3C%3D+list.length%3B+i%2B%2B)%7B%0A+++++++++if(list%5Bi%5D+%3C+min)+//don't+need+curly+braces+if+just+one+line+below%0A++++++++++++min+%3D+list%5Bi%5D%3B%0A++++++%7D%0A++++++%0A++++++return+min%3B%0A%09%09%0A%09%7D%0A%0A+++%0A+++public+static+void+main(String%5B%5D+args)+%7B%0A++++++int%5B%5D+input+%3D+%7B-1,+-2,+-3,+-4,+-7,+-5%7D%3B%0A++++++int+result+%3D+main5(input)%3B%0A++++++if+(result+%3D%3D+1)%0A+++++++++System.out.println(%22found+the+bug!%22)%3B%0A++++++else%0A+++++++++System.out.println(%22keep+working+on+it,+you+haven't+found+the+bug+yet.%22)%3B%0A+++%7D%0A%7D&mode=edit */ public static boolean test5 = false; public static int main5(int[] list) { // write the code below, and return the correct result return -9999999; } /************************************************************************************************************** /* HELPER CODE -- you can ignore this for now! /*************************************************************************************************************/ public static boolean testChecker(String problem, String test, double result, ArrayList inputs, double expected) { double diff = result - expected; boolean passed = false; if (diff < 0.0001 && diff > -0.0001) passed = true; System.out.println(problem + ", " + test + ", did it pass? " + (passed)); if (! passed) { System.out.print("\tOops! Check your " + problem + " for the inputs "); String formatInputs = ""; for (int i = 0; i < inputs.size(); i++) formatInputs = formatInputs + inputs.get(i) + " and "; formatInputs = formatInputs.substring(0, formatInputs.length()) + " by plugging your " + problem + " into the visualizer for those inputs. We expected " + expected + " but your code returned " + result; 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; // seed 10 if (test1 == true) { completed = true; try { completed = testChecker("main1", "test1", main1(1), new ArrayList(Arrays.asList(1)), 45) && completed; } catch (Exception exception) { completed = false; System.out.println("Oops! Check your main1, test1, on inputs 1 -- it raised an exception."); } 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"); // factorial if (test2 == true) { completed = true; try { completed = testChecker("main2", "test1", main2(0), new ArrayList(Arrays.asList(0)), 1) && completed; } catch (Exception exception) { completed = false; System.out.println("Oops! Check your main2, test1, on inputs 0 -- it raised an exception."); } try { completed = testChecker("main2", "test2", main2(2), new ArrayList(Arrays.asList(2)), 2) && completed; } catch (Exception exception) { completed = false; System.out.println("Oops! Check your main2, test2, on inputs 2 -- it raised an exception."); } try { completed = testChecker("main2", "test3", main2(3), new ArrayList(Arrays.asList(3)), 6) && completed; } catch (Exception exception) { completed = false; System.out.println("Oops! Check your main2, test3, on inputs 3 -- it raised an exception."); } try { completed = testChecker("main2", "test4", main2(1), new ArrayList(Arrays.asList(1)), 1) && completed; } catch (Exception exception) { completed = false; System.out.println("Oops! Check your main2, test4, on inputs 1 -- it raised an exception."); } if (completed == true) { score++; System.out.println("Problem 2 finished, great work!"); } else System.out.println("Check the errors above for Problem 2, and try again!"); } else System.out.println("skipping test2"); // div by 7 and 30 if (test3 == true) { completed = true; try { completed = testChecker("main3", "test1", main3(), new ArrayList(Arrays.asList()), 70) && completed; } catch (Exception exception) { completed = false; System.out.println("Oops! Check your main3, test1 -- it raised an exception."); } if (completed == true) { score++; System.out.println("Problem 3 finished, great work!"); } else System.out.println("Check the errors above for Problem 3, and try again!"); } else System.out.println("skipping test3"); // all factors if (test4 == true) { completed = true; try { completed = testChecker("main4", "test1", main4(1), new ArrayList(Arrays.asList(1)), 1) && completed; } catch (Exception exception) { completed = false; System.out.println("Oops! Check your main4 test1, on inputs 0 -- it raised an exception."); } try { completed = testChecker("main4", "test2", main4(3), new ArrayList(Arrays.asList(3)), 4) && completed; } catch (Exception exception) { completed = false; System.out.println("Oops! Check your main4, test2, on inputs 1 -- it raised an exception."); } try { completed = testChecker("main4", "test3", main4(4), new ArrayList(Arrays.asList(4)), 7) && completed; } catch (Exception exception) { completed = false; System.out.println("Oops! Check your main4, test3, on inputs 4 -- it raised an exception."); } try { completed = testChecker("main4", "test4", main4(2), new ArrayList(Arrays.asList(2)), 3) && completed; } catch (Exception exception) { completed = false; System.out.println("Oops! Check your main4, test4, on inputs 2 -- it raised an exception."); } if (completed == true) { score++; System.out.println("Problem 4 finished, great work!"); } else System.out.println("Check the errors above for Problem 4, and try again!"); } else System.out.println("skipping test4"); // min list if (test5 == true) { completed = true; try { int[] arr = {1}; completed = testChecker("main5", "test1", main5(arr), new ArrayList(Arrays.asList(1)), 1) && completed; } catch (Exception exception) { completed = false; System.out.println("Oops! Check your main5, test1, on inputs {1} -- it raised an exception."); } try { int[] arr = {1, 2}; completed = testChecker("main5", "test2", main5(arr), new ArrayList(Arrays.asList(1, 2)), 1) && completed; } catch (Exception exception) { completed = false; System.out.println("Oops! Check your main5, test2, on inputs {1,2} -- it raised an exception."); } try { int[] arr = {2, 1}; completed = testChecker("main5", "test3", main5(arr), new ArrayList(Arrays.asList(2, 1)), 1) && completed; } catch (Exception exception) { completed = false; System.out.println("Oops! Check your main5, test3, on inputs {2,1} -- it raised an exception."); } try { int[] arr = {1, 2, 3}; completed = testChecker("main5", "test4", main5(arr), new ArrayList(Arrays.asList(1, 2, 3)), 1) && completed; } catch (Exception exception) { completed = false; System.out.println("Oops! Check your main5, test4, on inputs {1,2,3} -- it raised an exception."); } try { int[] arr = {2, 3, 1}; completed = testChecker("main5", "test5", main5(arr), new ArrayList(Arrays.asList(2, 3, 1)), 1) && completed; } catch (Exception exception) { completed = false; System.out.println("Oops! Check your main5, test5, on inputs {2,3,1} -- it raised an exception."); } try { int[] arr = {-1, -2}; completed = testChecker("main5", "test6", main5(arr), new ArrayList(Arrays.asList(-1, -2)), -2) && completed; } catch (Exception exception) { completed = false; System.out.println("Oops! Check your main5, test6, on inputs {-1,-2} -- it raised an exception."); } if (completed == true) { score++; System.out.println("Problem 5 finished, great work!"); } else System.out.println("Check the errors above for Problem 5, and try again!"); } else System.out.println("skipping test5"); if (!(test1 == true && test2 == true && test2 == true && test4 == true && test5 == true)) System.out.println("Please set all test cases to true before submitting your file called Homework4.java to the submitserver."); //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 Homework4.java"); //scoring System.out.println("\n***YOUR SCORE ON THIS ASSIGNMENT: ( " + score + " + [up to 4 points for style] ) out of 9 total***\n"); //submission String command = "tar -cvf Homework4.tar Homework4.java"; try { Process process = Runtime.getRuntime().exec(command); } catch (Exception excep) { excep.printStackTrace(); System.out.println("ERROR creating tarfile -- make sure you have tar utility installed"); } System.out.println("Use the submit server link on the syllabus to upload your Homework4.tar file for grading."); } }