end 027
This commit is contained in:
parent
9aa8bc1e4a
commit
af5707eea8
@ -271,6 +271,8 @@ randomly generated numbers):
|
|||||||
17 29 46 36 3
|
17 29 46 36 3
|
||||||
```
|
```
|
||||||
|
|
||||||
|
- [Program.cs](./methods/Program.cs)
|
||||||
|
|
||||||
If your code displays different results, you'll need to review your code to
|
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
|
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
|
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
|
problem. Continue updating and running your code until your code produces the
|
||||||
expected results.
|
expected results.
|
||||||
|
|
||||||
|
- [Program.cs](./reusable_methods/Program.cs)
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## Exercise
|
## 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
|
problem. Continue updating and running your code until your code produces the
|
||||||
expected results.
|
expected results.
|
||||||
|
|
||||||
|
- [Program.cs](./ipv4/Program.cs)
|
||||||
|
|
||||||
### Recap
|
### Recap
|
||||||
|
|
||||||
Here's what you've learned about using methods so far:
|
Here's what you've learned about using methods so far:
|
||||||
@ -1114,7 +1120,7 @@ string[] text = {
|
|||||||
"Today is a day to",
|
"Today is a day to",
|
||||||
"Whatever work you do",
|
"Whatever work you do",
|
||||||
"This is an ideal time to"
|
"This is an ideal time to"
|
||||||
}
|
};
|
||||||
string[] good = {
|
string[] good = {
|
||||||
"look forward to.",
|
"look forward to.",
|
||||||
"try new things!",
|
"try new things!",
|
||||||
@ -1134,9 +1140,9 @@ string[] neutral = {
|
|||||||
"get in tune with nature."
|
"get in tune with nature."
|
||||||
};
|
};
|
||||||
|
|
||||||
TellFortune();
|
tell_fortune();
|
||||||
|
|
||||||
void TellFortune() {
|
void tell_fortune() {
|
||||||
Console.WriteLine("A fortune teller whispers the following words:");
|
Console.WriteLine("A fortune teller whispers the following words:");
|
||||||
string[] fortune = (luck > 75 ? good : (luck < 25 ? bad : neutral));
|
string[] fortune = (luck > 75 ? good : (luck < 25 ? bad : neutral));
|
||||||
for (int i = 0; i < 4; i++) {
|
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
|
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.
|
depend on your understanding of the ideas that were presented in this module.
|
||||||
|
|
||||||
|
- [Program.cs](./fortune/Program.cs)
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## Summary
|
## Summary
|
||||||
|
37
027_create_methods/fortune/Program.cs
Normal file
37
027_create_methods/fortune/Program.cs
Normal file
@ -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();
|
10
027_create_methods/fortune/fortune.csproj
Normal file
10
027_create_methods/fortune/fortune.csproj
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<OutputType>Exe</OutputType>
|
||||||
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
</Project>
|
61
027_create_methods/ipv4/Program.cs
Normal file
61
027_create_methods/ipv4/Program.cs
Normal file
@ -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");
|
||||||
|
}
|
||||||
|
}
|
10
027_create_methods/ipv4/ipv4.csproj
Normal file
10
027_create_methods/ipv4/ipv4.csproj
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<OutputType>Exe</OutputType>
|
||||||
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
</Project>
|
33
027_create_methods/methods/Program.cs
Normal file
33
027_create_methods/methods/Program.cs
Normal file
@ -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();
|
10
027_create_methods/methods/methods.csproj
Normal file
10
027_create_methods/methods/methods.csproj
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<OutputType>Exe</OutputType>
|
||||||
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
</Project>
|
51
027_create_methods/reusable_methods/Program.cs
Normal file
51
027_create_methods/reusable_methods/Program.cs
Normal file
@ -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);
|
10
027_create_methods/reusable_methods/reusable_methods.csproj
Normal file
10
027_create_methods/reusable_methods/reusable_methods.csproj
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<OutputType>Exe</OutputType>
|
||||||
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
</Project>
|
Loading…
Reference in New Issue
Block a user