public class MakeChocolate { /******************************** * We want make a package of goal kilos of chocolate. We have * small bars (1 kilo each) and big bars (5 kilos each). * Return the number of small bars to use, assuming we always * use big bars before small bars. Return -1 if it can't be done. * * See the verify lines below for examples of input * and expected output. * * Run this class, If you see errors, it is because the tests below * did not pass. Once the tests do pass, you will see a log of `Success!` ********************************/ public static void main(String [] args) { try { // These are the tests that will run against your code // vars = small, big, goal, expected answer verify(4, 1, 9, 4); verify(4, 1, 10, -1); verify(4, 1, 7, 2); verify(6, 2, 7, 2); verify(4, 1, 5, 0); verify(4, 1, 4, 4); verify(5, 4, 9, 4); verify(9, 3, 18, 3); verify(3, 1, 9, -1); verify(1, 2, 7, -1); verify(1, 2, 6, 1); verify(1, 2, 5, 0); verify(6, 1, 10, 5); verify(6, 1, 11, 6); verify(6, 1, 12, -1); verify(6, 1, 13, -1); verify(6, 2, 10, 0); verify(6, 2, 11, 1); verify(6, 2, 12, 2); verify(60, 100, 550, 50); verify(1000, 1000000, 5000006, 6); verify(7, 1, 12, 7); verify(7, 1, 13, -1); verify(7, 2, 13, 3); System.out.println("Success!"); } catch (Error err) { System.out.println(err.getMessage()); } } /******************************** * YOUR CODE BELOW HERE ********************************/ private static int make(int small, int big, int goal) { int actualBig = big * 5; if (goal - actualBig < 0) { return make(small, big - 1, goal); } int leftover = goal - actualBig; if (leftover <= small) { return leftover; } else { return -1; } } /******************************** * YOUR CODE ABOVE HERE ********************************/ private static void verify(int small, int big, int goal, int expected) { int answer = make(small, big, goal); if (answer != expected) { throw new Error("wrong answer, expected " + answer + " to equal " + expected); } } }