|
| 1 | +public class Problem2 { |
| 2 | + |
| 3 | + /** |
| 4 | + * You are driving a little too fast, and a police officer stops you. |
| 5 | + * Write code to compute the fine you would have to pay. |
| 6 | + * If your speed is 60 or less, the result is 0 since there is no fine. |
| 7 | + * If speed is between 61 and 80 inclusive, the fine is 100 dollars. |
| 8 | + * If speed is 81 or more, the result is 200. |
| 9 | + * Unless it is a holiday -- on that day, your speed can be 5 higher in all cases. <br> |
| 10 | + * <br> |
| 11 | + * |
| 12 | + * <b>EXPECTATIONS:</b><br> |
| 13 | + speedingFine(60, false) <b>---></b> 0 <br> |
| 14 | + speedingFine (65, false) <b>---></b> 100 <br> |
| 15 | + speedingFine (65, true) <b>---></b> 0 <br> |
| 16 | + */ |
| 17 | + public static int speedingFine(int speed, boolean isHoliday) { |
| 18 | + if (isHoliday) { |
| 19 | + if(speed > 65 && speed <= 85) { |
| 20 | + return 100; |
| 21 | + } else if (speed > 85) { |
| 22 | + return 200; |
| 23 | + } |
| 24 | + return 0; |
| 25 | + } else { |
| 26 | + if(speed > 60 && speed <= 80) { |
| 27 | + return 100; |
| 28 | + } else if (speed > 80) { |
| 29 | + return 200; |
| 30 | + } |
| 31 | + return 0; |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + |
| 36 | + //----------------------STARTING POINT OF PROGRAM. IGNORE BELOW --------------------// |
| 37 | + public static void main(String args[]){ |
| 38 | + Problem2Test.runTests(); |
| 39 | + } |
| 40 | + |
| 41 | + static class Problem2Test { |
| 42 | + public static void runTests(){ |
| 43 | + |
| 44 | + |
| 45 | + int [] params1 = {60,65,65,80,85,85,70,75,75,40,40,90}; |
| 46 | + |
| 47 | + boolean[] params2 = { false, false, true, false, false, true, false, false, true, false, true, false }; |
| 48 | + |
| 49 | + int[] expected = { 0, 100, 0, 100, 200, 100, 100, 100, 100, 0, 0, 200 }; |
| 50 | + |
| 51 | + for(int i=0; i < params1.length; i++){ |
| 52 | + int result = Problem2.speedingFine(params1[i], params2[i]); |
| 53 | + if(result == expected[i]) { |
| 54 | + System.out.println(printPassResult(params1[i], params2[i], expected[i], result)); |
| 55 | + } else{ |
| 56 | + System.out.println(printFailResult(params1[i], params2[i], expected[i], result)); |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + private static String printPassResult(Object params1, Object params2, Object expected, Object result){ |
| 62 | + return "PASS: speedingFine("+ params1.toString()+ ", "+ params2.toString()+ ") -> " + result.toString(); |
| 63 | + } |
| 64 | + |
| 65 | + |
| 66 | + private static String printFailResult(Object params1, Object params2, Object expected, Object result){ |
| 67 | + String ret = "**********************" + "\n"; |
| 68 | + ret += "FAIL: speedingFine("+ params1.toString()+ ", "+ params2.toString()+ ") -> " + result.toString() |
| 69 | + + " Expected: "+ expected.toString(); |
| 70 | + ret += "\n" + "**********************"; |
| 71 | + return ret; |
| 72 | + } |
| 73 | + } |
| 74 | +} |
| 75 | + |
0 commit comments