This commit is contained in:
ipvg 2024-08-10 21:03:39 -04:00
parent 9aa8bc1e4a
commit af5707eea8
9 changed files with 233 additions and 3 deletions

View File

@ -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

View 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();

View 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>

View 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");
}
}

View 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>

View 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();

View 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>

View 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);

View 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>