From 9c48f0ac115e79c6c55b71cf157d3e2f3e41bcc6 Mon Sep 17 00:00:00 2001 From: Annemoon_de_Groen Date: Wed, 10 Jan 2024 09:24:07 +0100 Subject: [PATCH] Finished exercise and extension --- csharp-fundamentals-methods.Main/Core.cs | 20 ++++++++++++------- csharp-fundamentals-methods.Main/Extension.cs | 20 +++++++++++++++---- 2 files changed, 29 insertions(+), 11 deletions(-) diff --git a/csharp-fundamentals-methods.Main/Core.cs b/csharp-fundamentals-methods.Main/Core.cs index a69b542..95215c9 100644 --- a/csharp-fundamentals-methods.Main/Core.cs +++ b/csharp-fundamentals-methods.Main/Core.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Collections.Immutable; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -27,7 +28,7 @@ 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(); + return $"Hello {name}!"; } //TODO: 2. Increment a number @@ -36,7 +37,7 @@ Complete this method so that it increases the number given by 1 and returns the */ public int increment(int number) { - throw new NotImplementedException(); + return number + 1; } //TODO: 3. Construct a friendly greeting @@ -50,7 +51,7 @@ 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} :)"; } @@ -69,9 +70,11 @@ 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; lower + i <= upper; i++) + { + resultArray[i] = lower + i; + } return resultArray; } @@ -92,7 +95,10 @@ 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 += "!"; + return phrase.ToUpper() + marks; } diff --git a/csharp-fundamentals-methods.Main/Extension.cs b/csharp-fundamentals-methods.Main/Extension.cs index a4461e2..589d40f 100644 --- a/csharp-fundamentals-methods.Main/Extension.cs +++ b/csharp-fundamentals-methods.Main/Extension.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -17,9 +18,13 @@ 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 + return "The timer finished ages ago!"; } @@ -35,7 +40,8 @@ provided and the prep time per ingredient. public double estimatePrepTime(string[] strings, int v) { - throw new NotImplementedException(); + if (v == 0) v = 2; + return strings.Length * v; } @@ -51,7 +57,13 @@ public double estimatePrepTime(string[] strings, int v) public double calculateGramsOfSugar(string[] strings, int v) { - throw new NotImplementedException(); + bool hasSugar = false; + foreach (string ingredient in strings) + { + if ( ingredient == "sugar") hasSugar = true; + } + if (hasSugar) return v * 100; + else return 0; }