From 7f882d8418c1dd7fe2c6e5ef99cb14fd51a23512 Mon Sep 17 00:00:00 2001 From: Marit Date: Wed, 10 Jan 2024 15:58:13 +0100 Subject: [PATCH 1/2] Solved core and extension exercises. --- src/main/java/com/booleanuk/core/Exercise.java | 15 ++++++++------- .../java/com/booleanuk/extension/Extension.java | 11 +++++++---- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/booleanuk/core/Exercise.java b/src/main/java/com/booleanuk/core/Exercise.java index 5b1fe79..be533ec 100644 --- a/src/main/java/com/booleanuk/core/Exercise.java +++ b/src/main/java/com/booleanuk/core/Exercise.java @@ -1,5 +1,6 @@ package com.booleanuk.core; + public class Exercise { // A URL consists of these parts: [protocol]://[domain]/[path] public String brokenUrl = " httpz://booLeAn.co.uk/who-we-are "; @@ -7,7 +8,7 @@ public class Exercise { // 1. The brokenUrl member above contains an invalid URL. There's a z instead of an s in the protocol (httpz instead of https). // Using the `replace` method on brokenUrl, set the fixedUrl member below to the correct value. // https://docs.oracle.com/en/java/javase/18/docs/api/java.base/java/lang/String.html#replace(char,char) - public String fixedUrl = ""; + public String fixedUrl = brokenUrl.replace("z", "s"); // Here's a documentation link for all string methods, use it to figure out how to complete the rest of these requirements: @@ -16,26 +17,26 @@ public class Exercise { // 2. There are currently some upper case characters in the URL. Using an appropriate string method on the fixedUrl member above, // set the value of lowerCasedUrl. - public String lowerCasedUrl = ""; + public String lowerCasedUrl = fixedUrl.toLowerCase(); // 3. There is still white space on both ends of the URL! Use the appropriate string method to trim that white space // and set the value of the url member below - public String url = ""; + public String url = lowerCasedUrl.trim(); // 4. Using the appropriate string method on url, set the value of the protocol member below - public String protocol = ""; + public String protocol = url.substring(0,5); // 5. Using the appropriate string method on url, set the value of the domain member below - public String domain = ""; + public String domain = url.substring(8,21); // 6. Set the length member below to the length of the url member - public int length = 0; + public int length = url.length(); // 7. Using concatenation and existing members, set the faqUrl member below to the faq page of the boolean website - public String faqUrl = ""; + public String faqUrl = url.substring(0,22).concat("faq"); } diff --git a/src/main/java/com/booleanuk/extension/Extension.java b/src/main/java/com/booleanuk/extension/Extension.java index 5f84cdb..f48cf62 100644 --- a/src/main/java/com/booleanuk/extension/Extension.java +++ b/src/main/java/com/booleanuk/extension/Extension.java @@ -29,7 +29,7 @@ public StringBuilder one() { // 1. Using the sb variable above, add "Hello, world!" to the StringBuilder // WRITE YOUR CODE BETWEEN THIS LINE... - + sb.append("Hello, world!"); // ...AND THIS LINE @@ -43,7 +43,8 @@ public StringBuilder two() { // 2. After adding the message, use an appropriate StringBuilder method to reverse it // WRITE YOUR CODE BETWEEN THIS LINE... - + sb.append("Hello, world!"); + sb.reverse(); // ...AND THIS LINE @@ -57,7 +58,8 @@ public StringBuilder three() { // 2. After adding the message, remove the comma. // WRITE YOUR CODE BETWEEN THIS LINE... - + sb.append("Hello, world!"); + sb.deleteCharAt(5); // ...AND THIS LINE @@ -71,7 +73,8 @@ public StringBuilder four() { // 2. After adding the message, replace the word "world" with the word "Java" // WRITE YOUR CODE BETWEEN THIS LINE... - + sb.append("Hello, world!"); + sb.replace(7, 12, "Java"); // ...AND THIS LINE From 79031e18b788037c571bd8260b18bcc4f5a736b5 Mon Sep 17 00:00:00 2001 From: Marit Date: Wed, 10 Jan 2024 16:00:30 +0100 Subject: [PATCH 2/2] Solved core and extension exercises. --- src/main/java/com/booleanuk/core/Exercise.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/com/booleanuk/core/Exercise.java b/src/main/java/com/booleanuk/core/Exercise.java index be533ec..10b3abe 100644 --- a/src/main/java/com/booleanuk/core/Exercise.java +++ b/src/main/java/com/booleanuk/core/Exercise.java @@ -1,6 +1,5 @@ package com.booleanuk.core; - public class Exercise { // A URL consists of these parts: [protocol]://[domain]/[path] public String brokenUrl = " httpz://booLeAn.co.uk/who-we-are ";