Advertisement
ada1711

Untitled

Jul 20th, 2024
15
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.72 KB | None | 0 0
  1. **Lesson 6. Conditional Instruction IF - Continuation**
  2.  
  3. **Lesson Objective:**
  4.  
  5. The goal of the lesson is to reinforce information about the IF conditional instruction and understand the functionality of the Random class.
  6.  
  7. **Agenda:**
  8. 1. Questions and warm-up task
  9. 2. Review tasks
  10. 3. Random class
  11. 4. Summary task
  12.  
  13. **Questions from the previous lesson:**
  14. a) What is a conditional instruction and what is it used for?
  15. b) What instruction did you learn in the last lesson?
  16. c) What is the keyword `else` used for?
  17.  
  18. **Warm-up Task:**
  19. Write a console application that takes from the user the symbol or atomic number of an element and prints the element's name in the console. Include examples for 7 selected elements. The program should also display an appropriate message if the element is not found.
  20.  
  21. ```csharp
  22. Console.WriteLine("Enter the symbol or atomic number of the element:");
  23.  
  24. string elementData = Console.ReadLine();
  25.  
  26. if (elementData == "H" || elementData == "1")
  27. {
  28. Console.WriteLine("The element is -> Hydrogen");
  29. }
  30. else if (elementData == "Co" || elementData == "27")
  31. {
  32. Console.WriteLine("The element is -> Cobalt");
  33. }
  34. else if (elementData == "K" || elementData == "19")
  35. {
  36. Console.WriteLine("The element is -> Potassium");
  37. }
  38. else if (elementData == "Ag" || elementData == "47")
  39. {
  40. Console.WriteLine("The element is -> Silver");
  41. }
  42. else if (elementData == "Au" || elementData == "79")
  43. {
  44. Console.WriteLine("The element is -> Gold");
  45. }
  46. else if (elementData == "Mg" || elementData == "12")
  47. {
  48. Console.WriteLine("The element is -> Magnesium");
  49. }
  50. else if (elementData == "He" || elementData == "2")
  51. {
  52. Console.WriteLine("The element is -> Helium");
  53. }
  54. else
  55. {
  56. Console.WriteLine("No such element found");
  57. }
  58.  
  59. Console.ReadLine();
  60. ```
  61.  
  62. Mention to participants that if only one instruction needs to be executed for a fulfilled condition, braces can be omitted, using a one-line if statement.
  63.  
  64. ```csharp
  65. if (elementData == "H" || elementData == "1")
  66. Console.WriteLine("The element is -> Hydrogen");
  67. ```
  68.  
  69. **Task 1. Online Store (nested conditional instructions)**
  70. Write a console application that simulates a simple online store with the option to choose 2-3 items or games. The program should take into account the final price of the order and whether the user has a discount code that gives a 15% discount.
  71.  
  72. ```csharp
  73. static void Main(string[] args)
  74. {
  75. float keyboardPrice = 100f;
  76. float mousePrice = 80f;
  77. string promoCode = "Gigant15%";
  78. Console.WriteLine("Welcome! \nChoose the number of the item you are interested in: \n" +
  79. $"1 - Backlit Keyboard - {keyboardPrice:C}\n" +
  80. $"2 - Gaming Mouse - {mousePrice:C}");
  81.  
  82. int choice = int.Parse(Console.ReadLine());
  83. if (choice == 1)
  84. {
  85. Console.WriteLine("Excellent choice! Enter the promo code: ");
  86. string userPromoCode = Console.ReadLine();
  87. if (userPromoCode == promoCode)
  88. {
  89. Console.WriteLine($"Amount to pay {keyboardPrice * 0.85f}");
  90. }
  91. else
  92. {
  93. Console.WriteLine($"Amount to pay {keyboardPrice}");
  94. }
  95. }
  96. else if (choice == 2)
  97. {
  98. // similar to 1
  99. }
  100. else
  101. {
  102. Console.WriteLine("Unfortunately, it seems we don't have what you're looking for.");
  103. }
  104. Console.ReadKey();
  105. }
  106. ```
  107.  
  108. **Task 2. Game – Quiz Giants**
  109. Create a simple console application that simulates a quiz with multiple-choice questions and four answers for each. Create the initial part of the code together with students. Add 2-3 sample questions and implement the basic part of the code, then give time to add 2-3 own questions. Show the possibilities of the `toUpper()` and `toLower()` methods to change the characters entered by the user to uppercase or lowercase.
  110.  
  111. ```csharp
  112. static void Main(string[] args)
  113. {
  114. Console.WriteLine("Welcome to the Giants Quiz");
  115. int correctAnswers = 0;
  116. bool continuePlaying = true;
  117. char answer;
  118.  
  119. string question1 = "First question: Which of the following is not a programming language?";
  120. string ans1A = "Java";
  121. string ans1B = "C#";
  122. string ans1C = "HTML";
  123. string ans1D = "Python";
  124. string question2 = "Second question: Which of the following is not an operating system?";
  125. string ans2A = "Windows";
  126. string ans2B = "Linux";
  127. string ans2C = "Android";
  128. string ans2D = "Microsoft";
  129.  
  130. Console.WriteLine(question1);
  131. Console.WriteLine($" A: {ans1A}\n B: {ans1B}\n C: {ans1C}\n D: {ans1D}");
  132. Console.WriteLine("Choose your answer: ");
  133. answer = char.Parse(Console.ReadLine());
  134. if (char.ToUpper(answer) == 'C')
  135. {
  136. correctAnswers++;
  137. Console.WriteLine("Well done, let's continue! Press any key");
  138. Console.ReadKey();
  139. Console.Clear(); // clear console screen
  140. }
  141. else
  142. {
  143. Console.WriteLine("Sorry, the correct answer is C. Press any key");
  144. continuePlaying = false;
  145. Console.ReadKey();
  146. Console.Clear();
  147. }
  148. if (continuePlaying)
  149. {
  150. Console.WriteLine(question2);
  151. Console.WriteLine($" A: {ans2A}\n B: {ans2B}\n C: {ans2C}\n D: {ans2D}");
  152. Console.WriteLine("Choose your answer: ");
  153. answer = char.Parse(Console.ReadLine());
  154. if (char.ToUpper(answer) == 'D')
  155. {
  156. correctAnswers++;
  157. Console.WriteLine("Well done, let's continue! Press any key");
  158. Console.ReadKey();
  159. Console.Clear();
  160. }
  161. else
  162. {
  163. Console.WriteLine("Sorry, the correct answer is D. Press any key");
  164. continuePlaying = false;
  165. Console.ReadKey();
  166. Console.Clear();
  167. }
  168. }
  169. Console.WriteLine($"You gave {correctAnswers} correct answers");
  170. Console.ReadLine();
  171. }
  172. ```
  173.  
  174. **Random Class**
  175.  
  176. In programming, we can use additional functionalities that are hidden in special objects of various classes. So far, we have learned the classes Console, String, and Math. Another useful class is Random, which is a generator of randomness. It allows for generating pseudo-random numbers within a specified range.
  177.  
  178. Why "Pseudo"? These values are never truly random because they are generated based on a specific seed given when initializing the Random object, i.e., `new Random(12345)`, where 12345 is the seed. If we do not provide the seed value in the constructor, it will default to the current system time. If we always generated numbers based on the same seed, we would get the same sequence of numbers every time.
  179.  
  180. To generate a value, we first need to create a Random class object, and then create a numeric variable to which we will assign the result of the `maszynaLosujaca.Next()` method. This method can take the range as parameters, e.g., for 1 - 100, we call `generator.Next(1, 101);` Note that numbers are generated up to but not including the given range, so we will never get the value given as the second parameter. If we want to draw values up to 100, we must specify 101. In other words, the minimum value is included in the draw range, the maximum value is not.
  181.  
  182. ```csharp
  183. Random maszynaLosujaca = new Random();
  184. int drawnValue = maszynaLosujaca.Next(1, 101);
  185. ```
  186.  
  187. **Task 3. Guessing Game**
  188. Create a console application that draws a number from the range of 1 to 20, and the user has to guess it in three attempts. After each answer, a hint should be displayed whether the entered number is less than or greater than the sought value. The program should inform how many attempts it took to guess the number.
  189.  
  190. ```csharp
  191. // Create random number generator and draw a value
  192. Random randomMachine = new Random();
  193. int numberToGuess = randomMachine.Next(1, 21);
  194. int currentAttempt = 0;
  195. int enteredNumber;
  196.  
  197. // First attempt
  198. Console.WriteLine("Enter a number (1-20): ");
  199. enteredNumber = int.Parse(Console.ReadLine());
  200. currentAttempt++;
  201. if (enteredNumber > numberToGuess)
  202. {
  203. Console.WriteLine("Not quite, my number is smaller than the entered one");
  204. }
  205. else if (enteredNumber < numberToGuess)
  206. {
  207. Console.WriteLine("Not quite, my number is greater than the entered one");
  208. }
  209. else
  210. {
  211. Console.WriteLine($"Well done, you guessed the drawn number in {currentAttempt} attempt(s)!");
  212. Console.ReadLine();
  213. return;
  214. }
  215.  
  216. // Second attempt
  217. Console.WriteLine("Enter a number (1-20): ");
  218. enteredNumber = int.Parse(Console.ReadLine());
  219. currentAttempt++;
  220. if (enteredNumber > numberToGuess)
  221. {
  222. Console.WriteLine("Not quite, my number is smaller than the entered one");
  223. }
  224. else if (enteredNumber < numberToGuess)
  225. {
  226. Console.WriteLine("Not quite, my number is greater than the entered one");
  227. }
  228. else
  229. {
  230. Console.WriteLine($"Well done, you guessed the drawn number in {currentAttempt} attempt(s)!");
  231.  
  232.  
  233. Console.ReadLine();
  234. return;
  235. }
  236.  
  237. // Third attempt
  238. Console.WriteLine("Enter a number (1-20): ");
  239. enteredNumber = int.Parse(Console.ReadLine());
  240. currentAttempt++;
  241. if (enteredNumber > numberToGuess)
  242. {
  243. Console.WriteLine("Not quite, my number is smaller than the entered one");
  244. Console.WriteLine($"Unfortunately, you didn't guess the number. The correct number is: {numberToGuess}");
  245. }
  246. else if (enteredNumber < numberToGuess)
  247. {
  248. Console.WriteLine("Not quite, my number is greater than the entered one");
  249. Console.WriteLine($"Unfortunately, you didn't guess the number. The correct number is: {numberToGuess}");
  250. }
  251. else
  252. {
  253. Console.WriteLine($"Well done, you guessed the drawn number in {currentAttempt} attempt(s)!");
  254. Console.ReadLine();
  255. return;
  256. }
  257. Console.ReadLine();
  258. ```
  259.  
  260. **Final Questions:**
  261. - What is nested conditional instruction?
  262. - What do we use the `Console.Clear()` function for?
  263. - What does the `char.ToUpper()` function do?
  264. - How does random number generation work?
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement