{"id":707,"date":"2014-08-10T16:34:32","date_gmt":"2014-08-10T06:34:32","guid":{"rendered":"http:\/\/blogs.unsw.edu.au\/comp1400\/?p=707"},"modified":"2014-08-10T16:36:44","modified_gmt":"2014-08-10T06:36:44","slug":"lab-week-3-data-types-2","status":"publish","type":"post","link":"https:\/\/blogs.unsw.edu.au\/comp1400\/blog\/2014\/08\/lab-week-3-data-types-2\/","title":{"rendered":"Lab Week 3 &#8211; Data Types"},"content":{"rendered":"<h2><span style=\"font-size: 1.17em\">Feedback on your progress<\/span><\/h2>\n<p>Each week, we will post several 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 should attempt the quizzes in your own time before the lab and bring your answers to your tutor, who will give you feedback. For now, quizzes are voluntary and we hope you&#8217;ll try them.<\/p>\n<p>This week&#8217;s quiz is <a title=\"Week 3 Quiz\" href=\"http:\/\/www.cse.unsw.edu.au\/~cs1400\/14s2\/quizzes\/Week%203%20Quiz.pdf\">here<\/a>.<\/p>\n<h2>Lab Exercises<\/h2>\n<p>This week&#8217;s lab exercises are concerned with data types, which were introduced in last week&#8217;s lectures. 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>Feedback on your progress Each week, we will post several 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 [&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-707","post","type-post","status-publish","format-standard","hentry","category-lab"],"_links":{"self":[{"href":"https:\/\/blogs.unsw.edu.au\/comp1400\/wp-json\/wp\/v2\/posts\/707","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=707"}],"version-history":[{"count":2,"href":"https:\/\/blogs.unsw.edu.au\/comp1400\/wp-json\/wp\/v2\/posts\/707\/revisions"}],"predecessor-version":[{"id":711,"href":"https:\/\/blogs.unsw.edu.au\/comp1400\/wp-json\/wp\/v2\/posts\/707\/revisions\/711"}],"wp:attachment":[{"href":"https:\/\/blogs.unsw.edu.au\/comp1400\/wp-json\/wp\/v2\/media?parent=707"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.unsw.edu.au\/comp1400\/wp-json\/wp\/v2\/categories?post=707"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.unsw.edu.au\/comp1400\/wp-json\/wp\/v2\/tags?post=707"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}