Topics:

  • data types and variables
  • math operations
  • conditionals and loops
  • printing sequences
    • assignment: sequences, FizzBuzz, Fibonacci

Assignment

Hello.java

public class Hello
{
    public static void hello()
    {
        System.out.println("Hello, world!");
    }

    public static void printSequence()
    {
        // 5, 10, 15, 20, 25, ...

        int number = 5;

        for (int i=0; i<5; i++)
        {
            System.out.println(number);
            number += 5; // recursive definition
        }
    }

    public static void printSequence2()
    {
        // 5, 10, 15, 20, 25, ...
        
        for (int i=5; i<25; i+=5)
            System.out.println(i);
    }

    public static void printSequence3()
    {
        // 5, 10, 15, 20, 25, ...
        
        for (int i=0; i<5; i++)
            System.out.println(i*5); // explicit formula
    }

    public static void main(String[] args)
    {
        hello();

        printSequence();
        System.out.println();

        printSequence2();
        System.out.println();

        printSequence3();
        System.out.println();
    }
}

Output:

Hello, world!
5
10
15
20
25

5
10
15
20

0
5
10
15
20

Sequences.java

public class Sequences
{
    public static void printNumbers()
    {
        for (int i=0; i<10; i++)
        {
            if (i%2 == 0)
                System.out.println("Even!");
            else if (i == 7)
            {
                System.out.println("Lucky number 7!");
            }
            else
                System.out.println(i);
        }
    }

    public static void main(String[] args)
    {
        printNumbers();
    }
}

Output:

Even!
1
Even!
3
Even!
5
Even!
Lucky number 7!
Even!
9