Lab2 - Java Variables
Lab2 - Java Variables
Instructions:
Task # 1: Creating a new Project in Eclipse.
1. Find the Eclipse icon in your PC. It should on Desktop. Then double click on the Eclipse
icon.
2. Select the Desktop as your workspace directory.
3. Go to File èNew è Java Project.
4. Write the Project Name “Lab2”. Then, Click Next è Finish.
Exercise1:
Creating a new java file called “Lab2Exercise1”.
1. Right-Click on the project folder you just create. You can find it in the left side of the editor.
Then, go to AddèNewè Class.
2. Write Lab2Exercise1 for the class name. Then, click Finish.
3. Copy the following code into the class editor.
4. If there is no syntax error in your code, then run the file (go to Run on menu bar èRun)
Exercise2:
1. Add to the project, Lab2, a new java file and name it Lab2Exercise2
2. Declare two variables int a, int b
3. Initialize the two variables above (a = 27, b = 10).
4. Calculate the addition, subtraction, multiplication, division, and modulo (remainder) of a and
b, and then display the result as following:
a + b is 37
a - b is 17
a * b is 270
a / b is 2.7
a % b is 7
Exercise3:
1. Add to the project, Lab2, a new java file and name it Lab2Exercise3
1. Declare three variables int x, double y, and float z.
2. Initialize the three variables above (x = 9, y = 2.3, and z = 5.2).
3. Display the result of each of the following arithmetic expressions using a System.out.println
( ) statement. In the Interpretation column, find out the precedence rules discussed in the
lecture.
Arithmetic Expression Result Interpretation
x+y*z
x/y*z
x/2+y/2
x/2
x%2
x%5*3+1
(y + 3) * 2
z / (1 + 1)
Exercise4:
1. Add to the project, Lab2, a new java file and name it Lab2Exercise4
2. Declare an int variable i initialize it to 4, and then find the outputs of the following increment
and decrement statements following the same order:
System.out.println (i);
System.out.println (i++);
System.out.println (i);
System.out.println (--i);
System.out.println (i--);
System.out.println (i);
Exercise5:
1. Add to the project, Lab2, a new java file and name it Lab2Exercise5
2. Copy the following code.