From cca5231e817bef6df0f06761f6c1b4a68e3f8c24 Mon Sep 17 00:00:00 2001 From: henrikrosenkilde Date: Wed, 10 Jan 2024 09:51:14 +0100 Subject: [PATCH 1/2] =?UTF-8?q?fullf=C3=B8rt=20f=C3=B8rste=20tasks?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- csharp-fundamentals-methods.Main/Core.cs | 26 ++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/csharp-fundamentals-methods.Main/Core.cs b/csharp-fundamentals-methods.Main/Core.cs index a69b542..f378a62 100644 --- a/csharp-fundamentals-methods.Main/Core.cs +++ b/csharp-fundamentals-methods.Main/Core.cs @@ -27,7 +27,8 @@ logic. See the example below and take some time to break it down and understand //TODO: 1. Create a method that accepts a name and returns a greeting public string greet(string name) { - throw new NotImplementedException(); + string greeting = "Hello Nathan!"; + return greeting; } //TODO: 2. Increment a number @@ -36,7 +37,8 @@ Complete this method so that it increases the number given by 1 and returns the */ public int increment(int number) { - throw new NotImplementedException(); + int incrementedNumber = number + 1; + return incrementedNumber; } //TODO: 3. Construct a friendly greeting @@ -50,11 +52,13 @@ Complete this method so that it accepts a name as an input and outputs a friendl */ public string happilyGreet(string name) { - throw new NotImplementedException(); + string greeting = "Hi, " + name + " :)"; + return greeting; } + //TODO: 4. Construct an array of numbers /* Create a method named constructNumberArray that accepts two whole numbers named lower and upper. @@ -69,15 +73,19 @@ Create a method named constructNumberArray that accepts two whole numbers named public int[] constructNumberArray(int lower, int upper) { + int[] resultArray = new int[upper - lower + 1]; - int[] resultArray = { }; + for (int i = lower; i <= upper; i++) + { + resultArray[i - lower] = i; + } return resultArray; - } + //TODO: 5. Shout at a dev /* Create a method named shout that accepts a string and a whole number. @@ -92,9 +100,15 @@ The number of exclamation marks appended must be determined by the number provid public string shout(string phrase, int number) { - return $""; + string result = phrase.ToUpper(); + for (int i = 0; i < number; i++) + { + result += "!"; + } + return result; } + } } From 5f315ecf89a9c5396fba600acdd2cc48952e2f0f Mon Sep 17 00:00:00 2001 From: henrikrosenkilde Date: Wed, 10 Jan 2024 09:59:37 +0100 Subject: [PATCH 2/2] ferdig extensions --- csharp-fundamentals-methods.Main/Extension.cs | 48 ++++++++++++++++--- 1 file changed, 42 insertions(+), 6 deletions(-) diff --git a/csharp-fundamentals-methods.Main/Extension.cs b/csharp-fundamentals-methods.Main/Extension.cs index a4461e2..29e6c80 100644 --- a/csharp-fundamentals-methods.Main/Extension.cs +++ b/csharp-fundamentals-methods.Main/Extension.cs @@ -17,9 +17,20 @@ public class Extension "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 double timerStatus(int v) + public string timerStatus(int remainingMinutes) { - throw new NotImplementedException(); + if (remainingMinutes == 0) + { + return "The cake is ready!"; + } + else if (remainingMinutes > 0) + { + return "The cake is still baking!"; + } + else + { + return "The timer finished ages ago!"; + } } @@ -33,13 +44,20 @@ 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 double estimatePrepTime(string[] strings, int v) + public double estimatePrepTime(string[] ingredients, int prepTime) { - throw new NotImplementedException(); + if (prepTime == 0) + { + prepTime = 2; + } + + double totalPrepTime = prepTime * ingredients.Length; + return totalPrepTime; } + //TODO: Extension 3: calculateGramsOfSugar that accepts two parameters 1 an array of ingredients that will always contain 3 ingredients AND 2 the number of layers the cake has. The cake will need 100g of sugar per layer, if that ingredient is present in the provided list of ingredients. The method should return the number of grams of sugar needed to make the cake. /* 3. Create a method named calculateGramsOfSugar that accepts two parameters: @@ -49,13 +67,31 @@ public double estimatePrepTime(string[] strings, int v) The method should return the number of grams of sugar needed to make the cake. */ - public double calculateGramsOfSugar(string[] strings, int v) + public double calculateGramsOfSugar(string[] ingredients, int numLayers) { - throw new NotImplementedException(); + double sugarRequired = 0; + + bool sugarPresent = false; + + for (int i = 0; i < ingredients.Length; i++) + { + if (ingredients[i] == "sugar") + { + sugarPresent = true; + } + } + + if (sugarPresent) + { + sugarRequired = 100 * numLayers; + } + + return sugarRequired; } + } }