Computer Programming 2
1
Operators
Week003 - Operator
Laboratory Exercise 003
Objective/s:
At the end of this activity, you should be able to:
Write and evaluate expressions to compute values
Write program using different operations
Understand and use the different kind of operators in a java program.
What to Prepare for the Activity:
NetBeans IDE 8.2 which could be downloaded from https://netbeans.org/downloads/
Java Standard Edition (SE) Development Kit 8 (JDK8).
Procedure:
1. Write a java program named JavaOperators and print the results of the following
expressions:
a. 30 / 5
b. 12.0/5
c. 10/5-8/2
d. 34*12 + 15*33
e. 42%2 + 1%16
Test Stem / Question Choices
A:
1: What is the result?
6
2.4
-2
903
1
B:
6 2.4 -2 93 23
C: syntax error
D:
Assessments
30 / 5
12.0/5
10/5-8/2
34*12 + 15*33
42%2 + 1%16
2. Now add the following:
a. “five ” + 5
b. “5” /5
c. 5 – “5”
Test Stem / Question Choices
2: How many errors are there? A: 3
B: 2
C: 1
D: 0
3. Try to correct the lines with error by providing the correct data type, save and recompile.
Test Stem / Question Choices
3: What is the result? A: five + 5
1
0
B: 10
1
0
C: 5+5
5/5
Computer Programming 2
3
Operators
5-5
D: nothing shows
4. Open the file that you have created and add the following codes in your main method:
int x = 0;
int y = 2;
int z = 4;
System.out.println(++x);
System.out.println(x++);
System.out.println(z % x);
System.out.println(x >> y);
a. Save the file.
b. Compile and run the program.
Test Stem / Question Choices
4: What is the output of this statement A: 3
System.out.println(++x);?
B: 2
C: 1
D: 0
5: What is the output of this statement A: 3
System.out.println(x++);?
B: 2
C: 1
D: 0
6: What is the output of this statement A: 3
System.out.println(z % x);?
B: 2
C: 1
D: 0
7: What is the output of this statement A: 3
Assessments
System.out.println(x >> y);? B: 2
C: 1
D: 0
5. Add also the following codes in your main method:
boolean a;
boolean b = true;
System.out.println(a && b);
System.out.println(a || b);
System.out.println(b ? a : b);
c. Save the file.
d. Compile and run the program.
Test Stem / Question Choices
8: What is the output of this statement A: true
System.out.println(a && b);?
B: false
C: 1
D: 0
9: What is the output of this statement A: true
System.out.println(a || b);?
B: false
C: 1
D: 0
10: What is the output of this statement A: true
System.out.println(b ? a : b);?
B: false
C: 1
D: 0
Create a NetBeans project for problem no. 6 below. The project name should be as
follows:
Project Name: Lab003_<lastname_firstname>
Example: Lab003_Blanco_Maria
Computer Programming 2
5
Operators
The Java filename for No. 5 should be MaxAndMin.java
Compress the NetBeans project into .rar or .zip format.
Only NetBeans project compressed in .rar or .zip format will be accepted.
6. Write a program that will declare and initialize 2 integer variables. The program should
compute the sum, difference, product, quotient (first integer/second integer) and
average of the 2 integers. It should also determine the maximum (higher value) and
the minimum (smaller value) between the two. Use the ? operator to implement the
maximum and minimum value.
Sample Output:
First Integer: 25
Second Integer: 3
Sum: 28
Difference: 22
Product: 75
Quotient: 8.333333333333334
Average: 14.0
Max integer: 25
Min integer: 3
Assessments