Advertisement
Guest User

Untitled

a guest
Jan 17th, 2019
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 31.67 KB | None | 0 0
  1.  
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  6. <title>Programming 1 - Java 2</title>
  7. </head>
  8. <body>
  9. </style>
  10. <div class="parameter" id="courseNo">CI4105-2019-Java</div>
  11. <div class="parameter" id="courseName">Programming 1</div>
  12. <div class="parameter" id="lessonNo">2</div>
  13. <div class="parameter" id="lessonName">Java 2</div>
  14. <div class="parameter" id="learningOutcomes">n/a</div>
  15. <div class="parameter" id="level">4</div>
  16. <div class="parameter" id="notes">n/a</div>
  17. <div class="parameter" id="language">java</div>
  18. <div class="parameter" id="multi">false</div>
  19. <div class="parameter" id="lockPlace">90.243.114.177,141.241.26.7,141.241.26.109,141.241.26.196</div>
  20. <div class="parameter" id="lockTimeDay">Fri 00:00-14:00</div>
  21. <div class="parameter" id="courseNavUrl">https://kunet.kingston.ac.uk/ku13043/TLAP/1819/javacanvas.html</div>
  22. <div class="parameter" id="lectureSlideUrl">https://kingston.app.box.com/embed/s/adnhtkcddb49pqpxmrge2h2ktxzolo1j</div>
  23. <!--<div class="parameter" id="videoUrl">https://kingston.app.box.com/embed/s/1l1zxl3bpdyxs5picghnr6v3cnq2rc27</div>-->
  24. <h1>Introduction to Java</h1>
  25. <div class="section" id="Converting from Python">
  26. <h2 class="title">Simple pseudocode conversions</h2>
  27. <p>You should have had enough of an exposure to Java during the lecture, and you'll have seen equivalents of all the constructs that you used during your work in Python. Let's start things off this week with a bit of simple conversion to Java from Python.</p>
  28.  
  29. <h3>Bronze medal</h3>
  30.  
  31. <p> Convert the following Python into Java</p>
  32.  
  33. <pre class="prettyprint nopaste">count = 10
  34. for i in range (1,11):
  35. count = count + count * i
  36. print "The number is now "+count</pre>
  37.  
  38. <div class="testCase">
  39. <!-- next line is alternative way of specifying ID for a test case -->
  40. <!-- Should be used where there are two versions of the same test, e.g.
  41. gold/silver/bronze, as strictly speaking having two elements with
  42. the same ID in XML/HTML is illegal, although most browsers forgive
  43. you -->
  44. <div class="medalType">bronze</div>
  45. <div class="medalDesc">The Road To Damascus</div>
  46. <div class="id">Conversion</div>
  47. <div class="test code runmain">
  48.  
  49. String[] lines = consoleOutputAsLines();
  50.  
  51. if (lines.length != 10)
  52. {
  53. feedback("There doesn't seem to the right number of lines in your output. Perhaps your loop isn't quite right...");
  54. return false;
  55. }
  56.  
  57. if (lines[0].equals("The number is now 20") &&
  58. lines[1].equals("The number is now 60") &&
  59. lines[2].equals("The number is now 240") &&
  60. lines[3].equals("The number is now 1200") &&
  61. lines[4].equals("The number is now 7200") &&
  62. lines[5].equals("The number is now 50400") &&
  63. lines[6].equals("The number is now 403200") &&
  64. lines[7].equals("The number is now 3628800") &&
  65. lines[8].equals("The number is now 36288000") &&
  66. lines[9].equals("The number is now 399168000") &&
  67. lines.length == 10)
  68. {
  69. return true;
  70. }
  71.  
  72. if (!consoleOutput().contains("20") ||
  73. !consoleOutput().contains("60") ||
  74. !consoleOutput().contains("240") ||
  75. !consoleOutput().contains("1200") ||
  76. !consoleOutput().contains("7200") ||
  77. !consoleOutput().contains("50400") ||
  78. !consoleOutput().contains("403200") ||
  79. !consoleOutput().contains("3628800") ||
  80. !consoleOutput().contains("36288000") ||
  81. !consoleOutput().contains("399168000"))
  82. {
  83. feedback("Your output doesn't seem to contain all the numbers we were expecting. Maybe your calculation isn't right.");
  84. }
  85.  
  86. if (consoleOutput().contains("20") &&
  87. consoleOutput().contains("60") &&
  88. consoleOutput().contains("240") &&
  89. consoleOutput().contains("1200") &&
  90. consoleOutput().contains("7200") &&
  91. consoleOutput().contains("50400") &&
  92. consoleOutput().contains("403200") &&
  93. consoleOutput().contains("3628800") &&
  94. consoleOutput().contains("36288000") &&
  95. consoleOutput().contains("399168000"))
  96. {
  97. feedback("Your numbers are right, but you don't seem to be displaying the correct textRemember that programmers need to be strict about their capitalisation, spacing and remaining authentic to the specification they've been given. If you're told to print 'The number is now' then do so! Don't print 'the number is now' or 'THE NUMBER IS NOW' or 'thE nuMBer Is Now' or 'Ramesis Niblick The Third Kerplunk Kerplunk Whoops Where's My Thribble'.'");
  98. }
  99.  
  100. </div>
  101. </div>
  102.  
  103. <h3>Silver medal</h3>
  104.  
  105. <p>Next, we have the pseudocode from the lecture slides that calculates how long it would take to pay off a loan of 500 pounds if there was 10% interest monthly, and 100 pounds was paid each month.</p>
  106.  
  107. <p>Something we haven't been taking into account in our pseudocode thus far is the variety of data types that a strongly typed language uses. When we have a variable in our pseudocode, and we're porting it to Java, we need to make a decision as to what data type it's going to be. So, for example, consider what data type the variable <code>months</code> will have to be. In contrast, what data type will <code>balance</code> have to be?</p>
  108.  
  109. <pre class="prettyprint nopaste">months = 1
  110. balance = 500
  111. totalpaid = 0
  112. while balance > 100:
  113. balance = balance - 100
  114. interest = balance * 0.1
  115. balance = balance + interest
  116. totalpaid = totalpaid + 100
  117. months = months + 1
  118. totalpaid = totalpaid+balance
  119. print "You paid",totalpaid
  120. print "It took you",months,"months"</pre>
  121.  
  122.  
  123. <div class="testCase">
  124. <div class="medalType">silver</div>
  125. <div class="medalDesc">The Road To Damascus</div>
  126. <div class="id">Conversion</div>
  127. <div class="test code runmain">
  128. if (consoleOutput().contains("637.0634") &&
  129. consoleOutput().contains("7 months")) return true;
  130. </div>
  131. </div>
  132.  
  133. <h3>Gold medal</h3>
  134.  
  135. <p>You can upgrade that silver to a gold by successfully implementing the more complex bit of code below, where we have two nested loops, one a <code>for</code>, one a <code>while</code>.</p>
  136.  
  137. <pre class="prettyprint nopaste">for x in range (0,10):
  138. stars = "*"
  139. count = 0
  140. while count < x:
  141. stars = stars + "*"
  142. count = count + 1
  143. print stars</pre>
  144.  
  145. <div class="testCase">
  146. <div class="medalType">gold</div>
  147. <div class="medalDesc">The Road To Damascus</div>
  148. <div class="id">Conversion</div>
  149. <div class="codeIncludes" data-feedback="For a authentic implementation, we'd expect you would use a 'for' loop.">for</div>
  150. <div class="codeIncludes" data-feedback="For a authentic implementation, we'd expect you would use a 'while' loop.">while</div>
  151. <div class="test code runmain">
  152.  
  153. String[] lines = consoleOutputAsLines();
  154. if (lines[0].equals("**"))
  155. {
  156. feedback("Double check your outer loop versus the pseudocode. Have you started your loop at the same number as the one in the pseudocode?");
  157. return false;
  158. }
  159.  
  160. if (lines.length != 10)
  161. {
  162. feedback("There doesn't seem to the right number of lines in your output. Perhaps your loops aren't quite right...");
  163. return false;
  164. }
  165.  
  166. String co = consoleOutput().trim().replace("\r\n", " ").replace("\n", " ");
  167. if (!co.equals("* ** *** **** ***** ****** ******* ******** ********* **********"))
  168. {
  169. feedback("I didn't get the output I expected.");
  170. return false;
  171. }
  172.  
  173. if (co.equals("* ** *** **** ***** ****** ******* ******** ********* **********"))
  174. {
  175. return true;
  176. }
  177. </div>
  178. </div>
  179. </div>
  180.  
  181. <div class="section" id="Equality and Strings">
  182. <h2 class="title">Equality and strings</h2>
  183. <p>The <code>if</code> statement is a little different in Java when dealing with whether two things are equal, when compared to numbers. Consider the code below, which involves a numerical comparison for equality:</p>
  184.  
  185. <pre class="prettyprint codepaste">Scanner myScan = new Scanner(System.in);
  186. System.out.println("Enter a number");
  187. int num = myScan.nextInt();
  188. if (num == 5)
  189. {
  190. System.out.println("You typed 5. That's my favourite number.");
  191. }</pre>
  192.  
  193. <p>Remember that the double equals conditional operator == looks for equality. Conditional operators are used to see if a particular condition is true or false. So this will get a number from the user and see if it is equal to 5.</p>
  194.  
  195. <p>Strings are a different case and cannot be compared with the == operator. Your program will run, and often, it will even seem to be acting correctly but you cannot depend on == working as you'd expect with strings. Instead, you should use the <code>equals</code> method:</p>
  196.  
  197. <pre class="prettyprint codepaste">Scanner myScan = new Scanner(System.in);
  198. System.out.println("Enter your name");
  199. String name = myScan.nextLine();
  200. if (name.equals("Paul"))
  201. {
  202. System.out.println("Paul is my favourite name");
  203. }
  204. else
  205. {
  206. System.out.println("Ugh. That's not a nice name");
  207. }</pre>
  208.  
  209. <p>There is also the <code>equalsIgnoreCase</code> method:</p>
  210.  
  211. <pre class="prettyprint codepaste">Scanner myScan = new Scanner(System.in);
  212. System.out.println("Enter your name");
  213. String name = myScan.nextLine();
  214. if (name.equalsIgnoreCase("Paul"))
  215. {
  216. System.out.println("Paul is my favourite name");
  217. }
  218. else
  219. {
  220. System.out.println("Ugh. That's not a nice name");
  221. }</pre>
  222.  
  223. <p>You can also chain <code>if</code> statements, by using <code>else if</code> as shown below:</p>
  224.  
  225. <pre class="prettyprint codepaste">Scanner myScan = new Scanner(System.in);
  226. System.out.println("Enter your name");
  227. String name = myScan.nextLine();
  228. if (name.equalsIgnoreCase("Paul"))
  229. {
  230. System.out.println("Paul is my favourite name");
  231. }
  232. else if (name.equalsIgnoreCase("Fred"))
  233. {
  234. System.out.println("Fred is my second favourite name");
  235. }
  236. else
  237. {
  238. System.out.println("Ugh. That's not a nice name");
  239. }</pre>
  240.  
  241. <p>Again, pay attention to the structure, "grammar" and "punctuation" - the <code>syntax</code>, to give its proper terminology.</p>
  242.  
  243. <h3>Bronze medal</h3>
  244.  
  245. <p>Your task now is to write the code - you could probably modify/extend the previous example, or if you prefer, start from scratch - to get an abbreviated day of the week from the user and display the name of the day in full. So, the user should type one of the abbreviations below and in return get the full name of the day of the week as shown below:</p>
  246.  
  247. <table style="width : 300px">
  248. <tr style="background: #AAAAAA; font-weight: bolder"><td>Input</td><td>Output</td></tr>
  249. <tr><td>M</td><td>Monday</td></tr>
  250. <tr><td>Tu</td><td>Tuesday</td></tr>
  251. <tr><td>W</td><td>Wednesday</td></tr>
  252. <tr><td>Th</td><td>Thursday</td></tr>
  253. <tr><td>F</td><td>Friday</td></tr>
  254. <tr><td>Sa</td><td>Saturday</td></tr>
  255. <tr><td>Su</td><td>Sunday</td></tr>
  256. </table>
  257.  
  258. <p><b>NB: Note the capitalisation of the inputs and the responses!</b></p>
  259.  
  260. <p>Use whichever approach you like to analyse the problem. You might consider scribbling down some pseudocode first if and then convert into Java. You should spend the requisite time to understanding and mapping out the problem before writing code - but the technique you use to do that is up to you. DON'T JUST START BANGING AWAY LIKE AN ARMED POLICEMAN AT THE KEYBOARD! Take the time to plan your solution out first.</p>
  261.  
  262. <div class="testCase" style="margin-top: 0.8em">
  263. <div class="id">daysOfWeek</div>
  264. <div class="medalType">bronze</div>
  265. <div class="medalDesc">Days of the Week</div>
  266. <div class="test">
  267. <!-- flat fee, no additional minutes/texts -->
  268. <div class="inputTest">Su</div>
  269. <div class="code">
  270. if (consoleOutput().toLowerCase().contains("sunday")) return true;
  271. feedback("Failed on Sunday");
  272. </div>
  273. </div>
  274. <div class="test">
  275. <!-- flat fee, no additional minutes/texts -->
  276. <div class="inputTest">M</div>
  277. <div class="code">
  278. if (consoleOutput().toLowerCase().contains("monday")) return true;
  279. feedback("Failed on Monday");
  280. </div>
  281. </div>
  282. <div class="test">
  283. <!-- flat fee, no additional minutes/texts -->
  284. <div class="inputTest">Tu</div>
  285. <div class="code">
  286. if (consoleOutput().toLowerCase().contains("tuesday")) return true;
  287. feedback("Failed on Tuesday");
  288. </div>
  289. </div>
  290. <div class="test">
  291. <!-- flat fee, no additional minutes/texts -->
  292. <div class="inputTest">W</div>
  293. <div class="code">
  294. if (consoleOutput().toLowerCase().contains("wednesday")) return true;
  295. feedback("Failed on Wednesday");
  296. </div>
  297. </div>
  298. <div class="test">
  299. <!-- flat fee, no additional minutes/texts -->
  300. <div class="inputTest">Th</div>
  301. <div class="code">
  302. if (consoleOutput().toLowerCase().contains("thursday")) return true;
  303. feedback("Failed on Thursday");
  304. </div>
  305. </div>
  306. <div class="test">
  307. <!-- flat fee, no additional minutes/texts -->
  308. <div class="inputTest">F</div>
  309. <div class="code">
  310. if (consoleOutput().toLowerCase().contains("friday")) return true;
  311. feedback("Failed on Friday");
  312. </div>
  313. </div>
  314. <div class="test">
  315. <!-- flat fee, no additional minutes/texts -->
  316. <div class="inputTest">Sa</div>
  317. <div class="code">
  318. if (consoleOutput().toLowerCase().contains("saturday")) return true;
  319. feedback("Failed on Saturday");
  320. </div>
  321. </div>
  322.  
  323. </div>
  324.  
  325. <h3>Silver medal</h3>
  326.  
  327. <p>To upgrade to a silver, you need to extend the program so that in addition to the above messages, if the user does not type a valid day of the week as listed above, the program should display a polite error message that includes the word <code>error</code>. (Make sure you DO include the word <code>error</code> in the error message, though, otherwise your code won't pass the test).</p>
  328.  
  329. <div class="testCase" style="margin-top: 0.8em">
  330. <div class="id">daysOfWeek</div>
  331. <div class="medalType">silver</div>
  332. <div class="medalDesc">Days of the Week</div>
  333. <div class="test">
  334. <!-- flat fee, no additional minutes/texts -->
  335. <div class="inputTest">Su</div>
  336. <div class="code">
  337. if (consoleOutput().toLowerCase().contains("sunday")) return true;
  338. </div>
  339. </div>
  340. <div class="test">
  341. <!-- flat fee, no additional minutes/texts -->
  342. <div class="inputTest">M</div>
  343. <div class="code">
  344. if (consoleOutput().toLowerCase().contains("monday")) return true;
  345. </div>
  346. </div>
  347. <div class="test">
  348. <!-- flat fee, no additional minutes/texts -->
  349. <div class="inputTest">Tu</div>
  350. <div class="code">
  351. if (consoleOutput().toLowerCase().contains("tuesday")) return true;
  352. </div>
  353. </div>
  354. <div class="test">
  355. <!-- flat fee, no additional minutes/texts -->
  356. <div class="inputTest">W</div>
  357. <div class="code">
  358. if (consoleOutput().toLowerCase().contains("wednesday")) return true;
  359. </div>
  360. </div>
  361. <div class="test">
  362. <!-- flat fee, no additional minutes/texts -->
  363. <div class="inputTest">Th</div>
  364. <div class="code">
  365. if (consoleOutput().toLowerCase().contains("thursday")) return true;
  366. </div>
  367. </div>
  368. <div class="test">
  369. <!-- flat fee, no additional minutes/texts -->
  370. <div class="inputTest">F</div>
  371. <div class="code">
  372. if (consoleOutput().toLowerCase().contains("friday")) return true;
  373. </div>
  374. </div>
  375. <div class="test">
  376. <!-- flat fee, no additional minutes/texts -->
  377. <div class="inputTest">Sa</div>
  378. <div class="code">
  379. if (consoleOutput().toLowerCase().contains("saturday")) return true;
  380. </div>
  381. </div>
  382. <div class="test">
  383. <!-- flat fee, no additional minutes/texts -->
  384. <div class="inputTest">aardvark</div>
  385. <div class="code">
  386. if (consoleOutput().toLowerCase().contains("error")) return true;
  387. </div>
  388. </div>
  389.  
  390. </div>
  391.  
  392. <h3>Gold medal</h3>
  393.  
  394. <p>Finally, a gold will be awarded for extending the error handling so that if the person types in a non-valid input, it keeps repeating until a valid input is entered. You will need to go beyond just an <code>if</code> statement and need some kind of loop construct.</p>
  395.  
  396. <p>Things you may find useful to remember in this exercise are:</p>
  397.  
  398. <ul>
  399. <li>Variable scope</li>
  400. <li>The NOT operator !</li>
  401. <li>Logical operators, e.g. || (or) && (and)</li>
  402. <li>The minimum number of times a <code>while</code> loop repeats, and the minimum number of times a <code>do/while</code> loop repeats...</li>
  403. </ul>
  404.  
  405. <div class="testCase" style="margin-top: 0.8em">
  406. <div class="id">daysOfWeek</div>
  407. <div class="medalType">gold</div>
  408. <div class="medalDesc">Days of the Week</div>
  409. <div class="test">
  410. <!-- flat fee, no additional minutes/texts -->
  411. <div class="inputTest">Su</div>
  412. <div class="code">
  413. if (consoleOutput().toLowerCase().contains("sunday")) return true;
  414. feedback("Failed Su");
  415. </div>
  416. </div>
  417. <div class="test">
  418. <!-- flat fee, no additional minutes/texts -->
  419. <div class="inputTest">M</div>
  420. <div class="code">
  421. if (consoleOutput().toLowerCase().contains("monday")) return true;
  422. feedback("Failed M");
  423. </div>
  424. </div>
  425. <div class="test">
  426. <!-- flat fee, no additional minutes/texts -->
  427. <div class="inputTest">Tu</div>
  428. <div class="code">
  429. if (consoleOutput().toLowerCase().contains("tuesday")) return true;
  430. feedback("Failed Tu");
  431. </div>
  432. </div>
  433. <div class="test">
  434. <!-- flat fee, no additional minutes/texts -->
  435. <div class="inputTest">W</div>
  436. <div class="code">
  437. if (consoleOutput().toLowerCase().contains("wednesday")) return true;
  438. feedback("Failed W");
  439. </div>
  440. </div>
  441. <div class="test">
  442. <!-- flat fee, no additional minutes/texts -->
  443. <div class="inputTest">Th</div>
  444. <div class="code">
  445. if (consoleOutput().toLowerCase().contains("thursday")) return true;
  446. feedback("Failed Th");
  447. </div>
  448. </div>
  449. <div class="test">
  450. <!-- flat fee, no additional minutes/texts -->
  451. <div class="inputTest">F</div>
  452. <div class="code">
  453. if (consoleOutput().toLowerCase().contains("friday")) return true;
  454. feedback("Failed F");
  455. </div>
  456. </div>
  457. <div class="test">
  458. <!-- flat fee, no additional minutes/texts -->
  459. <div class="inputTest">Sa</div>
  460. <div class="code">
  461. if (consoleOutput().toLowerCase().contains("saturday")) return true;
  462. feedback("Failed Sa");
  463. </div>
  464. </div>
  465. <div class="test">
  466. <!-- flat fee, no additional minutes/texts -->
  467. <div class="inputTest">aardvark</div>
  468. <div class="inputTest">aardvark</div>
  469. <div class="inputTest">aardvark</div>
  470. <div class="inputTest">aardvark</div>
  471. <div class="inputTest">aardvark</div>
  472. <div class="inputTest">Sa</div>
  473. <div class="code">
  474. int countSat = consoleOutput().toLowerCase().split("saturday", -1).length-1;
  475. int countError = consoleOutput().toLowerCase().split("error", -1).length-1;
  476. if (countError == 5 && (countSat == 2||countSat==10)) return true;
  477. if (countError != 5)
  478. {
  479. feedback("Your solution doesn't seem to repeat on incorrect input like it should.");
  480. return false;
  481. }
  482. if (countSat != 2&&countSat != 10)
  483. {
  484. feedback("Your solution repeats until there's a correct input, but doesn't seem to display the correct day after the error prompt. countError=" + countError +", countSat="+ countSat+"=\n"+consoleOutput().toString());
  485. }
  486. </div>
  487. </div>
  488.  
  489. </div>
  490.  
  491. </div>
  492.  
  493.  
  494. <div class="section" id="LowPricePlan">
  495. <h2 class="title">Low Price Plan</h2>
  496. <p>Last week you did an exercise where you wrote a program to calculate the cost of a mobile phone bill. This week you'll extend that. You can attempt to do this week's exercise without completing the gold from last week, but if you did get the gold it might be useful to use that as your starting point.</p>
  497.  
  498. <p><b>IT WILL ALMOST CERTAINLY BE HELPFUL TO TAKE A PEN AND PAPER AND WORK OUT HOW THESE EXERCISES NEED TO BE CALCULATED BY HAND, before you write a single line of code.</b> Perhaps you might consider using pseudocode to map out what you need to do.</p>
  499.  
  500. <p><b>If we get anyone asking for help who has NOT done the pen and paper thing first, we will walk away until you have! You have been warned! :-)</b></p>
  501.  
  502. <h3>Bronze medal</h3>
  503.  
  504. <p>Write a program that will ask the user to type in how many minutes they've used on their mobile phone, and then how many texts they have used. It should then display their total bill, according to the following rules:</p>
  505.  
  506. <ul>
  507. <li>The user is charged a flat fee of 1000p per month as their line rental.</li>
  508. <li>For this flat fee, they get 75 free minutes, and 250 free texts.</li>
  509. <li>If they go over 75 minutes, they should be charged 12p for each additional minute they use.</li>
  510. <li>If they go over 250 free texts, they should be charged 7p for each additional text they send.</li>
  511. </ul>
  512.  
  513. <div class="testCase" style="margin-top: 0.8em">
  514. <div class="id">phonePricePlan</div>
  515. <div class="medalType">bronze</div>
  516. <div class="medalDesc">Phone Price Plan Challenge</div>
  517. <div class="test">
  518. <div class="inputTest">74</div>
  519. <div class="inputTest">249</div>
  520. <div class="code">
  521. if (consoleOutput().contains("1000")) return true;
  522. </div>
  523. </div>
  524. <div class="test">
  525. <div class="inputTest">123</div>
  526. <div class="inputTest">345</div>
  527. <div class="code">
  528. if (consoleOutput().contains("2241")) return true;
  529. </div>
  530. </div>
  531. <div class="test">
  532. <div class="inputTest">74</div>
  533. <div class="inputTest">345</div>
  534. <div class="code">
  535. if (consoleOutput().contains("1665")) return true;
  536. </div>
  537. </div>
  538. <div class="test">
  539. <div class="inputTest">123</div>
  540. <div class="inputTest">249</div>
  541. <div class="code">
  542. if (consoleOutput().contains("1665")) return true;
  543. </div>
  544. </div>
  545. </div>
  546.  
  547. <h3>Silver medal</h3>
  548.  
  549. <p>To get a silver medal, complete the bronze exercise. Then extend it so that your program also tells the user how many minutes and texts they were billed for (i.e. that weren't part of their free allowance), and how much these billable minutes and texts cost them, as well as displaying the overall total bill.</p>
  550.  
  551. <p>A silver-standard effort might look something like that below:</p>
  552.  
  553. <div style="text-align: center"><img src="lowprice.gif" style="border: 1px solid black; border-right: 0px"/></div>
  554.  
  555. <div class="testCase" style="margin-top: 0.8em">
  556. <div class="id">phonePricePlan</div>
  557. <div class="medalType">silver</div>
  558. <div class="medalDesc">Phone Price Plan Challenge</div>
  559. <div class="test">
  560. <div class="inputTest">74</div>
  561. <div class="inputTest">249</div>
  562. <div class="code">
  563. if (consoleOutput().contains("1000")) return true;
  564. </div>
  565. </div>
  566. <div class="test">
  567. <div class="inputTest">123</div>
  568. <div class="inputTest">345</div>
  569. <div class="code">
  570. if (consoleOutput().contains("2241")
  571. && consoleOutput().contains("48")
  572. && consoleOutput().contains("95")
  573. && consoleOutput().contains("576")
  574. && consoleOutput().contains("665")
  575. ) return true;
  576. </div>
  577. </div>
  578. <div class="test">
  579. <div class="inputTest">74</div>
  580. <div class="inputTest">345</div>
  581. <div class="code">
  582. if (consoleOutput().contains("1665")) return true;
  583. </div>
  584. </div>
  585. <div class="test">
  586. <div class="inputTest">123</div>
  587. <div class="inputTest">249</div>
  588. <div class="code">
  589. if (consoleOutput().contains("1665")) return true;
  590. </div>
  591. </div>
  592. </div>
  593.  
  594. <h3>Gold Medal</h3>
  595.  
  596. <p>The phone company has now extended their service so that they offer a variety of price plans. You can choose one of four tariffs. Each tariff has a different monthly fee, and for this monthly fee you will get a certain amount of inclusive minutes and texts. The tariffs are as follows:</p>
  597.  
  598. <table>
  599. <tr style="font-weight: bolder; background: #AAAAAA"><td>Plan No</td><td>Monthly Cost</td><td>Free minutes</td><td>Free texts</td></tr>
  600. <tr><td>1</td><td>£10.00</td><td>75</td><td>250</td></tr>
  601. <tr><td>2</td><td>£15.00</td><td>150</td><td>250</td></tr>
  602. <tr><td>3</td><td>£20.00</td><td>300</td><td>300</td></tr>
  603. <tr><td>4</td><td>£25.00</td><td>300</td><td>Unlimited</td></tr>
  604. </table>
  605.  
  606. <p>Your program should ask the user to enter which tariff number they subscribe to, then ask how many minutes and how many texts they've used before calculating their total bill for the month (you don't have to display the number of billable texts and minutes if you don't want to, but you can if you like).</p>
  607.  
  608. <p>Because you now know all about the different numeric data types, we now want the code to calculate the value in pounds. So the bill should be displayed as pounds, with pence shown after a decimal point. Some of your variables, therefore, will need to be changed into doubles.</p>
  609.  
  610. <div class="testCase" style="margin-top: 0.8em">
  611. <div class="id">phonePricePlan</div>
  612. <div class="medalType">gold</div>
  613. <div class="medalDesc">Phone Price Plan Challenge</div>
  614. <div class="test">
  615. <div class="inputTest">1</div>
  616. <div class="inputTest">74</div>
  617. <div class="inputTest">249</div>
  618. <div class="code">
  619. if (consoleOutput().contains("10.0")) return true;
  620. </div>
  621. </div>
  622. <div class="test">
  623. <div class="inputTest">2</div>
  624. <div class="inputTest">74</div>
  625. <div class="inputTest">249</div>
  626. <div class="code">
  627. if (consoleOutput().contains("15.0")) return true;
  628. </div>
  629. </div>
  630. <div class="test">
  631. <div class="inputTest">3</div>
  632. <div class="inputTest">74</div>
  633. <div class="inputTest">249</div>
  634. <div class="code">
  635. if (consoleOutput().contains("20.0")) return true;
  636. </div>
  637. </div>
  638. <div class="test">
  639. <div class="inputTest">4</div>
  640. <div class="inputTest">74</div>
  641. <div class="inputTest">249</div>
  642. <div class="code">
  643. if (consoleOutput().contains("25.0")) return true;
  644. </div>
  645. </div>
  646. <div class="test">
  647. <div class="inputTest">1</div>
  648. <div class="inputTest">76</div>
  649. <div class="inputTest">251</div>
  650. <div class="code">
  651. if (consoleOutput().contains("10.19")) return true;
  652. </div>
  653. </div>
  654. <div class="test">
  655. <div class="inputTest">2</div>
  656. <div class="inputTest">151</div>
  657. <div class="inputTest">251</div>
  658. <div class="code">
  659. if (consoleOutput().contains("15.19")) return true;
  660. </div>
  661. </div>
  662. <div class="test">
  663. <div class="inputTest">3</div>
  664. <div class="inputTest">301</div>
  665. <div class="inputTest">301</div>
  666. <div class="code">
  667. if (consoleOutput().contains("20.19")) return true;
  668. </div>
  669. </div>
  670. <div class="test">
  671. <div class="inputTest">4</div>
  672. <div class="inputTest">301</div>
  673. <div class="inputTest">9000</div>
  674. <div class="code">
  675. if (consoleOutput().contains("25.12")) return true;
  676. </div>
  677. </div>
  678.  
  679. </div>
  680.  
  681.  
  682. <h3>Tips</h3>
  683.  
  684. <ul>
  685. <li>The ORDER is important otherwise you won't get the medals. You must ask for the minutes first, then the texts.</li>
  686. <li>Don't forget to add the line rental onto the bill!</li>
  687. <li>There are a number of ways you might consider approaching this problem, for example:
  688. <ul>
  689. <li>Deduct the free minutes and texts from the actual amount used, use <code>if</code> statements to ensure that they don't end up with a negative amount, then calculate the totals based on the adjusted figures</li>
  690. <li>Use <code>if</code> statements to determine whether the person has gone over the free minutes and texts, calculate how many minutes/texts they've gone over, and work out the total based on these values.</li>
  691. </ul></li>
  692. <li>There is no correct approach, although some are more efficient than others! For example, it is possible to do the silver version of the exercise with just two <code>if</code> statements (but use as many as you need)</li>
  693. <li>Finally, seriously, DO THE PEN AND PAPER THING! You will thank me for it - honestly.</li>
  694. </ul>
  695.  
  696. </div>
  697.  
  698. <div class="section" id="Repetition">
  699. <h2 class="title">Loops</h2>
  700. <p>In this exercise you will need to write programs that repeat. You will need to know whether the best choice is a <code>for</code>, <code>while</code> or <code>do/while</code>.</p>
  701.  
  702. <h3>Bronze medal</h3>
  703.  
  704. <p>Write a program that gets two numbers from the keyboard. It should then count from the first number to the second number.</p>
  705.  
  706. <div class="testCase">
  707. <div class="id">loopsAndReps</div>
  708. <div class="medalType">bronze</div>
  709. <div class="medalDesc">Loops and repetition</div>
  710. <div class="test">
  711. <div class="inputTest">3</div>
  712. <div class="inputTest">7</div>
  713. <div class="code">
  714. String result = consoleOutput();
  715. result = result.replaceAll("\\n","");
  716. if (result.endsWith("34567")) return true;
  717. if (result.endsWith("3456"))
  718. {
  719. feedback("You're missing off the final number!");
  720. return false;
  721. }
  722. if (result.endsWith("456"))
  723. {
  724. feedback("You're missing off the first AND final numbers!");
  725. return false;
  726. }
  727. if (result.endsWith("4567"))
  728. {
  729. feedback("You're missing off the final number!");
  730. return false;
  731. }
  732. </div>
  733. </div>
  734. </div>
  735.  
  736. <h3>Silver medal</h3>
  737.  
  738. <p>Write a program that asks the user for a number. If the number is more than 10, it should display the text "more". It should keep doing this, repeatedly asking for a number, until they type a number less than 10. <b>You should not display "more" if they type a number less than 10!</b></p>
  739.  
  740. <div class="testCase">
  741. <div class="id">loopsAndReps</div>
  742. <div class="medalType">silver</div>
  743. <div class="medalDesc">Loops and repetition</div>
  744. <div class="test">
  745. <div class="inputTest">12</div>
  746. <div class="inputTest">17</div>
  747. <div class="inputTest">24</div>
  748. <div class="inputTest">2</div>
  749. <div class="code">
  750. String result = consoleOutput();
  751. feedback(result.split("more").length+"");
  752. if (result.split("more").length == 4) return true;
  753. if (result.split("more").length == 5)
  754. {
  755. feedback("You seem to be displaying the 'more' text on the final less than 10 number.");
  756. return false;
  757. }
  758. </div>
  759. </div>
  760. </div>
  761.  
  762. <p><b>Tips:</b></p>
  763.  
  764. <ul>
  765. <li>What is the minimum number of times this loop will repeat? What is the best loop construct to use, then?</li>
  766. <li>Remember variable scope! If you expect to be able to use a variable in a <code>do/while</code> condition, it has to be declared outside of the loop.</li>
  767. </ul>
  768.  
  769. <h3>Gold medal</h3>
  770.  
  771. <p>Write a program that asks the user for a number. If the number is less than 10, it should count from 1 to the number supplied. It should keep doing this, repeatedly asking for a number, until they type a number more than 10. <b>You should not do any counting if they type a number more than 10!</b></p>
  772.  
  773. <div class="testCase">
  774. <div class="id">loopsAndReps</div>
  775. <div class="medalType">gold</div>
  776. <div class="medalDesc">Loops and repetition</div>
  777. <div class="test">
  778. <div class="inputTest">4</div>
  779. <div class="inputTest">9</div>
  780. <div class="inputTest">3</div>
  781. <div class="inputTest">2</div>
  782. <div class="inputTest">14</div>
  783. <div class="code">
  784. String result = consoleOutput();
  785. result = result.replaceAll("\\n","").replaceAll("[^0-9]","");
  786. feedback (result);
  787.  
  788. if (result.contains("13"))
  789. {
  790. feedback("You seem to be displaying the count for the final run that is greater than ten!");
  791. return false;
  792. }
  793.  
  794. if (result.contains("0"))
  795. {
  796. feedback("Your counter starts at 0!");
  797. return false;
  798. }
  799.  
  800. if (result.contains("123456781"))
  801. {
  802. feedback("Your counter is not counting up to the number supplied - it's only going to one less than the number.");
  803. return false;
  804. }
  805.  
  806.  
  807. if (result.contains("12341") && result.contains("1234567891") && result.contains("1231")) return true;
  808.  
  809.  
  810. </div>
  811. </div>
  812. </div>
  813.  
  814.  
  815. </div>
  816.  
  817. </body>
  818. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement