Selaa lähdekoodia

java: Start track with Bob

This is the complete skeleton for enabling Java, from curriculum
through the first exercise. Java is listed in the main curriculum.rb
but commented out; we will activate once there are a sufficient body
of exercises ported.

The java.png icon is taken from http://findicons.com/icon/88943/java?id=89126,
released under a freeware license.

Describe how to minimally set-up Java and JUnit. (Future work may
try to see if we can get Locale.additional_files working and provide
build integration via Gradle or Maven.)

The BobTest is a straight up port of the Python Bob.

This starts the path of fixing #933.
Emil Sit 13 vuotta sitten
vanhempi
commit
163a8361c3
2 muutettua tiedostoa jossa 173 lisäystä ja 0 poistoa
  1. 138
    0
      assignments/java/bob/BobTest.java
  2. 35
    0
      assignments/java/bob/example.java

+ 138
- 0
assignments/java/bob/BobTest.java Näytä tiedosto

@@ -0,0 +1,138 @@
1
+import org.junit.Test;
2
+
3
+import static org.junit.Assert.*;
4
+
5
+public class BobTest {
6
+    private final Bob bob = new Bob();
7
+
8
+    @Test
9
+    public void say_something() {
10
+        assertEquals(
11
+            "Whatever.",
12
+            bob.hey("Tom-ay-to, tom-aaaah-to.")
13
+        );
14
+    }
15
+
16
+    @Test
17
+    public void shouting() {
18
+        assertEquals(
19
+            "Woah, chill out!",
20
+            bob.hey("WATCH OUT!")
21
+        );
22
+    }
23
+
24
+    @Test
25
+    public void asking_a_question() {
26
+        assertEquals(
27
+            "Sure.",
28
+            bob.hey("Does this cryogenic chamber make me look fat?")
29
+        );
30
+    }
31
+
32
+    @Test
33
+    public void asking_a_numeric_question() {
34
+        assertEquals(
35
+            "Sure.",
36
+            bob.hey("You are, what, like 15?")
37
+        );
38
+    }
39
+
40
+    @Test
41
+    public void talking_forcefully() {
42
+        assertEquals(
43
+            "Whatever.",
44
+            bob.hey("Let's go make out behind the gym!")
45
+        );
46
+    }
47
+
48
+    @Test
49
+    public void using_acronyms_in_regular_speech() {
50
+        assertEquals(
51
+            "Whatever.", bob.hey("It's OK if you don't want to go to the DMV.")
52
+        );
53
+    }
54
+
55
+    @Test
56
+    public void forceful_questions() {
57
+        assertEquals(
58
+            "Woah, chill out!", bob.hey("WHAT THE HELL WERE YOU THINKING?")
59
+        );
60
+    }
61
+
62
+    @Test
63
+    public void shouting_numbers() {
64
+        assertEquals(
65
+            "Woah, chill out!", bob.hey("1, 2, 3 GO!")
66
+        );
67
+    }
68
+
69
+    @Test
70
+    public void only_numbers() {
71
+        assertEquals(
72
+            "Whatever.", bob.hey("1, 2, 3")
73
+        );
74
+    }
75
+
76
+    @Test
77
+    public void question_with_only_numbers() {
78
+        assertEquals(
79
+            "Sure.", bob.hey("4?")
80
+        );
81
+    }
82
+
83
+    @Test
84
+    public void shouting_with_special_characters() {
85
+        assertEquals(
86
+            "Woah, chill out!", bob.hey("ZOMG THE %^*@#$(*^ ZOMBIES ARE COMING!!11!!1!")
87
+        );
88
+    }
89
+
90
+    @Test
91
+    public void shouting_with_umlauts() {
92
+        assertEquals(
93
+            "Woah, chill out!", bob.hey("\u00dcML\u00c4\u00dcTS!")
94
+        );
95
+    }
96
+
97
+    @Test
98
+    public void calmly_speaking_with_umlauts() {
99
+        assertEquals(
100
+            "Whatever.", bob.hey("\u00dcML\u00e4\u00dcTS!")
101
+        );
102
+    }
103
+
104
+    @Test
105
+    public void shouting_with_no_exclamation_mark() {
106
+        assertEquals(
107
+            "Woah, chill out!", bob.hey("I HATE YOU")
108
+        );
109
+    }
110
+
111
+    @Test
112
+    public void statement_containing_question_mark() {
113
+        assertEquals(
114
+            "Whatever.", bob.hey("Ending with ? means a question.")
115
+        );
116
+    }
117
+
118
+    @Test
119
+    public void prattling_on() {
120
+        assertEquals(
121
+            "Sure.", bob.hey("Wait! Hang on. Are you going to be OK?")
122
+        );
123
+    }
124
+
125
+    @Test
126
+    public void silence() {
127
+        assertEquals(
128
+            "Fine. Be that way!", bob.hey("")
129
+        );
130
+    }
131
+
132
+    @Test
133
+    public void prolonged_silence() {
134
+        assertEquals(
135
+            "Fine. Be that way!", bob.hey("    ")
136
+        );
137
+    }
138
+}

+ 35
- 0
assignments/java/bob/example.java Näytä tiedosto

@@ -0,0 +1,35 @@
1
+/**
2
+ * Bob is a lackadasical teenager.
3
+ */
4
+public class Bob {
5
+    public String hey(String input) {
6
+        input = normalize(input);
7
+        if (isSilence(input))
8
+            return "Fine. Be that way!";
9
+        if (isShout(input))
10
+            return "Woah, chill out!";
11
+        if (isQuestion(input))
12
+            return "Sure.";
13
+        return "Whatever.";
14
+    }
15
+
16
+    private static String normalize(String input) {
17
+        return input.trim();
18
+    }
19
+
20
+    private static boolean isSilence(String input) {
21
+        return input.equals("");
22
+    }
23
+
24
+    private static boolean isShout(String input) {
25
+        final String upperCased = input.toUpperCase();
26
+        final String lowerCased = input.toLowerCase();
27
+        final boolean containsSomeLetters = !lowerCased.equals(upperCased);
28
+        final boolean isAllUpperCase = upperCased.equals(input);
29
+        return (containsSomeLetters && isAllUpperCase);
30
+    }
31
+
32
+    private static boolean isQuestion(String input) {
33
+        return input.endsWith("?");
34
+    }
35
+}