Lab Sheet 4: Expressions and Operators in Java


Task 1: Assignment and Arithmetic Operators
Exercise 1: Arithmetic Operations
Objective: Write and execute a Java program to compute arithmetic expressions.
Steps:
Create a Java class named
ArithmeticOperations
.Declare variables
a = 10
,b = 5
.Compute and print the following:
a + b * 2
(a + b) * 2
a % b + 5
Compile and run the program.
Expected Output:
Result1: 20
Result2: 30
Result3: 5
Exercise 2: Increment and Decrement Operators
Objective: Experiment with increment (++
) and decrement (--
) operators.
Steps:
Create a Java class named
IncrementDecrementDemo
.Initialize
int x = 5
.Apply the following operations:
x += 3
x++
--x
Print the final value of
x
.Compile and run the program.
Expected Output:
Final x: 8
Task 2: Relational and Logical Operators
Exercise 1: Relational Expressions
Objective: Write a program to test relational expressions.
Steps:
Create a Java class named
RelationalOperatorsDemo
.Declare variables
x = 3
,y = 5
.Print the results of the following expressions:
x == 3
x >= y
y < 10
x - y <= 0
Compile and run the program.
Expected Output:
true
false
true
true
Exercise 2: Logical Operators
Objective: Evaluate logical expressions with given values.
Steps:
Create a Java class named
LogicalOperatorsDemo
.Initialize variables:
int a = 5, b = 2, c = 4, d = 5;
Print the results of:
a == 5
b * d == c * c
d % b * c > 5 || c % b * d < 7
d % b * c > 5 && c % b * d < 7
Compile and run the program.
Expected Output:
true
false
true
false
Task 3: Bitwise and Shift Operators
Exercise 1: Bitwise Operations
Objective: Perform bitwise operations and observe results.
Steps:
Create a Java class named
BitwiseDemo
.Initialize
int a = 60
,int b = 13
.Compute and print:
a & b
a | b
a ^ b
~a
a << 2
a >> 2
Compile and run the program.
Expected Output:
a & b = 12
a | b = 61
a ^ b = 49
~a = -61
a << 2 = 240
a >> 2 = 15
Task 4: Practical Programming Problems
Exercise 1: Convert Meters to Kilometers and Meters
Objective: Write a program to convert a given distance in meters to kilometers and meters.
Steps:
Create a Java class named
DistanceConverter
.Take input (e.g.,
2500
meters).Compute:
km = meters / 1000
remainingMeters = meters % 1000
Print the result.
Compile and run the program.
Expected Output (for input 2500):
2 km and 500 m
Exercise 2: Fahrenheit to Celsius Conversion
Objective: Convert Fahrenheit to Celsius using the formula.
Steps:
Create a Java class named
TemperatureConverter
.Take input (e.g.,
45
°F).Compute:
celsius = (fahrenheit - 32) * 5.0 / 9;
Print the result rounded to 2 decimal places.
Test with
45°F
and-87°F
.
Expected Output:
45°F = 7.22°C
-87°F = -66.11°C
Exercise 3: Extract Digits of a 3-Digit Number
Objective: Write a program to print individual digits of a 3-digit number.
Steps:
Create a Java class named
DigitExtractor
.Take input (e.g.,
456
).Compute:
digit1 = num / 100
digit2 = (num / 10) % 10
digit3 = num % 10
Print the digits.
Compile and run the program.
Expected Output (for input 456):
4, 5, 6
Exercise 4: Calculate the Area of a Circle
Objective: Compute the area of a circle using Math.PI
.
Steps:
Create a Java class named
CircleArea
.Take input for radius (e.g.,
5.0
).Compute:
area = Math.PI * radius * radius;
Print the result.
Compile and run the program.
Expected Output (for radius 5.0):
Area: 78.53981633974483
Subscribe to my newsletter
Read articles from Arzath Areeff directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Arzath Areeff
Arzath Areeff
I co-founded digizen.lk to promote online safety and critical thinking. Currently, I’m developing an AI app to fight misinformation. As Founder and CEO of ideaGeek.net, I help turn startup dreams into reality, and I share tech insights and travel stories on my YouTube channels, TechNomad and Rz Omar.