From feb2900257d97e6aa5821c7daaf37fe832df7795 Mon Sep 17 00:00:00 2001 From: Klara Andersson Date: Wed, 10 Jan 2024 09:38:54 +0100 Subject: [PATCH] Methods and extensions tests done --- csharp-fundamentals-methods.Main/Core.cs | 28 +++++++++++++++---- csharp-fundamentals-methods.Main/Extension.cs | 28 ++++++++++++------- 2 files changed, 40 insertions(+), 16 deletions(-) diff --git a/csharp-fundamentals-methods.Main/Core.cs b/csharp-fundamentals-methods.Main/Core.cs index a69b542..f266245 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(); + //throw new NotImplementedException(); + return "Hello " + name + "!"; } //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(); + //throw new NotImplementedException(); + return ++number; } //TODO: 3. Construct a friendly greeting @@ -50,7 +52,8 @@ Complete this method so that it accepts a name as an input and outputs a friendl */ public string happilyGreet(string name) { - throw new NotImplementedException(); + //throw new NotImplementedException(); + return "Hi, " + name + " :)"; } @@ -69,11 +72,16 @@ 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 counter = 0; - int[] resultArray = { }; + for (int i = lower; i <= upper; i++) + { + resultArray[counter] = i; + counter++; + } return resultArray; - } @@ -92,7 +100,15 @@ The number of exclamation marks appended must be determined by the number provid public string shout(string phrase, int number) { - return $""; + string marks = ""; + + for (int i = 0; i < number; i++) + { + marks = marks + "!"; + } + + + return phrase.ToUpper() + marks; } diff --git a/csharp-fundamentals-methods.Main/Extension.cs b/csharp-fundamentals-methods.Main/Extension.cs index a4461e2..57dcf6a 100644 --- a/csharp-fundamentals-methods.Main/Extension.cs +++ b/csharp-fundamentals-methods.Main/Extension.cs @@ -17,9 +17,12 @@ 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 v) { - throw new NotImplementedException(); + if (v == 0) return "The cake is ready!"; + else if (v > 0) return "The cake is still baking!"; + else if (v < 0) return "The timer finished ages ago!"; + else return "Nothing"; } @@ -35,12 +38,13 @@ provided and the prep time per ingredient. public double estimatePrepTime(string[] strings, int v) { - throw new NotImplementedException(); + if (v == 0) return 2 * strings.Length; + else return v * (double)strings.Length; } - //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. + //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: - an array of ingredients that will always contain 3 ingredients @@ -50,12 +54,16 @@ public double estimatePrepTime(string[] strings, int v) */ public double calculateGramsOfSugar(string[] strings, int v) - { - throw new NotImplementedException(); - } - - - + { + for (int i = 0; i < strings.Length; i++) + { + if (strings[i] == "sugar") + { + return 100 * v; + } + } + return 0; + } } }