COMP1400 – Programming for Designers

Class announcements for Programming for Designers

COMP1400 – Programming for Designers random header image

Lab Week 4 – Loops and Arrays

Posted by on August 16th, 2014 · Lab

In this lab, we’ll practice creating a new BlueJ project from scratch. Use the “New” 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.

The self-test quiz for this week is here.

Basic Output

1. Write a method that prints the string “Hello World” 8 times. The output should be:

Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World

2. Write a method that prints your name, age and favourite number, using integers for the numbers. For example:

My name is Fred, I am 18 years old, my favourite number is 2.

Simple Calculations

1. Write a method, which takes one parameter n and returns the number squared.

2. Write a method, which takes three parameters a, b and c to calculate the area of a triangle using Heron’s formula: area = sqrt(s(s-a)(s-b)(s-c)). Where s is the “semi-perimeter” given by s = (a+b+c)/2. The sqrt can be calculated by Math.sqrt()

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.

4. Write a method that takes a 4 digit number and then prints the number of units, tens, hundreds and thousands. For example:

Number: 1234
4 units
3 tens
2 hundreds
1 thousands

Using conditions

1. Write a method that takes in one parameter year and prints “This is a leap year”, if the year is a leap year or “This is not a leap year” 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.
HINT: the code x % y returns the remainder when x is divided by y

2. Write a method that takes three integers and prints them out in increasing order.

3. Write a method which takes in three real numbers a, b and c representing the coefficients of a quadratic equation ax2 + bx + cand uses the quadratic formula to determine one solution of the equation ax2 + bx + c = 0. Use an if statement to ensure that the program works correctly for all numeric input.

4. Write a method that takes a single integer, n between 0 and 9 and prints out the word for that number. For example

Number: 0
zero
Number: 1
one
Number: 9
nine

Using Loops

1. Write a method, which takes two parameters n and pow and returns the value of npow

2. Write a method with a while loop that prints out the numbers from 1 to 10 and their squares.

3. Change your method in question 1 to use a for loop.

4. Modify your method in question 2 so that is prints “Number is even”, if the number (or its square) is even. Otherwise print “Number is odd”.

5. Write a method that takes one parameter n and outputs an n x n square of asterisks. For example

Square: 5
*****
*****
*****
*****
*****

6. Write a method that takes one parameter n and outputs an n x n triangle. For example

Triangle: 5
    *
   **
  ***
 ****
*****

7. Write a method, which takes one parameter, a positive integer n and prints all the factors of n. For example:

n: 1001
The factors of 1001 are:
1
7
11
13
77
91
143
1001

Using Arrays

1. Write the method, void stars(int[] values) which, for each element of values prints out the number of asterisks corresponding to the integer. For example:

 values: {3, 2, 1, 5, 5, 3, 1, 0, 2}
***
**
*
*****
*****
***
*

**

2. Write the method, boolean isIncreasing (int[] values) which returns true if the values in the array are in increasing order. It should return false otherwise

 

No Comments so far ↓

There are no comments yet...Kick things off by filling out the form below.

You must log in to post a comment.