{"id":507,"date":"2013-08-08T21:54:00","date_gmt":"2013-08-09T04:54:00","guid":{"rendered":"http:\/\/blogs.unsw.edu.au\/comp1400\/?p=507"},"modified":"2013-08-13T23:47:18","modified_gmt":"2013-08-14T06:47:18","slug":"lab-week-3-data-types","status":"publish","type":"post","link":"https:\/\/blogs.unsw.edu.au\/comp1400\/blog\/2013\/08\/lab-week-3-data-types\/","title":{"rendered":"Lab Week 3 &#8211; Data Types"},"content":{"rendered":"<p>This week&#8217;s lab exercises are concerned with data types, which were introduced in last week&#8217;s lectures.<\/p>\n<h3>Feedback on your progress<\/h3>\n<p>At the start of the lab, you&#8217;ll be given a short quiz on writing simple methods. This shouldn&#8217;t take more than 15 minutes.<\/p>\n<p>Each week, we will ask you short quiz questions to encourage you to do some preparation before the lab. The intention of the quizzes is to give you quick feedback so that you know if you understand the material in the course so far and to encourage you to prepare for the labs by reading over the lecture material from the previous weeks.<\/p>\n<p>You&#8217;ll be given a mark for each quiz just to make sure that you take them seriously, but\u00a0note that they contribute only a small part to your final assessment (about half of your lab mark, the other half from your attempts at the lab exercises).\u00a0Your answers will be marked as either satisfactory or unsatisfactory by your tutor during the lab. If you have a good reason for missing a quiz, talk to your tutor about making it up.<\/p>\n<p>After completing the quiz, you should attempt the exercises below using the\u00a0<a href=\"http:\/\/www.cse.unsw.edu.au\/~cs1400\/labs\/Tut3.zip\">Tut3.zip<\/a> project for BlueJ.<\/p>\n<h3>Data types<\/h3>\n<p>What\u00a0<strong>data type<\/strong> would you use to represent the following values? Why?<\/p>\n<ol>\n<li>The length of a piece of string.<\/li>\n<li>The number of students at UNSW.<\/li>\n<li>A person&#8217;s age.<\/li>\n<li>The mass of the sun.<\/li>\n<li>The exact number of stars in the Milky Way.<\/li>\n<li>The mathematical constant \u03c0.<\/li>\n<\/ol>\n<h3>Expressions<\/h3>\n<p>What are the\u00a0<strong>value<\/strong> and\u00a0<strong>type<\/strong> of the following expressions? Work these out on paper and then test your answers by typing them into the Code Pad in BlueJ. Can you explain the result?<\/p>\n<ol>\n<li>5 \/ 2<\/li>\n<li>5 % 2<\/li>\n<li>5.0 \/ 2<\/li>\n<li>5 \/ 2.0<\/li>\n<li>5.0 \/ 2.5<\/li>\n<li>5.0f \/ 2<\/li>\n<li>1.5 % 1<\/li>\n<li>1 \/ 0<\/li>\n<li>1.0 \/ 0<\/li>\n<li>10000000000 + 1<\/li>\n<li>10000000000L + 1<\/li>\n<li>10000000000.0 + 1<\/li>\n<li>10000000000.0f + 1<\/li>\n<li>int x = 10;<\/li>\n<li>x = x + 2;<\/li>\n<li>int y = 3;<\/li>\n<li>int z = x + y + 1;<\/li>\n<li>x = x+ 2;<\/li>\n<\/ol>\n<h3>Circle area<\/h3>\n<p>The\u00a0<strong>circleArea<\/strong> method in the Tut3 class calculates the area of a circle with a given radius.<\/p>\n<ul>\n<li>Create a new Tut3 object and try calling this method with different input values.<\/li>\n<li>Add breakpoint on the first line and step through the code. Watch the &#8216;local variable&#8217; panel to see variables being created and change value.<\/li>\n<li>Change the method to calculate the area of an ellipse of given width and height. The area of an ellipse is \u03c0\u00a0\u00d7 height \u00d7 width \/ 2. You can use the value 3.141592 for \u03c0 or use Java&#8217;s math library definition, Math.PI.<\/li>\n<\/ul>\n<h3>Hand shaking<\/h3>\n<p>If there are five people Alan, Bel, Cameron, Dan, and Emily in the room and they each have to shake hands with everyone else, how many handshakes occur? What if there were 10 people? What if there were only two? Work out a general formula.<\/p>\n<p>Complete the &#8216;handshakes&#8217; method in the Tut3 project to use your formula to compute the number of handshakes for a given size group.<\/p>\n<h3>The Math Class<\/h3>\n<p>For more complicated mathematical expressions, Java has a lot of built in methods in the Math class. Take a look at the\u00a0<a href=\"http:\/\/docs.oracle.com\/javase\/6\/docs\/api\/java\/lang\/Math.html\">documentation<\/a> to see what is available. Try typing the following<br \/>\nin CodePad. Explain the results.<\/p>\n<ul>\n<li>Math.sqrt(2.0)<\/li>\n<li>Math.round(3.1)<\/li>\n<li>Math.round(3.7)<\/li>\n<li>Math.min(1, 2)<\/li>\n<li>Math.max(1, 2)<\/li>\n<\/ul>\n<h3>If-statements<\/h3>\n<p>Assume we have declared an integer variable <strong>x<\/strong>. Write an if-statement that will print &#8220;one&#8221; if the value of <strong>x<\/strong> is 1, &#8220;two&#8221; if the value of <strong>x<\/strong> is 2 and &#8220;something else&#8221; if the value <strong>x<\/strong> is some other value.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This week&#8217;s lab exercises are concerned with data types, which were introduced in last week&#8217;s lectures. Feedback on your progress At the start of the lab, you&#8217;ll be given a short quiz on writing simple methods. This shouldn&#8217;t take more than 15 minutes. Each week, we will ask you short quiz questions to encourage you [&hellip;]<\/p>\n","protected":false},"author":328,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[630],"tags":[],"class_list":["post-507","post","type-post","status-publish","format-standard","hentry","category-lab"],"_links":{"self":[{"href":"https:\/\/blogs.unsw.edu.au\/comp1400\/wp-json\/wp\/v2\/posts\/507","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\/328"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.unsw.edu.au\/comp1400\/wp-json\/wp\/v2\/comments?post=507"}],"version-history":[{"count":13,"href":"https:\/\/blogs.unsw.edu.au\/comp1400\/wp-json\/wp\/v2\/posts\/507\/revisions"}],"predecessor-version":[{"id":514,"href":"https:\/\/blogs.unsw.edu.au\/comp1400\/wp-json\/wp\/v2\/posts\/507\/revisions\/514"}],"wp:attachment":[{"href":"https:\/\/blogs.unsw.edu.au\/comp1400\/wp-json\/wp\/v2\/media?parent=507"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.unsw.edu.au\/comp1400\/wp-json\/wp\/v2\/categories?post=507"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.unsw.edu.au\/comp1400\/wp-json\/wp\/v2\/tags?post=507"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}