In this weeks session we will continue to work with flow control. We will look back over the last two sessions and continue working on the tasks from those two sessions.

Question: Give me examples of the following structures

  • Arrays
  • For loops
  • If/elseif/else
  • switch/case


Session: User Input, Conditionals: switch and if

Session: Arrays, flow control and random number


Tasks for today:

Task 1: Finish off the tasks from the previous lessons.


Tasks from session one:

Task 1: Create a two-dimensional array and fill it with stars (or any other character that you like). Then print it to the console. Make it a 10 by 10 grid.

Hint: to print to the console you will use the code snippet: System.out.print(square[1][2]) to print on the same line and System.out.println(square[1][2]) to print a ‘new line’ after you have printed what’s in the braces.

Task 2: Extend the previous task and randomly ‘insert’ a different character into the array.

Hint: See section below for code snippet for choosing a random number.

Task 3: Extend further the example by randomly inserting a ‘tetromino shape’ into the grid. Define the┬átetromino using another array. Output might look like this:


Here we have inserted a tetromino shape into a 10 by 10 grid. Each time the code is run the tetromino will be in a different location.

Generate Random Number

To learn more about random number genration visit this very good tutorial:

private static int randomBetween(int min, int max) {

    Random rand = new Random();

    // nextInt is normally exclusive of the top value,
    // so add 1 to make it inclusive
    int num = rand.nextInt((max - min) + 1) + min;
    // System.out.println(num);

    return num;


Tasks from session two:

Task 1: Write a program that asks a simple question to the user, captures the answer and responds to that question with an answer that depends on user’s answer.

HINT: What is you favourite ice cream? Answer: Chocolate. Computer responds: Delicious

Task 2: Write a simple guessing game. The computer chooses a random number and the user must guess what it is. If the choice is incorrect give the user a hint. If the guess is less than the computer’s number print to the console ‘Too Small’, if the guess is more than the computer’s number print to the console ‘Too Big’.

HINT: in order to repeatedly ask the user the same question put the code in the following infinite loop

        while (true) {

            // Code to repeat here


when you want to exit use this code: System.exit(0);

Task 3: Write a Rock Paper Scissors game using all the constructs we have studied today.

Task 4: Extend the above task by writing a Rock Paper Scissor Lizard Spock game.

The rules are:

Scissors cuts Paper
Paper covers Rock
Rock crushes Lizard
Lizard poisons Spock
Spock smashes Scissors
Scissors decapitates Lizard
Lizard eats Paper
Paper disproves Spock
Spock vaporizes Rock
Rock crushes scissors