{"id":302,"date":"2012-08-02T07:38:17","date_gmt":"2012-08-02T14:38:17","guid":{"rendered":"http:\/\/blogs.unsw.edu.au\/infs1609\/?p=302"},"modified":"2012-08-02T08:02:27","modified_gmt":"2012-08-02T15:02:27","slug":"lab-week-4-loops-and-arrays","status":"publish","type":"post","link":"https:\/\/blogs.unsw.edu.au\/infs1609\/blog\/2012\/08\/lab-week-4-loops-and-arrays\/","title":{"rendered":"Lab Week 4 &#8211; Loops and Arrays"},"content":{"rendered":"<p>This week, you will download the BlueJ project <a href=\"http:\/\/www.cse.unsw.edu.au\/~is1609\/12s2\/examples\/Week4.zip\">Week4.zip<\/a> and write methods to manipulate arrays. Two methods are given to you complete. You don&#8217;t have to do anything to them.<\/p>\n<p>The method <strong>readArray<\/strong><\/p>\n<p><code> public int[] readArray()<\/code><\/p>\n<p>reads a list of numbers, one per line, ending with a blank line, and returns an array containing the numbers. <em>Don&#8217;t try to understand the details of this method. It uses things that you&#8217;ll learn about later.<\/em><\/p>\n<p>The method <strong>printArray<\/strong> takes an integer array parameter and prints it to the terminal<\/p>\n<p><code> public void printArray(int[] R)<\/code><\/p>\n<p>The project provides templates for other methods that you have to write. Your job is to work out the code that goes inside them.<\/p>\n<p>The first method, <strong>arrayAdd<\/strong>, takes two arrays as parameters and returns a new array that is the sum of the first two.<\/p>\n<p><code> public int[] arrayAdd(int[] R1, int[] R2)<\/code><\/p>\n<p>The addition of two arrays yields an array whose elements are the sum of the corresponding elements in the parameter arrays, i.e.<\/p>\n<p><code> R1[0] + R2[0],\u00a0R1[1] + R2[1],\u00a0R1[2] + R2[2], ....<\/code><\/p>\n<p>The <strong>dotProduct<\/strong> of two arrays returns a single integer.<\/p>\n<p><code> public int dotProduct(int[] R1, int[] R2)<\/code><\/p>\n<p>It is found by multiplying corresponding elements and adding them all up, i.e.<\/p>\n<p><code> R1[0] * R2[0] +\u00a0R1[1] * R2[1] + R1[2] * R2[2], ....<\/code><\/p>\n<p>Finally, you should write a method, <strong>main<\/strong>,<\/p>\n<p><code> public void main()<\/code><\/p>\n<p>that reads two arrays, then:<\/p>\n<ul>\n<li>calls <strong>arrayAdd<\/strong> and calls <strong>printArray<\/strong> to print the resulting array<\/li>\n<li>calls\u00a0<strong>dotProduct<\/strong> and prints the result<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>This week, you will download the BlueJ project Week4.zip and write methods to manipulate arrays. Two methods are given to you complete. You don&#8217;t have to do anything to them. The method readArray public int[] readArray() reads a list of numbers, one per line, ending with a blank line, and returns an array containing the [&hellip;]<\/p>\n","protected":false},"author":328,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[236],"tags":[],"class_list":["post-302","post","type-post","status-publish","format-standard","hentry","category-tutorials"],"_links":{"self":[{"href":"https:\/\/blogs.unsw.edu.au\/infs1609\/wp-json\/wp\/v2\/posts\/302","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.unsw.edu.au\/infs1609\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.unsw.edu.au\/infs1609\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.unsw.edu.au\/infs1609\/wp-json\/wp\/v2\/users\/328"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.unsw.edu.au\/infs1609\/wp-json\/wp\/v2\/comments?post=302"}],"version-history":[{"count":3,"href":"https:\/\/blogs.unsw.edu.au\/infs1609\/wp-json\/wp\/v2\/posts\/302\/revisions"}],"predecessor-version":[{"id":309,"href":"https:\/\/blogs.unsw.edu.au\/infs1609\/wp-json\/wp\/v2\/posts\/302\/revisions\/309"}],"wp:attachment":[{"href":"https:\/\/blogs.unsw.edu.au\/infs1609\/wp-json\/wp\/v2\/media?parent=302"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.unsw.edu.au\/infs1609\/wp-json\/wp\/v2\/categories?post=302"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.unsw.edu.au\/infs1609\/wp-json\/wp\/v2\/tags?post=302"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}