Back to all lessons


Lesson 9

Methods, arrays

Task 0
  1. Create a new numeric variable and set any value.
  2. Pass the variable to a method. Modify the value of a given parameter.
  3. After calling the method display value of the variable.

Repeat these steps for an array (change value of array's element).

Task 1

What is the difference between preincrementation (++a) and postincrementation (a++)?


// Run and check!
public class Example {
    public static void main(String[] args) {
        int a = 2;
        int b = 2;
        System.out.println(++a);
        System.out.println(b++);
        System.out.println(a);
        System.out.println(b);
    }
}
            
Task 2

Declare two variables with byte as a type. Display result of AND (&), OR (|) and XOR(^) bit by bit.

Task 3

Left shift operator (<<) moves bits to the left. Create a loop, that displays a result of using left shift operator with incerementing parameter.


// Hint
public class Example {
    public static void main(String[] args) {
        int num = 1;
        System.out.println(num << 1);
        System.out.println(num << 2);
        System.out.println(num << 3);
    }
}
                
Task 5

Rewrite code from previous to achieve same results, but using multiplication instead of left shift.

Task 6

What is a type of variable, that suits for these literals:

  • 1
  • 0x23F
  • '  '
  • 2.23e19
  • 214343434L
Task 7

There is a 10 chararacters array. Find the index of the lowest value.

Task 8

Create a program, that sorts an integer array.

Task 9 (not obligatory)

Create Tic Tac Toe.

Some hints:

  1. Store a state of the board in 9 elements array.
  2. Read an integer, which tells we want to put 'x' or 'o'
  3. Until game is not over, print board and wait for move.
  4. Check for winning (losing) condition.