{"id":325,"date":"2012-08-13T18:05:19","date_gmt":"2012-08-14T01:05:19","guid":{"rendered":"http:\/\/blogs.unsw.edu.au\/infs1609\/?p=325"},"modified":"2012-08-13T18:05:19","modified_gmt":"2012-08-14T01:05:19","slug":"extra-practice-weeks-1-4","status":"publish","type":"post","link":"https:\/\/blogs.unsw.edu.au\/infs1609\/blog\/2012\/08\/extra-practice-weeks-1-4\/","title":{"rendered":"Extra Practice Weeks 1 &#8211; 4"},"content":{"rendered":"<p>These are additional practice questions to be done in your spare time, covering content up to Week 4 (Arrays). They are not compulsory, however\u00a0<strong><em>the more you practice programming, the better you get.<\/em><\/strong> You should be able to answer all these questions by the end of the week.<\/p>\n<p>For these questions create a new BlueJ project, and a new Java Class. All can be done in the same class file<\/p>\n<h2>Basic Output<\/h2>\n<p>1. Write a method that prints the string &#8220;Hello World&#8221; 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 <em>n<\/em> and returns the number squared.<\/p>\n<p>2. Write a method, which takes three parameters <em>a, b<\/em> and <em>c<\/em> to calculate the area of a triangle using Heron&#8217;s formula: <em>area = sqrt(s(s-a)(s-b)(s-c))<\/em>. Where <em>s<\/em> is the &#8220;semi-perimeter&#8221; given by <em>s = (a+b+c)\/2<\/em>. The sqrt can be calculated by <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 <em>year<\/em> and prints &#8220;This is a leap year&#8221;, if the year is a leap year or &#8220;This is not a leap year&#8221; 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 <em>x % y<\/em> returns the remainder when <em>x<\/em> is divided by <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 <em>a, b<\/em> and <em>c<\/em> representing the coefficients of a quadratic equation <em>ax<sup>2<\/sup> + bx + c<\/em> and uses the quadratic formula to determine one solution of the equation <em>ax<sup>2<\/sup> + bx + c = 0<\/em>. Use an <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, <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 <em>n<\/em> and <em>pow<\/em> and returns the value of <em>n<sup>pow<\/sup><\/em><\/p>\n<p>2. Write a method with a <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 <em>for<\/em> loop.<\/p>\n<p>4. Modify your method in question 2 so that is prints &#8220;Number is even&#8221;, if the number (or its square) is even. Otherwise print &#8220;Number is odd&#8221;.<\/p>\n<p>5. Write a method that takes one parameter <em>n<\/em> and outputs an <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 <em>n<\/em> and outputs an <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 <em>n<\/em> and prints all the factors of <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, <em>void stars(int[] values)<\/em> which, for each element of <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, <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","protected":false},"excerpt":{"rendered":"<p>These are additional practice questions to be done in your spare time, covering content up to Week 4 (Arrays). They are not compulsory, however\u00a0the more you practice programming, the better you get. You should be able to answer all these questions by the end of the week. For these questions create a new BlueJ project, [&hellip;]<\/p>\n","protected":false},"author":345,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[359],"tags":[],"class_list":["post-325","post","type-post","status-publish","format-standard","hentry","category-announcements"],"_links":{"self":[{"href":"https:\/\/blogs.unsw.edu.au\/infs1609\/wp-json\/wp\/v2\/posts\/325","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\/345"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.unsw.edu.au\/infs1609\/wp-json\/wp\/v2\/comments?post=325"}],"version-history":[{"count":48,"href":"https:\/\/blogs.unsw.edu.au\/infs1609\/wp-json\/wp\/v2\/posts\/325\/revisions"}],"predecessor-version":[{"id":373,"href":"https:\/\/blogs.unsw.edu.au\/infs1609\/wp-json\/wp\/v2\/posts\/325\/revisions\/373"}],"wp:attachment":[{"href":"https:\/\/blogs.unsw.edu.au\/infs1609\/wp-json\/wp\/v2\/media?parent=325"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.unsw.edu.au\/infs1609\/wp-json\/wp\/v2\/categories?post=325"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.unsw.edu.au\/infs1609\/wp-json\/wp\/v2\/tags?post=325"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}