{"id":424,"date":"2012-09-09T21:47:47","date_gmt":"2012-09-10T04:47:47","guid":{"rendered":"http:\/\/blogs.unsw.edu.au\/comp1400\/?p=424"},"modified":"2012-09-09T21:47:47","modified_gmt":"2012-09-10T04:47:47","slug":"week-8-tut-objects","status":"publish","type":"post","link":"https:\/\/blogs.unsw.edu.au\/comp1400\/blog\/2012\/09\/week-8-tut-objects\/","title":{"rendered":"Week 8 Tut &#8211; Objects"},"content":{"rendered":"<ol>\n<li>Create an instance of the <code>java.util.Random<\/code> class in BlueJ using the <strong>Tools &gt; Use Library Class&#8230;<\/strong> option.<\/li>\n<li>Right click on the object created to see its methods. Try running some of them. What do they do?<\/li>\n<li>Read the <a href=\"http:\/\/docs.oracle.com\/javase\/6\/docs\/api\/java\/util\/Random.html\">Java docs<\/a> for the Random class.<\/li>\n<li>Which method would you use to:\n<ul>\n<li>Simulate a fair coin toss?<\/li>\n<li>Simulate a (six-sided) dice roll? What about ten-sided? Twenty-sided? <\/li>\n<li>Simulate a biased coin that shows heads 67% of the time?<\/li>\n<\/ul>\n<li>Open the Code Pad and do the following\n<ul>\n<li>Write a command to construct a Random object with the seed 200 and assign it to a variable.<\/li>\n<li>Call nextInt() several times on the object you created.<\/li>\n<li>Create a second object with the same seed and call nextInt() on it too. Explain the result.<\/li>\n<\/ul>\n<\/li>\n<li>Write a method <code>rollDice(int number, int nSides)<\/code> which returns the total result of rolling the <code>number<\/code> dice with <code>nSides<\/code> sides.\n<p>So for example <code>rollDice(3, 6)<\/code> should return the result of rolling<br \/>\n3 six-sided dice (adding to a number between 3 and 18 inclusive).<\/li>\n<li>Write a method <code>selectRandom(String[] names)<\/code> which returns a randomly selected name from the given array. Each name should be selected with equal probability.<\/li>\n<li><strong>Challenge:<\/strong> Write a method <code>shuffle(String[] names)<\/code> which takes an array of names are returns a copy of the list with the names randomly rearranged. Each possible should be returned with equal probability.\n<p>So for instance, given the code:<br \/>\n<code><br \/>\nString[] names = {\"Malcolm\", \"Troy\", \"Sim\"};<br \/>\nString[] shuffled = shuffle(names);<br \/>\n<\/code><br \/>\nThe following return values should be equally likely:<\/p>\n<ol>\n<li>{&#8220;Malcolm&#8221;, &#8220;Troy&#8221;, &#8220;Sim&#8221;}<\/li>\n<li>{&#8220;Malcolm&#8221;, &#8220;Sim&#8221;, &#8220;Troy&#8221;}<\/li>\n<li>{&#8220;Troy&#8221;, &#8220;Malcolm&#8221;, &#8220;Sim&#8221;}<\/li>\n<li>{&#8220;Troy&#8221;, &#8220;Sim&#8221;, &#8220;Malcolm&#8221;}<\/li>\n<li>{&#8220;Sim&#8221;, &#8220;Troy&#8221;, &#8220;Malcolm&#8221;}<\/li>\n<li>{&#8220;Sim&#8221;, &#8220;Malcolm&#8221;, &#8220;Troy&#8221;}<\/li>\n<\/ol>\n<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>Create an instance of the java.util.Random class in BlueJ using the Tools &gt; Use Library Class&#8230; option. Right click on the object created to see its methods. Try running some of them. What do they do? Read the Java docs for the Random class. Which method would you use to: Simulate a fair coin toss? [&hellip;]<\/p>\n","protected":false},"author":80,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[630],"tags":[],"class_list":["post-424","post","type-post","status-publish","format-standard","hentry","category-lab"],"_links":{"self":[{"href":"https:\/\/blogs.unsw.edu.au\/comp1400\/wp-json\/wp\/v2\/posts\/424","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.unsw.edu.au\/comp1400\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.unsw.edu.au\/comp1400\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.unsw.edu.au\/comp1400\/wp-json\/wp\/v2\/users\/80"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.unsw.edu.au\/comp1400\/wp-json\/wp\/v2\/comments?post=424"}],"version-history":[{"count":6,"href":"https:\/\/blogs.unsw.edu.au\/comp1400\/wp-json\/wp\/v2\/posts\/424\/revisions"}],"predecessor-version":[{"id":430,"href":"https:\/\/blogs.unsw.edu.au\/comp1400\/wp-json\/wp\/v2\/posts\/424\/revisions\/430"}],"wp:attachment":[{"href":"https:\/\/blogs.unsw.edu.au\/comp1400\/wp-json\/wp\/v2\/media?parent=424"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.unsw.edu.au\/comp1400\/wp-json\/wp\/v2\/categories?post=424"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.unsw.edu.au\/comp1400\/wp-json\/wp\/v2\/tags?post=424"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}