Discussion of Activity 1.1 - Executing some simple assignment statements

Sample solutions

  1. You should get three compilation errors indicating that variables x, y and z "might not have been initialized" and therefore their values cannot be displayed by the System.out.println statements.

    This is an example of a situation where Java does not initialize variables to a default value. To correct these errors, you need to explicitly assign an initial value to each variable (as in the next task).

  2. You should have added something like this
    x = 23;
    y = 333;
    z = 99;
    

    Note that it is important to use lower case names for x, y and z, as that is how they have been declared (since Java is case-sensitive).

    We could also have carried out declaration and initialization in one statement as follows:

    int x = 23;
    int y = 333;
    int z = 99;
    

    The output should be:

    After initialization:
    x contains 23
    y contains 333
    z contains 99
    
  3. You should have added something like this
    x = x + 12;
    y = y - 100;
    z = x * y;
    System.out.println("After further calculations:");
    System.out.println("x contains " + x );
    System.out.println("y contains " + y );
    System.out.println("z contains " + z );
    

    The output should be:

    After initialization:
    x contains 23
    y contains 333
    z contains 99
    After further calculations:
    x contains 35
    y contains 233
    z contains 8155