Lab Sheet 4: Expressions and Operators in Java

Arzath AreeffArzath Areeff
4 min read

Task 1: Assignment and Arithmetic Operators

Exercise 1: Arithmetic Operations

Objective: Write and execute a Java program to compute arithmetic expressions.

Steps:

  1. Create a Java class named ArithmeticOperations.

  2. Declare variables a = 10, b = 5.

  3. Compute and print the following:

    • a + b * 2

    • (a + b) * 2

    • a % b + 5

  4. 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:

  1. Create a Java class named IncrementDecrementDemo.

  2. Initialize int x = 5.

  3. Apply the following operations:

    • x += 3

    • x++

    • --x

  4. Print the final value of x.

  5. 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:

  1. Create a Java class named RelationalOperatorsDemo.

  2. Declare variables x = 3, y = 5.

  3. Print the results of the following expressions:

    • x == 3

    • x >= y

    • y < 10

    • x - y <= 0

  4. Compile and run the program.

Expected Output:

true  
false  
true  
true

Exercise 2: Logical Operators

Objective: Evaluate logical expressions with given values.

Steps:

  1. Create a Java class named LogicalOperatorsDemo.

  2. Initialize variables:

     int a = 5, b = 2, c = 4, d = 5;
    
  3. 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

  4. 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:

  1. Create a Java class named BitwiseDemo.

  2. Initialize int a = 60, int b = 13.

  3. Compute and print:

    • a & b

    • a | b

    • a ^ b

    • ~a

    • a << 2

    • a >> 2

  4. 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:

  1. Create a Java class named DistanceConverter.

  2. Take input (e.g., 2500 meters).

  3. Compute:

    • km = meters / 1000

    • remainingMeters = meters % 1000

  4. Print the result.

  5. 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:

  1. Create a Java class named TemperatureConverter.

  2. Take input (e.g., 45°F).

  3. Compute:

     celsius = (fahrenheit - 32) * 5.0 / 9;
    
  4. Print the result rounded to 2 decimal places.

  5. 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:

  1. Create a Java class named DigitExtractor.

  2. Take input (e.g., 456).

  3. Compute:

    • digit1 = num / 100

    • digit2 = (num / 10) % 10

    • digit3 = num % 10

  4. Print the digits.

  5. 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:

  1. Create a Java class named CircleArea.

  2. Take input for radius (e.g., 5.0).

  3. Compute:

     area = Math.PI * radius * radius;
    
  4. Print the result.

  5. Compile and run the program.

Expected Output (for radius 5.0):

Area: 78.53981633974483
0
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.