From 8fc87a09f2e9965f4192033b59a41dc8b675e985 Mon Sep 17 00:00:00 2001 From: Erik Bergvall Date: Tue, 7 Jan 2025 15:05:42 +0100 Subject: [PATCH] Completed the base exercise --- .../java/com/booleanuk/core/Exercise.java | 70 +++++++++++++++---- 1 file changed, 57 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/booleanuk/core/Exercise.java b/src/main/java/com/booleanuk/core/Exercise.java index 70ac307..006c25d 100644 --- a/src/main/java/com/booleanuk/core/Exercise.java +++ b/src/main/java/com/booleanuk/core/Exercise.java @@ -40,60 +40,82 @@ public String sayGoodMorning(boolean isMorning) { // 1. What will the returned value be if I run sayGoodMorning(false)? // Change the returned value in the method below to your answer. It is case-sensitive. public String one() { - return ""; + return "Good day!"; } // 2. What will the output be if I run sayGoodMorning(true)? // Change the returned value in the method below to your answer. It is case-sensitive. public String two() { - return ""; + return "Good morning!"; } // 3. What will the output be if I run sayGoodMorning("Hello".equals("Hello"))? // Change the returned value in the method below to your answer. It is case-sensitive. public String three() { - return ""; + return "Good morning!"; } // 4. What will the output be if I run sayGoodMorning(!"A word".equals("Another word")) public String four() { - return ""; + return "Good morning!"; } // 5. What will the output be if I run sayGoodMorning(25 != 25) public String five() { - return ""; + return "Good day!"; } // 6. Use a conditional statement to return "Correct!" if the input is more than 7 // or "Wrong!" if not public String six(int num) { - return "Not implemented yet!"; + if (num > 7){ + return "Correct!"; + } + else { + return "Wrong!"; + } } // 7. Use a conditional statement to return "Correct!" if the input is false // or "Wrong!" if not public String seven(boolean bool) { - return "Not implemented yet!"; + if (!bool){ + return "Correct!"; + } else { + return "Wrong!"; + } } // 8. Use a conditional statement to return "Correct!" if numOne is more than or equal to numTwo // or "Wrong!" if not public String eight(int numOne, int numTwo) { - return "Not implemented yet!"; + if (numOne >= numTwo){ + return "Correct!"; + }else { + return "Wrong!"; + } } // 9. Use a conditional statement to return true if the array provided is not empty // or false if it is empty public boolean nine(int[] nums) { - return false; + if (nums == null || nums.length == 0){ + return false; + } else{ + return true; + } } // 10. Use a conditional statement to return true if the provided string contains the word // "milk", or false if not // https://www.w3schools.com/java/java_ref_string.asp public boolean ten(String sentence) { - return false; + if (sentence.contains("milk")){ + return true; + } + else { + return false; + } } // 11. Use conditional statements to return the number 3 if the provided string contains @@ -101,13 +123,25 @@ public boolean ten(String sentence) { // Return the number 9 if the string contains both coffee and milk. // Otherwise, return the number 0. public int eleven(String sentence) { - return -1; + if(sentence.contains("milk") && sentence.contains("coffee")){ + return 9; + }else if (sentence.contains("milk")){ + return 3; + } else if (sentence.contains(("coffee"))){ + return 6; + } else { + return 0; + } } // 12. Use conditional statements to return true if num is more than or equal to lower and is // less than or equal to upper, otherwise return false. public boolean twelve(int num, int lower, int upper) { - return false; + if(num >= lower && num <= upper){ + return true; + } else { + return false; + } } /* @@ -123,6 +157,16 @@ public boolean twelve(int num, int lower, int upper) { 20+ | Adult */ public String thirteen(int age) { - return "Not implemented yet!"; + if (age == 0){ + return "Baby"; + } else if( age < 5){ + return "Toddler"; + } else if (age < 13) { + return "Child"; + } else if (age < 20) { + return "Teenager"; + } else { + return "Adult"; + } } }