From c1a20bb3a4e30166191452991620315070aa6064 Mon Sep 17 00:00:00 2001 From: Rose Breedveld Date: Thu, 1 Feb 2024 13:36:18 +0100 Subject: [PATCH] Rose Breedveld Fundamentals- Methods core and Extentions done --- csharp-fundamentals-methods.Main/Core.cs | 29 +++++++++++++--- csharp-fundamentals-methods.Main/Extension.cs | 33 +++++++++++++++---- 2 files changed, 50 insertions(+), 12 deletions(-) diff --git a/csharp-fundamentals-methods.Main/Core.cs b/csharp-fundamentals-methods.Main/Core.cs index a69b542..d3f0ec4 100644 --- a/csharp-fundamentals-methods.Main/Core.cs +++ b/csharp-fundamentals-methods.Main/Core.cs @@ -27,16 +27,19 @@ 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(); + name = "Nathan"; + return "Hello " + name + "!"; } + //TODO: 2. Increment a number /* Complete this method so that it increases the number given by 1 and returns the result */ public int increment(int number) { - throw new NotImplementedException(); + number += 1; + return number; } //TODO: 3. Construct a friendly greeting @@ -50,7 +53,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(); + return $"Hi, {name} :)"; + } @@ -70,7 +74,12 @@ Create a method named constructNumberArray that accepts two whole numbers named public int[] constructNumberArray(int lower, int upper) { - int[] resultArray = { }; + int[] resultArray = new int[(upper - lower +1)]; + for (int i = 0; i < resultArray.Length; i++) + { + resultArray[i] = lower; + lower++; + } return resultArray; @@ -92,7 +101,17 @@ The number of exclamation marks appended must be determined by the number provid public string shout(string phrase, int number) { - return $""; + // make numer to a string --> use StringBuilder + // need a loop that counts the number in ! + // number in '!' .ToString() + // .ToUpper the phrase + StringBuilder sb = new StringBuilder(); + sb.Append(phrase); + for (int i = 0; i < number; i++) + { + sb.Append('!'); + } + return sb.ToString().ToUpper(); } diff --git a/csharp-fundamentals-methods.Main/Extension.cs b/csharp-fundamentals-methods.Main/Extension.cs index a4461e2..7eb7fa6 100644 --- a/csharp-fundamentals-methods.Main/Extension.cs +++ b/csharp-fundamentals-methods.Main/Extension.cs @@ -17,9 +17,14 @@ 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 minLeft) { - throw new NotImplementedException(); + if (minLeft == 0) { + return "The cake is ready!"; + } else if (minLeft > 0) { + return "The cake is still baking!"; + } + return "The timer finished ages ago!"; } @@ -33,14 +38,22 @@ 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 int estimatePrepTime(string[] strings, int v) { - throw new NotImplementedException(); + int prepTime = v; + // every ingredient must be count with two + if (v == 0) { + prepTime = 2; + } + // return preptime multiplied with the amount of strings .Length + return prepTime * 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 @@ -49,9 +62,15 @@ 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 int calculateGramsOfSugar(string[] strings, int v) { - throw new NotImplementedException(); + // if suger cointains in the list of strings add 100 gr per layer + if (strings.Contains("sugar")) + { + // multiply 100 gr of suger per layer + return 100 * v; + } + return 0; }