Assignment 1

The first assignment has to be handed in before September 24. Students should put their name, student number and Ariel login name on their assignment. There is an assignment box for this course on the first floor of CCB. The fourth part of the assignment consists of writing a small Java program. A print out of the program should be handed in (no documentation is required) and the program should be submitted electronically. Use AbsoluteDifferenceSequence.java as the filename of the Java program. On Ariel, type
submit 2011 a1 AbsoluteDifferenceSequence.java
to submit the program. If the file is successfully submitted the message
All files successfully submitted
is printed. For more details, see the manual page of submit. Programs that do not compile or run on Ariel are awarded an F. The letter grade for the assignment is a rough average of the letter grades for the four parts.

The first assignment can be found here.

The office hours of the teaching assistant can be found here.

A solution of the first assignment can be found here.

Reading material

Additional material

ImprovedFibonacci.java
ParameterizedFibonacci.java

A break statement can be used to exit from any block, not just from a switch statement. A break statement is most often used to immediately exit any block.

An unlabelled break statement terminates the innermost switch, for, while, or do.

public static boolean containsZero(int[] arrayOfInt) {
    boolean found = false;
    for (int i = 0; i < arrayOfInt.length; i++) {
        if (arrayOfInt[i] == 0) {
            found = true;
            break;
        }
    }
    return found;
}

To terminate an outer statement, label the outer statement and use its label name in the break statement. The outer statement need not be a switch, for, while, or do.

public static boolean containsZero(int[][] matrixOfInt) {
    boolean found = false;
    search:
        for (int i = 0; i < matrixOfInt.length; i++) {
            for (int j = 0; j < matrixOfInt[i].length; j++) {
                if (matrixOfInt[i][j] == 0) {
                    found = true;
                    break search;
                }
            }
        }
    return found;
}

A continue statement is often used to skip over part of an iteration. A continue statement can only be used within a for, while, or do.

An unlabelled continue statement terminates the current iteration of the innermost for, while, or do and starts the next iteration.

public static int sum(int[] arrayOfInt) {
    int sum = 0;
    for (int i = 0; i < arrayOfInt.length; i++) {
        if (arrayOfInt[i] == 0) {
            continue;
        }
        sum += arrayOfInt[i];
    }
    return sum;
}

To terminate the current iteration of an outer for, while, or do, label the outer statement and use its label name in the continue statement.

 
public static int sumOfProducts(int[][] matrixOfInt) {
    int sum = 0;
    summation:
        for (int i = 0; i < matrixOfInt.length; i++) {
            int product = 1;
            for (int j = 0; j < matrixOfInt[i].length; j++) {
                if (matrixOfInt[i][j] == 0) {
                    continue summation;
                } else {
                    product *= matrixOfInt[i][j];
                }
            }
            sum += product;
        }
    return sum;
}

Instead of

Integer.valueOf(args[0]).intValue()
as used in ParameterizedFibonacci.java, one can also use
Integer.parseInt(args[0])
to interpret the first command line argument as an integer.