{"id":525,"date":"2013-08-15T19:55:50","date_gmt":"2013-08-16T02:55:50","guid":{"rendered":"http:\/\/blogs.unsw.edu.au\/comp1400\/?p=525"},"modified":"2013-08-15T20:13:45","modified_gmt":"2013-08-16T03:13:45","slug":"lab-week-4-loops-and-arrays","status":"publish","type":"post","link":"https:\/\/blogs.unsw.edu.au\/comp1400\/blog\/2013\/08\/lab-week-4-loops-and-arrays\/","title":{"rendered":"Lab Week 4 &#8211; Loops and Arrays"},"content":{"rendered":"<p>In this lab, we&#8217;ll practice creating a new BlueJ project from scratch. Use the &#8220;New&#8221; menu item to make a new class. Call it Week4. All the exercises below can be done in the same class file. You may not be able to finish all the questions during the lab time. Make sure that you do at least one question from each section in the lab and finish the rest as homework.<\/p>\n<h2>Basic Output<\/h2>\n<p>1. Write a method that prints the string \u201cHello World\u201d 8 times. The output should be:<\/p>\n<pre>Hello World\r\nHello World\r\nHello World\r\nHello World\r\nHello World\r\nHello World\r\nHello World\r\nHello World<\/pre>\n<p>2. Write a method that prints your name, age and favourite number, using integers for the numbers. For example:<\/p>\n<pre>My name is Fred, I am 18 years old, my favourite number is 2.<\/pre>\n<h2>Simple Calculations<\/h2>\n<p>1. Write a method, which takes one parameter\u00a0<em>n<\/em> and returns the number squared.<\/p>\n<p>2. Write a method, which takes three parameters\u00a0<em>a, b<\/em> and\u00a0<em>c<\/em> to calculate the area of a triangle using Heron\u2019s formula:\u00a0<em>area = sqrt(s(s-a)(s-b)(s-c))<\/em>. Where\u00a0<em>s<\/em> is the \u201csemi-perimeter\u201d given by\u00a0<em>s = (a+b+c)\/2<\/em>. The sqrt can be calculated by\u00a0<em>Math.sqrt()<\/em><\/p>\n<p>3. Write a method that performs a calculation that you learnt in high school mathematics. The method should take as parameters any variables of the calculations and return the result of the calculation.<\/p>\n<p>4. Write a method that takes a 4 digit number and then prints the number of units, tens, hundreds and thousands. For example:<\/p>\n<pre>Number: 1234\r\n4 units\r\n3 tens\r\n2 hundreds\r\n1 thousands<\/pre>\n<h2>Using conditions<\/h2>\n<p>1. Write a method that takes in one parameter\u00a0<em>year<\/em> and prints \u201cThis is a leap year\u201d, if the year is a leap year or \u201cThis is not a leap year\u201d if it is not. Recall that years are leap years if they are divisible by 4. Except years divisible by 100 are not leap years unless they are also divisible by 400.<br \/>\nHINT: the code\u00a0<em>x % y<\/em> returns the remainder when\u00a0<em>x<\/em> is divided by\u00a0<em>y<\/em><\/p>\n<p>2. Write a method that takes three integers and prints them out in increasing order.<\/p>\n<p>3. Write a method which takes in three real numbers\u00a0<em>a, b<\/em> and\u00a0<em>c<\/em> representing the coefficients of a quadratic equation\u00a0<em>ax<sup>2<\/sup> + bx + c<\/em> and uses the quadratic formula to determine one solution of the equation\u00a0<em>ax<sup>2<\/sup> + bx + c = 0<\/em>. Use an\u00a0<em>if statement<\/em> to ensure that the program works correctly for all numeric input.<\/p>\n<p>4. Write a method that takes a single integer,\u00a0<em>n<\/em> between 0 and 9 and prints out the word for that number. For example<\/p>\n<pre>Number: 0\r\nzero\r\nNumber: 1\r\none\r\nNumber: 9\r\nnine<\/pre>\n<h2>Using Loops<\/h2>\n<p>1. Write a method, which takes two parameters\u00a0<em>n<\/em> and\u00a0<em>pow<\/em> and returns the value of\u00a0<em>n<sup>pow<\/sup><\/em><\/p>\n<p>2. Write a method with a\u00a0<em>while<\/em> loop that prints out the numbers from 1 to 10 and their squares.<\/p>\n<p>3. Change your method in question 1 to use a\u00a0<em>for<\/em> loop.<\/p>\n<p>4. Modify your method in question 2 so that is prints \u201cNumber is even\u201d, if the number (or its square) is even. Otherwise print \u201cNumber is odd\u201d.<\/p>\n<p>5. Write a method that takes one parameter\u00a0<em>n<\/em> and outputs an\u00a0<em>n x n<\/em> square of asterisks. For example<\/p>\n<pre>Square: 5\r\n*****\r\n*****\r\n*****\r\n*****\r\n*****<\/pre>\n<p>6. Write a method that takes one parameter\u00a0<em>n<\/em> and outputs an\u00a0<em>n x n<\/em> triangle. For example<\/p>\n<pre>Triangle: 5\r\n    *\r\n   **\r\n  ***\r\n ****\r\n*****<\/pre>\n<p>7. Write a method, which takes one parameter, a positive integer\u00a0<em>n<\/em> and prints all the factors of\u00a0<em>n<\/em>. For example:<\/p>\n<pre>n: 1001\r\nThe factors of 1001 are:\r\n1\r\n7\r\n11\r\n13\r\n77\r\n91\r\n143\r\n1001<\/pre>\n<h2>Using Arrays<\/h2>\n<p>1. Write the method,\u00a0<em>void stars(int[] values)<\/em> which, for each element of\u00a0<em>values<\/em> prints out the number of asterisks corresponding to the integer. For example:<\/p>\n<pre> values: {3, 2, 1, 5, 5, 3, 1, 0, 2}\r\n***\r\n**\r\n*\r\n*****\r\n*****\r\n***\r\n*\r\n\r\n**<\/pre>\n<p>2. Write the method,\u00a0<em>boolean isIncreasing (int[] values)<\/em> which returns true if the values in the array are in increasing order. It should return false otherwise<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this lab, we&#8217;ll practice creating a new BlueJ project from scratch. Use the &#8220;New&#8221; menu item to make a new class. Call it Week4. All the exercises below can be done in the same class file. You may not be able to finish all the questions during the lab time. Make sure that 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-525","post","type-post","status-publish","format-standard","hentry","category-lab"],"_links":{"self":[{"href":"https:\/\/blogs.unsw.edu.au\/comp1400\/wp-json\/wp\/v2\/posts\/525","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=525"}],"version-history":[{"count":5,"href":"https:\/\/blogs.unsw.edu.au\/comp1400\/wp-json\/wp\/v2\/posts\/525\/revisions"}],"predecessor-version":[{"id":528,"href":"https:\/\/blogs.unsw.edu.au\/comp1400\/wp-json\/wp\/v2\/posts\/525\/revisions\/528"}],"wp:attachment":[{"href":"https:\/\/blogs.unsw.edu.au\/comp1400\/wp-json\/wp\/v2\/media?parent=525"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.unsw.edu.au\/comp1400\/wp-json\/wp\/v2\/categories?post=525"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.unsw.edu.au\/comp1400\/wp-json\/wp\/v2\/tags?post=525"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}