From 99cc853e724324e6d33520a241cd7e794cfb7519 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matilda=20S=C3=B6nnergaard?= Date: Tue, 7 Jan 2025 17:44:38 +0100 Subject: [PATCH 1/2] Complete core exercises --- .../java/com/booleanuk/core/Exercise.java | 61 +++++++++++++++---- 1 file changed, 49 insertions(+), 12 deletions(-) diff --git a/src/main/java/com/booleanuk/core/Exercise.java b/src/main/java/com/booleanuk/core/Exercise.java index 70ac307..0560e0c 100644 --- a/src/main/java/com/booleanuk/core/Exercise.java +++ b/src/main/java/com/booleanuk/core/Exercise.java @@ -40,60 +40,72 @@ 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 "Wrong!"; + } else { + return "Correct!"; + } } // 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; + return nums.length > 0; } // 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; + return sentence.contains("milk"); } // 11. Use conditional statements to return the number 3 if the provided string contains @@ -101,12 +113,27 @@ 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")) { + if (sentence.contains("coffee")) { + return 9; + } + 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 (num >= lower && num <= upper); + if (num >= lower) { + if (num <= upper) { + return true; + } + } return false; } @@ -123,6 +150,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 <= 4) { + return "Toddler"; + } else if (age <= 12) { + return "Child"; + } else if (age <= 19) { + return "Teenager"; + } else { + return "Adult"; + } } } From b133c355326694442ff938a77b2a1cf53d84a2cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matilda=20S=C3=B6nnergaard?= Date: Tue, 7 Jan 2025 19:29:10 +0100 Subject: [PATCH 2/2] Complete extension exercises --- .../com/booleanuk/extension/Extension.java | 26 +++++++++++++++---- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/booleanuk/extension/Extension.java b/src/main/java/com/booleanuk/extension/Extension.java index 44e540f..601c652 100644 --- a/src/main/java/com/booleanuk/extension/Extension.java +++ b/src/main/java/com/booleanuk/extension/Extension.java @@ -13,9 +13,15 @@ public class Extension extends ExtensionBase { "The cake is still baking!" if there are any remaining minutes left, and "The timer finished ages ago!" if the remaining minutes is a negative number */ - - - + public String timerStatus(int minLeft) { + if (minLeft == 0) { + return "The cake is ready!"; + } else if (minLeft > 0) { + return "The cake is still baking!"; + } else { + return "The timer finished ages ago!"; + } + } /* 2. Create a method named estimatePrepTime that accepts two parameters: @@ -26,8 +32,11 @@ public class Extension extends ExtensionBase { provided and the prep time per ingredient. If a prep time of 0 is provided, the method should assume each ingredient takes 2 minutes to prepare. */ - - + public int estimatePrepTime(String[] ingredients, int timePerIngredient) { + if (timePerIngredient == 0) + timePerIngredient = 2; + return ingredients.length * timePerIngredient; + } /* 3. Create a method named calculateGramsOfSugar that accepts two parameters: @@ -40,6 +49,13 @@ public class Extension extends ExtensionBase { You may need to use programming techniques we have yet to cover in the course to solve this task. */ + public int calculateGramsOfSugar(String[] ingredients, int nLayers) { + boolean sugarIsIngredient = false; + for (String ingredient : ingredients) { + sugarIsIngredient |= ingredient.equals("sugar"); + } + return sugarIsIngredient ? nLayers * 100 : 0; + }