Back to all lessons


Lesson 8

Methods, arrays

Task 0

Create a new method, which prints your name on screen.


public class SomeClass {
    public static void main(String[] args) {
        myMethod();
    }
    public static void myMethod() {
        // your instruction
    }
}
            
Task 1

Create a new method, which takes a boolean param. Print message depending of the param's value.


public class SomeClass {
    public static void main(String[] args) {
        myMethod(5);
    }
    public static void myMethod(int a) {
        System.out.println(a);
    }
}
                
Task 2

Create a new method for every task in this lesson.

Task 3

Declare an array of any type (e.g. int).


public class Main {
    public static void main(String[] args) {
        // change "type" for int, char, boolean etc.
        type[] nameOfArray = new type[sizeOfArray];
    }
}
            
Task 4

Declare one dimensional array of int type which contains 10 elements.

Then print an information confirming the length of the array.

Task 5

Declare one dimensional array of int type which contains 10 elements.

Then fill in the array with random values 0 or 1.


public class Main {
    public static void main(String[] args) {
        // Prints a 'random' number in range [0, 100]
        System.out.println(
            Math.random()*101
        );
    }
}
                        
Task 6

Write a program that counts how many 0's and 1's are in the array from the previous task.

Task 7

Create a character array (char as type) and put there your nickname / name.

Then display whole message (character by character).

Task 8

Create a method "printIntegerArray", which takes an array as a param and displays it row by row.

Task 9

Create a method "printSumOfNumbersInArray", which takes an array as a param and displays a sum of all elements.