Lektion 2: lösningsförslag

  1. // 1
  2. string s = "abc";
  3. bool authorized;
  4. if (s == "secret123") {
  5. authorized = true;
  6. }
  7. else {
  8. authorized = false;
  9. }
  10. // 1.1
  11. // Den första lösningen beskrev allting som ett enda uttryck medan denna förlitar sig på selektion och mutation.
  12. // 1.2
  13. string s = "abc";
  14. bool authorized;
  15. if (s == "secret123") {
  16. authorized = true;
  17. }
  18. if (s != "secret123") {
  19. authorized = false;
  20. }
  21. // 2
  22. // Betygsnivåer: G - 20, VG - 40, max - 60
  23. int points = 50;
  24. string grade;
  25. if (points < 20) {
  26. grade = "IG";
  27. }
  28. else if (points < 40) {
  29. grade = "G";
  30. }
  31. else {
  32. grade = "VG";
  33. }
  34. // 2.1
  35. int points = 50;
  36. string grade;
  37. if (points < 0) {
  38. grade = "ogiltigt";
  39. }
  40. else if (points < 20) {
  41. grade = "IG";
  42. }
  43. else if (points < 40) {
  44. grade = "G";
  45. }
  46. else if (points < 60){
  47. grade = "VG";
  48. }
  49. else {
  50. grade = "fusk!";
  51. }
  52. // 3
  53. string gender = "man";
  54. int age = 25;
  55. bool married = false;
  56. int children = 3;
  57. string car = "ABC123";
  58. string summary = gender + ", " + age;
  59. if (married)
  60. {
  61. summary += ", gift";
  62. }
  63. else
  64. {
  65. summary += ", ogift";
  66. }
  67. if (children > 0)
  68. {
  69. summary += ", " + children + " barn";
  70. }
  71. if (car != null)
  72. {
  73. summary += ", reg " + car;
  74. }
  75. // 4
  76. double salary = 30000;
  77. double tax = salary * 0.30;
  78. int cars = 0;
  79. int electricCars = 1;
  80. int flights = 2;
  81. bool familyAbroad = true;
  82. string building = "house";
  83. if (cars == 0)
  84. {
  85. tax = tax / 2;
  86. }
  87. else if (cars > 1)
  88. {
  89. tax += 2000 * (cars - 1);
  90. }
  91. if (electricCars >= 1)
  92. {
  93. tax -= 1000;
  94. }
  95. if (flights >= 2 && !familyAbroad)
  96. {
  97. tax = tax * 1.20;
  98. }
  99. if (building == "house")
  100. {
  101. tax -= 500;
  102. }
  103. else
  104. {
  105. tax -= 1000;
  106. }
  107. if (tax < 0)
  108. {
  109. tax = 0;
  110. }
  111. // 5
  112. (wand && hogwarts) || gandalf
  113. // 5.1
  114. // åtta:
  115. // (true, true, true)
  116. // (true, true, false)
  117. // (true, false, true)
  118. // (false, true, true)
  119. // (true, false, false)
  120. // (false, true, false)
  121. // (false, false, true)
  122. // (false, false, false)
  123. // antalet kombinationer fås genom antalet variabler i kvadrat
  124. // 5.2
  125. // Ja. Nu är Gandalf bara en magiker om han *inte* har en stav.
  126. (wand && hogwarts) || (!wand && gandalf)
  127. // 6.1
  128. int temperature = 20;
  129. bool comfortable;
  130. if (temperature >= 18) {
  131. if (temperature <= 26) {
  132. comfortable = true;
  133. }
  134. else {
  135. comfortable = false;
  136. }
  137. }
  138. else {
  139. comfortable = false;
  140. }
  141. // 6.2
  142. string country = "USA";
  143. int age = 40;
  144. int elected = 0;
  145. bool eligible;
  146. if (country == "USA") {
  147. if (age >= 35) {
  148. if (elected <= 1) {
  149. eligible = true;
  150. }
  151. else {
  152. eligible = false;
  153. }
  154. }
  155. else {
  156. eligible = false;
  157. }
  158. }
  159. else {
  160. eligible = false;
  161. }
  162. // 6.3
  163. string country = "USA";
  164. int age = 40;
  165. int elected = 0;
  166. bool savedTheWorld = true;
  167. bool eligible;
  168. if (country == "USA") {
  169. if (age >= 35) {
  170. if (elected <= 1) {
  171. eligible = true;
  172. }
  173. else {
  174. eligible = false;
  175. }
  176. }
  177. else {
  178. eligible = false;
  179. }
  180. }
  181. else {
  182. eligible = false;
  183. }
  184. if (savedTheWorld) {
  185. eligible = true;
  186. }
  187. // 7
  188. // övning 1:
  189. string s = "abc";
  190. bool authorized = s == "secret123" ? true : false;
  191. // övning 2:
  192. int points = 50;
  193. string grade = points < 20 ? "IG" : (points < 40 ? "G" : "VG");
  194. // 7.1
  195. // Ja, som i övning 2 ovan: med ett ytterligare "?" och ":" (helst inom parentes) efter det första ":".
  196. // 8
  197. // I synnerhet långa if-else-kedjor där varje villkor använder likhetsoperatorn (==) kan passa att skriva om med switch.