Activity 1.1 - Executing some simple assignment statements

Topic

This exercise allows you to write some simple assignment statements as part of a Java program.

Materials

In addition to the Solution document, the project for this exercise contains one class:

Task

The class SimpleStatements contains a main method and there are a number of declarations of integer variables in this method. You are to assign values to these variables, and use further assignment statements to carry out arithmetic calculations involving these variables. After each stage, the program is to display the current contents of the variables.

Instructions

Open the project Ex1_1. In the Projects window expand the project tree and locate the source file SimpleStatements.java. Double-click on the file to open the Edit window for the class. Carry out the following instructions in turn, and at each stage build and run the program to see the effect.

  1. First run the program without making any changes. This should give several compilation errors. Study the message carefully and explain what you think is happening here.
  2. Insert three assignment statements to give initial values to the variables x, y and z as indicated by the comments in the source file. Run the program again - it should execute successfully this time. Is the output what you would expect now?
  3. Add three further assignment statements to modify the contents of variables x, y and z, as indicated by the comments in the source file. Insert (or copy and paste from above) System.out.println statements to display the new contents of the three variables. The text of the first of these output statements should say "After further calculations:" Now work out what output you would expect, then run the program again and check that the output is as you expected.