CS2514

Introduction to Java

Dr Derek Bridge

School of Computer Science & Information Technology

University College Cork

Comments

Statements

Blocks

Conditional statements

PythonJava
if x < y:
    print('x is smaller than y')
elif x == y:
    print('x is equal to y')
else:
    print('x is larger than y')
 
if (x < y) {
    System.out.println('x is smaller than y');
} else if (x === y) {
    System.out.println('x is equal to y');
} else {
    System.out.println('x is larger than y');
}

In the Java, note:

Boolean expressions

while loops

PythonJava
while x < y:
    x = x + 1
    print(x)
  
while(x < y) {
    x = x + 1;
    System.out.println(x);
}

In the Java, note:

for loops

for loops

for loops increment at the end of the loop body

Other control flow statements

Class exercise: convert to Java

from random import choice

secret_num = choice( range( 1, 101 ) )

while True:
    guess = int( input( "What's your guess: " ) )
    if guess < secret_num :
        print( "    ", guess, "is too low" )
    elif guess > secret_num :
        print( "    ", guess, "is too high" )
    else :
        print( "    ", guess, "is correct" )
        break

Solution

import java.util.Scanner;
import java.util.Random;

class GuessingGame {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Random randy = new Random();
        int secret_num = randy.nextInt(100) + 1; 
        while (true) {
            System.out.print("What's your guess: " );
            int guess = sc.nextInt();
            if (guess < secret_num) {
                System.out.println( "     " + guess + " is too low" );
            } else if (guess > secret_num) {
                System.out.println( "     " + guess + " is too high" );
            } else {
                System.out.println( "     " + guess + " is correct" );
                break;
            }
        }
    }
        
}

Variables

Local variables

Initialization of local variables

Class exercise

Which lines give compile-time errors?

class InitializationExercise {

    public static void main(String[] args) {
    
        java.util.Scanner sc = new java.util.Scanner(System.in);
        
        int x = 3;
        int y;
        int z;
        
        y = sc.nextInt();
        System.out.println(x + y + z);
        
        z = sc.nextInt();
        int i;
        if (z < 999) {
            i = 100;
        }
        System.out.println(i);
        
        int j;
        if (z < 999) {
            j = 100;
        } else {
            j = x;
        }
        System.out.println(j);
        
    }
}

Beware of the Python

This is why Python is such a dangerous language to use for real-world software:

z = int( input() )
if z < 999 :
    i = 100
print(i)

Scope of local variables

Java exercise

Draw the scope of each local variable onto your printout

public static void main(String[] args) {
    java.util.Scanner sc = new java.util.Scanner(System.in);
    int x = sc.nextInt();
    if (x < 0) {
        int y = sc.nextInt();
        x = x + y;
    } else {
        double z = sc.nextDouble();
        x = x * z;
    }
    System.out.println(x);
}

Python exercise

Draw the scope of each local variable onto your printout

def stupid_function() :
    x = int( input() )
    if x < 0 :
        y = int( input() )
        x = x + y
    else :
        z = float( input() )
        x = x * z
    print(x);

Java exercise

Draw the scope and find the compile-time errors

public static void main(String[] args) {
    java.util.Scanner sc = new java.util.Scanner(System.in);
    int x = sc.nextInt();
    if (x < 0) {
        int y = sc.nextInt();
        x = x + y;
    } 
    System.out.println(x);
    System.out.println(y);
    
    if (x < 100) {
        int z = sc.nextInt();
        x = x + z;
    } else {
        double z = sc.nextDouble();
        x = x * z;
    }
    System.out.println(z);
}

Python exercise

Draw the scope. Are there errors?

def stupid_function :
    x = int( input() )
    if x < 0 :
        y = int( input() )
        x = x + y
    print(x)
    print(y)
    
    if x < 100 :
        z = int( input() )
        x = x + z
    else :
        z = float( input() )
        x = x * z
    print(z)

Java exercise

Draw the scope and find the compile-time errors (if any)

public static void main(String[] args) {
    int x = 2;
    for (int i = 0; i < 10; i += 1) {
        x = x * x;
    }
    System.out.println(i);
    System.out.println(x);
}

Java exercise

Draw the scope and find the compile-time errors (if any)

public static void main(String[] args) {
    int x = 2;
    int i;
    for (i = 0; i < 10; i += 1) {
        x = x * x;
    }
    System.out.println(i);
    System.out.println(x);
}