diff --git a/027_create_methods/027_csharp.md b/027_create_methods/027_csharp.md index c08bffc..3bd8e05 100644 --- a/027_create_methods/027_csharp.md +++ b/027_create_methods/027_csharp.md @@ -271,6 +271,8 @@ randomly generated numbers): 17 29 46 36 3 ``` +- [Program.cs](./methods/Program.cs) + If your code displays different results, you'll need to review your code to find your error and make updates. Run the code again to see if you've fixed the problem. Continue updating and running your code until your code produces the @@ -627,6 +629,8 @@ find your error and make updates. Run the code again to see if you've fixed the problem. Continue updating and running your code until your code produces the expected results. +- [Program.cs](./reusable_methods/Program.cs) + --- ## Exercise @@ -990,6 +994,8 @@ find your error and make updates. Run the code again to see if you've fixed the problem. Continue updating and running your code until your code produces the expected results. +- [Program.cs](./ipv4/Program.cs) + ### Recap Here's what you've learned about using methods so far: @@ -1114,7 +1120,7 @@ string[] text = { "Today is a day to", "Whatever work you do", "This is an ideal time to" -} +}; string[] good = { "look forward to.", "try new things!", @@ -1134,9 +1140,9 @@ string[] neutral = { "get in tune with nature." }; -TellFortune(); +tell_fortune(); -void TellFortune() { +void tell_fortune() { Console.WriteLine("A fortune teller whispers the following words:"); string[] fortune = (luck > 75 ? good : (luck < 25 ? bad : neutral)); for (int i = 0; i < 4; i++) { @@ -1176,6 +1182,8 @@ check in the next unit. units before you continue on. All new ideas we discuss in other modules will depend on your understanding of the ideas that were presented in this module. +- [Program.cs](./fortune/Program.cs) + --- ## Summary diff --git a/027_create_methods/fortune/Program.cs b/027_create_methods/fortune/Program.cs new file mode 100644 index 0000000..1925dfd --- /dev/null +++ b/027_create_methods/fortune/Program.cs @@ -0,0 +1,37 @@ +Random random = new Random(); +int luck = random.Next(100); + +string[] text = { + "You have much to", + "Today is a day to", + "Whatever work you do", + "This is an ideal time to" +}; +string[] good = { + "look forward to.", + "try new things!", + "is likely to succeed.", + "accomplish your dreams!" +}; +string[] bad = { + "fear.", + "avoid major decisions.", + "may have unexpected outcomes.", + "re-evaluate your life." +}; +string[] neutral = { + "appreciate.", + "enjoy time with friends.", + "should align with your values.", + "get in tune with nature." +}; + +void tell_fortune() { + Console.WriteLine("A fortune teller whispers the following words:"); + string[] fortune = (luck > 75 ? good : (luck < 25 ? bad : neutral)); + for (int i = 0; i < 4; i++) { + Console.WriteLine($"{text[i]} {fortune[i]} "); + } +} + +tell_fortune(); diff --git a/027_create_methods/fortune/fortune.csproj b/027_create_methods/fortune/fortune.csproj new file mode 100644 index 0000000..2150e37 --- /dev/null +++ b/027_create_methods/fortune/fortune.csproj @@ -0,0 +1,10 @@ + + + + Exe + net8.0 + enable + enable + + + diff --git a/027_create_methods/ipv4/Program.cs b/027_create_methods/ipv4/Program.cs new file mode 100644 index 0000000..4716072 --- /dev/null +++ b/027_create_methods/ipv4/Program.cs @@ -0,0 +1,61 @@ +/* +if ipAddress consists of 4 numbers +and +if each ipAddress number has no leading zeroes +and +if each ipAddress number is in range 0 - 255 + +then ipAddress is valid + +else ipAddress is invalid +*/ + +string ip_addr_1 = "94.88.1.16"; +string ip_addr_2 = "255.0.0.255"; +string ip_addr_3 = "555..0.555"; +string ip_addr_4 = "255.015.0.255"; +string ip_addr_5 = "16.f.0.16"; +string[] ip_addrs = {ip_addr_1, ip_addr_2, ip_addr_3, ip_addr_4, ip_addr_5}; + +bool validate_ip(string ipv4) { + bool valid = false; + bool lenght = false; + bool zeroes = false; + bool range = false; + string[] ip_segs = ipv4.Split('.'); + foreach (string segm in ip_segs){ + lenght = segm.Length>0 && segm.Length<=3; + if(lenght){ + Int16 segment; + valid = Int16.TryParse(segm, out segment); + if(!valid){ + Console.Write($"({segm}) is a invalid ipv4 segment! "); + return false; + } else { + if(segm.StartsWith("0") && segm.Length > 1 || segment < 0 || segment > 255){ + Console.Write($"({segm}) is a invalid ipv4 range! "); + return false; + } else { + range = true; + zeroes = true; + } + } + } else { + Console.Write($"({segm}) is a invalid ipv4 lenght! "); + return false; + } + } + if(valid && zeroes && lenght && range) { + return true; + } else { + return false; + } +} + +foreach (string ip_addr in ip_addrs){ + if(validate_ip(ip_addr)){ + Console.WriteLine($"{ip_addr} is a valid IP"); + } else { + Console.WriteLine($"{ip_addr} is not a valid IP"); + } +} diff --git a/027_create_methods/ipv4/ipv4.csproj b/027_create_methods/ipv4/ipv4.csproj new file mode 100644 index 0000000..2150e37 --- /dev/null +++ b/027_create_methods/ipv4/ipv4.csproj @@ -0,0 +1,10 @@ + + + + Exe + net8.0 + enable + enable + + + diff --git a/027_create_methods/methods/Program.cs b/027_create_methods/methods/Program.cs new file mode 100644 index 0000000..bfe7e62 --- /dev/null +++ b/027_create_methods/methods/Program.cs @@ -0,0 +1,33 @@ +Console.WriteLine("Before calling a method"); +SayHello(); +Console.WriteLine("After calling a method"); + +void SayHello() { + Console.WriteLine("Hello World!"); +} + +void DisplayRandomNumbers01() { + Random random = new Random(); +} + +void DisplayRandomNumbers02() { + Random random = new Random(); + + for (int i = 0; i < 5; i++) { + Console.Write($"{random.Next(1, 100)} "); + } + Console.WriteLine(); +} + +void DisplayRandomNumbers03() { + Random random = new Random(); + for (int i = 0; i < 5; i++) { + Console.Write($"{random.Next(1, 100)} "); + } + Console.WriteLine(); +} + +Console.WriteLine("\nGenerating random numbers:"); +DisplayRandomNumbers01(); +DisplayRandomNumbers02(); +DisplayRandomNumbers03(); diff --git a/027_create_methods/methods/methods.csproj b/027_create_methods/methods/methods.csproj new file mode 100644 index 0000000..2150e37 --- /dev/null +++ b/027_create_methods/methods/methods.csproj @@ -0,0 +1,10 @@ + + + + Exe + net8.0 + enable + enable + + + diff --git a/027_create_methods/reusable_methods/Program.cs b/027_create_methods/reusable_methods/Program.cs new file mode 100644 index 0000000..27f7df8 --- /dev/null +++ b/027_create_methods/reusable_methods/Program.cs @@ -0,0 +1,51 @@ +//using System; +int[] times = {800, 1200, 1600, 2000}; +int diff = 0; + +/* Format and display medicine times */ +void display_times(int[] times){ + foreach (int val in times) { + string time = val.ToString(); + int len = time.Length; + if (len >= 3) { + time = time.Insert(len - 2, ":"); + } + else if (len == 2) { + time = time.Insert(0, "0:"); + } else { + time = time.Insert(0, "0:0"); + } + Console.Write($"{time} "); + } + Console.WriteLine(); +} + +void adjust_times(int diff){ + // Adjust the times by adding the difference, + // keeping the value within 24 hours + for (int i = 0; i < times.Length; i++) { + times[i] = ((times[i] + diff)) % 2400; + } +} + +Console.WriteLine("Enter current GMT"); +int current_GMT = Convert.ToInt32(Console.ReadLine()); + +Console.WriteLine("Current Medicine Schedule:"); +display_times(times); + +Console.WriteLine("Enter new GMT"); +int new_GMT = Convert.ToInt32(Console.ReadLine()); + +if (Math.Abs(new_GMT) > 12 || Math.Abs(current_GMT) > 12) { + Console.WriteLine("Invalid GMT"); +} else if (new_GMT <= 0 && current_GMT <= 0 || new_GMT >= 0 && current_GMT >= 0) { + diff = 100 * (Math.Abs(new_GMT) - Math.Abs(current_GMT)); + adjust_times(diff); +} else { + diff = 100 * (Math.Abs(new_GMT) + Math.Abs(current_GMT)); + adjust_times(diff); +} + +Console.WriteLine("New Medicine Schedule:"); +display_times(times); diff --git a/027_create_methods/reusable_methods/reusable_methods.csproj b/027_create_methods/reusable_methods/reusable_methods.csproj new file mode 100644 index 0000000..2150e37 --- /dev/null +++ b/027_create_methods/reusable_methods/reusable_methods.csproj @@ -0,0 +1,10 @@ + + + + Exe + net8.0 + enable + enable + + +