Skip to content

Commit 39b1bdd

Browse files
committed
add extension
1 parent 1f6909b commit 39b1bdd

File tree

7 files changed

+174
-98
lines changed

7 files changed

+174
-98
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
- Open the root directory of the project in IntelliJ
99
- Implement the requirements listed in comments in the `./src/main/java/com.booleanuk/core/Exercise.java` file
1010
- When ready to test your solution, open the `./src/test/java/com.booleanuk/core/ExerciseTest.java` file and click a "Run Test" button. You can either run the entire test suite via figure 1 in the screenshot below, or run a specific test via figure 2.
11+
- There is extension work in the `./src/main/java/com.booleanuk/extension/Extension.java` file and tests in the `./src/test/java/com.booleanuk/extension/ExtensionTest.java` file for when you finish the core exercise
1112

1213
![](./assets/run-a-test.PNG)
1314

src/main/java/com/booleanuk/Exercise.java

Lines changed: 0 additions & 41 deletions
This file was deleted.
Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,41 @@
11
package com.booleanuk.core;
22

33
public class Exercise {
4-
public static String sayHelloWorld() {
5-
return "Hello, world!";
6-
}
4+
// A URL consists of these parts: [protocol]://[domain]/[path]
5+
public String brokenUrl = " httpz://booLeAn.co.uk/who-we-are ";
6+
7+
// 1. The brokenUrl member above contains an invalid URL. There's a z instead of an s in the protocol (httpz instead of https).
8+
// Using the `replace` method on brokenUrl, set the fixedUrl member below to the correct value.
9+
// https://docs.oracle.com/en/java/javase/18/docs/api/java.base/java/lang/String.html#replace(char,char)
10+
public String fixedUrl = "";
11+
12+
13+
// Here's a documentation link for all string methods, use it to figure out how to complete the rest of these requirements:
14+
// https://docs.oracle.com/en/java/javase/18/docs/api/java.base/java/lang/String.html#method-summary
15+
16+
17+
// 2. There are currently some upper case characters in the URL. Using an appropriate string method on the fixedUrl member above,
18+
// set the value of lowerCasedUrl.
19+
public String lowerCasedUrl = "";
20+
21+
22+
// 3. There is still white space on both ends of the URL! Use the appropriate string method to trim that white space
23+
// and set the value of the url member below
24+
public String url = "";
25+
26+
27+
// 4. Using the appropriate string method on url, set the value of the protocol member below
28+
public String protocol = "";
29+
30+
31+
// 5. Using the appropriate string method on url, set the value of the domain member below
32+
public String domain = "";
33+
34+
35+
// 6. Set the length member below to the length of the url member
36+
public int length = 0;
37+
38+
39+
// 7. Using concatenation and existing members, set the faqUrl member below to the faq page of the boolean website
40+
public String faqUrl = "";
741
}
Lines changed: 75 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,80 @@
11
package com.booleanuk.extension;
22

