|
| 1 | +package org.teachingkidsprogramming.recipes.inDevelopment; |
| 2 | + |
| 3 | +import org.junit.Assert; |
| 4 | +import org.junit.Test; |
| 5 | +import org.teachingextensions.approvals.lite.util.FormattedException; |
| 6 | + |
| 7 | +//**************ANSWER: This deepdive is in progress!*****************// |
| 8 | +// |
| 9 | +@SuppressWarnings("unused") |
| 10 | +public class DeepDive09exceptions |
| 11 | +{ |
| 12 | + @Test |
| 13 | + public void exceptionsShouldProvideInformation() throws Exception |
| 14 | + { |
| 15 | + Chain c = createChain(); |
| 16 | + int answer = c.get("a").get("b").get("c").get("d").get("e").value; /* Fix This Line */ |
| 17 | + Assert.assertEquals(2048, answer); |
| 18 | + } |
| 19 | + @Test |
| 20 | + public void exceptionsShouldExplainPreconditions() throws Exception |
| 21 | + { |
| 22 | + Game game = new Game(); |
| 23 | + /* Add needed line here */ |
| 24 | + int fun = game.play(); |
| 25 | + Assert.assertEquals(11, fun); |
| 26 | + } |
| 27 | + /** |
| 28 | + * Ignore the following, It's needed to run the homework |
| 29 | + * |
| 30 | + * |
| 31 | + * |
| 32 | + * |
| 33 | + * |
| 34 | + * |
| 35 | + * |
| 36 | + * |
| 37 | + * |
| 38 | + * |
| 39 | + */ |
| 40 | + private int call(int a, int b, int c) |
| 41 | + { |
| 42 | + if (((a + c) / 2) == b) { throw new FormattedException("%s is not a valid input for (%s, %s, %s)", b, a, b, |
| 43 | + c); } |
| 44 | + return a + b + c; |
| 45 | + } |
| 46 | + private static class Game |
| 47 | + { |
| 48 | + boolean on = false; |
| 49 | + public void turnOn() |
| 50 | + { |
| 51 | + on = true; |
| 52 | + } |
| 53 | + public int play() |
| 54 | + { |
| 55 | + if (!on) { throw new FormattedException( |
| 56 | + "Before you can play a game you need to turn it on.\n game.turnOn()"); } |
| 57 | + return 11; |
| 58 | + } |
| 59 | + } |
| 60 | + private static class Chain |
| 61 | + { |
| 62 | + private String label; |
| 63 | + private Chain chain; |
| 64 | + public int value; |
| 65 | + public Chain(String label, Chain chain) |
| 66 | + { |
| 67 | + this.label = label; |
| 68 | + this.chain = chain; |
| 69 | + } |
| 70 | + public Chain(int value) |
| 71 | + { |
| 72 | + this.value = value; |
| 73 | + } |
| 74 | + public Chain get(String string) |
| 75 | + { |
| 76 | + if (!label.equals( |
| 77 | + string)) { throw new FormattedException("There is no value for '%s', please use '%s'", string, label); } |
| 78 | + return chain; |
| 79 | + } |
| 80 | + } |
| 81 | + private Chain createChain() |
| 82 | + { |
| 83 | + return new Chain("a", new Chain("b", new Chain("surprise", new Chain("d", new Chain("e", new Chain(2048)))))); |
| 84 | + } |
| 85 | + private static class _____ extends Exception |
| 86 | + { |
| 87 | + private static final long serialVersionUID = 7013264013388843231L; |
| 88 | + public _____(String message, Exception originalException) |
| 89 | + { |
| 90 | + super(message, originalException); |
| 91 | + } |
| 92 | + } |
| 93 | + public String ___ = "You need to fill in the blank ___"; |
| 94 | + public Integer ____ = -99; |
| 95 | +} |
0 commit comments