Skip to content

Commit feb35ef

Browse files
author
gheijer
committed
Fresh init
1 parent c3e742b commit feb35ef

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

93 files changed

+5038
-2
lines changed

README.md

Lines changed: 0 additions & 2 deletions
This file was deleted.

Week01/W01_1/W_01_1_01_Lesson.cs

Lines changed: 268 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,268 @@
1+
/*
2+
Note that in this week we will not have the Main method or methods,
3+
It is just organized this way so it is easier for you to read
4+
The order of the methods is the order the lesson should be given
5+
6+
Contents:
7+
* Variables
8+
* Operators
9+
* Comments
10+
* Output
11+
* String Concatenation
12+
* Input
13+
* Conversion From String
14+
* Conversion Double Int
15+
* Conversion Int Double
16+
* Input ReadKey
17+
* Be Careful With Read (optional)
18+
* Types of errors - compiler errors, warnings, run time errors, logical errors
19+
20+
Class Exercises: W_01_1_C01 - W_01_1_C03
21+
*/
22+
public static class W_01_1_01_Lesson
23+
{
24+
public static void Variables()
25+
{
26+
//sizeof(int) if you are interested -> answer is in bytes
27+
string surname = "Smith"; //NA
28+
char initial; //16 bit
29+
int age = 24; //32 bits
30+
double height = 165.3; //64 bits
31+
bool male = false; //8 bits
32+
33+
// Error initial has not been assigned
34+
// char initialCopy = initial;
35+
initial = 'J';
36+
age = 25;
37+
38+
var message = "Hello";
39+
// Advise to avoid var in the beginning
40+
// Error must use var in one line
41+
// var city;
42+
// city = "Rotterdam";
43+
44+
string fName = "Tim", mName = "van de", lName = "Berg";
45+
}
46+
47+
public static void Operators()
48+
{
49+
int num1 = 10;
50+
int num2 = 4;
51+
52+
int sum = num1 + num2;
53+
int diff = num1 - num2;
54+
int mul = num1 * num2;
55+
// If you divide an integer by an integer then the result is an integer with the decimal part is truncated
56+
int div = num1 / num2;
57+
int mod1 = num1 % num2;
58+
59+
double num3 = 10.0;
60+
// If there is a double in the expression, the result will be a double
61+
double divDouble = num3 / num2;
62+
double myCalculation = 1 + 10 / 4 + 1.5;
63+
64+
//Shortcuts
65+
num1 = num1 + 1;
66+
num1++;
67+
++num1;
68+
int increase1 = num1++;
69+
int increase2 = ++num1;
70+
71+
num2 = num2 - 1;
72+
num2--;
73+
--num2;
74+
int decrease1 = num1--;
75+
int decrease2 = --num1;
76+
77+
num1 = num1 + 2;
78+
num1 += 2;
79+
// -= /= *=
80+
}
81+
82+
public static void Comments()
83+
{
84+
// This is a comment
85+
/*
86+
This
87+
is
88+
a
89+
multiline
90+
comment
91+
*/
92+
}
93+
94+
public static void Output()
95+
{
96+
Console.WriteLine("Hello, world!");
97+
98+
Console.Write("Why do Java programmers ");
99+
Console.Write("have to ");
100+
Console.WriteLine("wear glasses?");
101+
Console.WriteLine("Because they don't C#");
102+
}
103+
104+
public static void StringConcatenation()
105+
{
106+
string surname = "Smith";
107+
char initial = 'J';
108+
int age = 24;
109+
double height = 165.3;
110+
bool male = false;
111+
112+
age = 25;
113+
114+
string information = "Name: " + initial + ", " + surname + "\nAge: " + age +
115+
"\nHeight: " + height + "\nMale?: " + male;
116+
//need to turn on autocomplete in IDE
117+
string informationInterpolated = $"Name: {initial}, {surname}\nAge: {age}" +
118+
$"\nHeight: {height}\nMale?: {male}";
119+
Console.WriteLine(information);
120+
Console.WriteLine(informationInterpolated);
121+
}
122+
123+
public static void Input()
124+
{
125+
Console.WriteLine("What is your favorite quote?");
126+
// Explain warning: There is a chance that null (absence of a string) is stored
127+
// Will learn about null later in the course
128+
string quote = Console.ReadLine();
129+
Console.WriteLine($"{quote}\nThat is a nice quote!");
130+
}
131+
132+
public static void ConversionFromString()
133+
{
134+
string initialStr = "S";
135+
string ageStr = "24";
136+
string heightStr = "165.3";
137+
138+
/*
139+
Explain that ToChar, ToInt32 and ToDouble are methods (aka functions)
140+
Introduction to C# documentation
141+
https://learn.microsoft.com/en-us/dotnet/api/system.convert?view=net-6.0
142+
*/
143+
char initial = Convert.ToChar(initialStr);
144+
int age = Convert.ToInt32(ageStr);
145+
// Depending on locale settings you may need to enter a , or a .
146+
double height = Convert.ToDouble(heightStr);
147+
148+
/*
149+
Show that if you enter a value that cannot be converted
150+
you get an exception. Explain exceptions. From Microsoft:
151+
"Exceptions in C# provide a structured, uniform, and type-safe way of handling both
152+
system level and application-level error conditions."
153+
For now, in exercises they can assume input will be valid.
154+
Next lesson we will look at data validation. Later on in the course they will
155+
learn more about exceptions.
156+
*/
157+
}
158+
159+
public static void ConversionIntDouble()
160+
{
161+
// an int (32 bit) can fit in a double (64 bit) without data loss
162+
int myInt = 100;
163+
double convertIntToDouble = myInt;
164+
}
165+
166+
public static void ConversionDoubleInt()
167+
{
168+
/*
169+
When converting from double to int the decimal part
170+
is rounded towards zero to the nearest integral value
171+
(so effectively it is truncated)
172+
*/
173+
double aDouble = 9.18;
174+
int aDoubleToInt = (int)aDouble;
175+
double bDouble = 99.999;
176+
int bDoubleToInt = (int)bDouble;
177+
double cDouble = -8.63;
178+
int cDoubleToInt = (int)cDouble;
179+
180+
// Usually, if the double does fit inside an int a certain value is returned
181+
// No need to cover but FYI: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/statements/checked-and-unchecked
182+
double dDouble = int.MaxValue + 1.0;
183+
int dDoubleToInt = (int)dDouble;
184+
double eDouble = int.MaxValue + 100.0;
185+
int eDoubleToInt = (int)eDouble;
186+
187+
// Exception
188+
// int tryConvert = Convert.ToInt32(dDouble);
189+
}
190+
191+
// Ensure to tell the students that this will not work in CodeGrade
192+
public static void InputReadKey()
193+
{
194+
Console.WriteLine("What is your favorite letter?");
195+
char letter = Console.ReadKey().KeyChar;
196+
Console.WriteLine($"{letter} is a nice letter!");
197+
}
198+
199+
public static void Errors()
200+
{
201+
/*
202+
Compiler Error - error corresponding to syntax or semantics
203+
204+
No need to compile to see errors -
205+
VS Code uses built in analyzers to identify compiler errors as you type
206+
Identified by a red "squiggle" underscore. Hover over it for additional details.
207+
Also listed in "Problems" tab (click to go to error)
208+
Can use filter to see only errors
209+
210+
Read what the compiler says. Then, based on that, examine your program to determine what the problem is.
211+
212+
For more information on the error look the error code up here:
213+
https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-messages/
214+
*/
215+
// int x = 5 // Syntax error: missing semicolon
216+
// int x = 2; // Semantics error: Duplicate variables
217+
218+
219+
/*
220+
Warning - Code will still compile (and therefore run) but there may there may be mistakes
221+
222+
For more information on the error look the error code up here:
223+
https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-messages/
224+
*/
225+
int age = 5; // warning
226+
string quote = Console.ReadLine(); // nullable warning
227+
228+
/*
229+
Runtime errors - occur if an issue is encountered while the program is running
230+
231+
For more information on the error look the Exception name up here:
232+
https://learn.microsoft.com/en-us/dotnet/api/system?view=net-6.0
233+
*/
234+
string greeting = "hello";
235+
int num = Convert.ToInt32(greeting); // Exception has occurred: CLR/System.FormatException
236+
237+
/*
238+
Logical errors - No error or warning is given, but the code does not behave as you intend it to
239+
240+
These errors can be identified when testing your code.
241+
242+
To solve these errors, read and debug your code.
243+
*/
244+
int xPosition = 0;
245+
int moveRight = xPosition - 1;
246+
247+
int yPosition = 0;
248+
int moveUp = yPosition++;
249+
}
250+
251+
// Leave this out if you like, a little confusing perhaps?
252+
public static void BeCarefulWithRead()
253+
{
254+
Console.WriteLine("What is your favorite letter?");
255+
//letter is an int, not a char :(
256+
int letter = Console.Read();
257+
Console.WriteLine($"The letter {letter} is a nice letter");
258+
259+
Console.WriteLine("What is your second favorite letter?");
260+
// letter2 will be the enter character
261+
int letter2 = Console.Read();
262+
Console.WriteLine($"The letter {letter2} is also a nice letter");
263+
}
264+
265+
/**********************************************
266+
* W_01_1_C01 - W_01_1_C03 *
267+
***********************************************/
268+
}

0 commit comments

Comments
 (0)