33
public class Extension {
4-
public static String sayHelloWorld() {
5-
return "Hello, world!";
4+
// In Java, Strings are immutable. If we want to change a string, we need to create new variables or members that
5+
// contain the new value. Consider the below code that builds a welcome message for a user:
6+
String message = "Hello";
7+
String message2 = message + ", John Smith!";
8+
String message3 = message2 + " Welcome to my app.";
9+
10+
// As you can see, it's difficult to construct something as simple as a welcome message this way. If we want to
11+
// work with a mutable string, we should use another approach. One option is to use the StringBuilder class:
12+
// https://docs.oracle.com/en/java/javase/18/docs/api/java.base/java/lang/StringBuilder.html
13+
14+
// The following block of code is a method. You've been using these during the core criteria, and this is how we
15+
// create our own methods. You'll be doing this in a future exercise. For now, write your code between the
16+
// WRITE YOUR CODE BETWEEN THIS LINE...
17+
// ... AND THIS LINE
18+
// See the below example:
19+
public void example() {
20+
// 0. Print the word "Hello"
21+
// WRITE YOUR CODE BETWEEN THIS LINE...
22+
System.out.println("Hello");
23+
// ...AND THIS LINE
24+
}
25+
26+
public StringBuilder one() {
27+
StringBuilder sb = new StringBuilder();
28+
29+
// 1. Using the sb variable above, add "Hello, world!" to the StringBuilder
30+
// WRITE YOUR CODE BETWEEN THIS LINE...
31+
32+
33+
34+
// ...AND THIS LINE
35+
36+
return sb;
37+
}
38+
39+
public StringBuilder two() {
40+
StringBuilder sb = new StringBuilder();
41+
42+
// 1. Using the sb variable above, add "Hello, world!" to the StringBuilder
43+
// 2. After adding the message, reverse it
44+
// WRITE YOUR CODE BETWEEN THIS LINE...
45+
46+
47+
48+
// ...AND THIS LINE
49+
50+
return sb;
51+
}
52+
53+
public StringBuilder three() {
54+
StringBuilder sb = new StringBuilder();
55+
56+
// 1. Using the sb variable above, add "Hello, world!" to the StringBuilder
57+
// 2. After adding the message, remove the comma.
58+
// WRITE YOUR CODE BETWEEN THIS LINE...
59+
60+
61+
62+
// ...AND THIS LINE
63+
64+
return sb;
65+
}
66+
67+
public StringBuilder four() {
68+
StringBuilder sb = new StringBuilder();
69+
70+
// 1. Using the sb variable above, add "Hello, world!" to the StringBuilder
71+
// 2. After adding the message, replace the word "world" with the word "Java"
72+
// WRITE YOUR CODE BETWEEN THIS LINE...
73+
74+
75+
76+
// ...AND THIS LINE
77+
78+
return sb;
679
}
780
}

src/test/java/com/booleanuk/ExerciseTest.java

Lines changed: 0 additions & 47 deletions
This file was deleted.
Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,47 @@
11
package com.booleanuk.core;
22

3-
import com.booleanuk.core.Exercise;
43
import org.junit.jupiter.api.Assertions;
54
import org.junit.jupiter.api.Test;
65

76
class ExerciseTest {
7+
Exercise exercise;
8+
9+
public ExerciseTest() {
10+
this.exercise = new Exercise();
11+
}
12+
13+
@Test
14+
public void urlShouldBeFixed() {
15+
Assertions.assertEquals(" https://booLeAn.co.uk/who-we-are ", this.exercise.fixedUrl);
16+
}
17+
18+
@Test
19+
public void urlShouldBeLowerCase() {
20+
Assertions.assertEquals(" https://boolean.co.uk/who-we-are ", this.exercise.lowerCasedUrl);
21+
}
22+
23+
@Test
24+
public void urlShouldBeTrimmed() {
25+
Assertions.assertEquals("https://boolean.co.uk/who-we-are", this.exercise.url);
26+
}
27+
28+
@Test
29+
public void protocolShouldBeHttps() {
30+
Assertions.assertEquals("https", this.exercise.protocol);
31+
}
32+
33+
@Test
34+
public void domainShouldBeBooleanCoUk() {
35+
Assertions.assertEquals("boolean.co.uk", this.exercise.domain);
36+
}
37+
38+
@Test
39+
public void lengthShouldBe32() {
40+
Assertions.assertEquals(32, this.exercise.length);
41+
}
42+
843
@Test
9-
public void shouldSayHelloWorld() {
10-
Assertions.assertEquals("Hello, world!", Exercise.sayHelloWorld());
44+
public void faqUrlShouldBeBooleanCoUkFaq() {
45+
Assertions.assertEquals("https://boolean.co.uk/faq", this.exercise.faqUrl);
1146
}
1247
}

src/test/java/com/booleanuk/extension/ExtensionTest.java

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,29 @@
44
import org.junit.jupiter.api.Test;
55

66
public class ExtensionTest {
7+
Extension extension;
8+
9+
public ExtensionTest() {
10+
this.extension = new Extension();
11+
}
12+
13+
@Test
14+
public void helloWorldShouldBeAppended() {
15+
Assertions.assertEquals("Hello, world!", this.extension.one().toString());
16+
}
17+
18+
@Test
19+
public void helloWorldShouldBeReversed() {
20+
Assertions.assertEquals("!dlrow ,olleH", this.extension.two().toString());
21+
}
22+
23+
@Test
24+
public void commaShouldBeRemoved() {
25+
Assertions.assertEquals("Hello world!", this.extension.three().toString());
26+
}
27+
728
@Test
8-
public void shouldSayHelloWorld() {
9-
Assertions.assertEquals("Hello, world!", Extension.sayHelloWorld());
29+
public void shouldSayHelloToJava() {
30+
Assertions.assertEquals("Hello, Java!", this.extension.four().toString());
1031
}
1132
}

0 commit comments

Comments
 (0)