Final Lab Sheet: Introduction to Java Programming


Lab Practical 1: Introduction to Java Programming
Objective: Write a simple Java program to print "Hello, World!" and compile/run it using the command line.
Task:
Write a Java program that prints
"Hello, World!"
.Compile and run it using
javac
andjava
commands.Modify the program to print your name in the next line.
Lab Practical 2: Using an IDE (IntelliJ/Eclipse)
Objective: Familiarise with an IDE by creating and running a Java project.
Task:
Create a new Java project in IntelliJ/Eclipse.
Write a program that calculates the sum of two numbers (5 and 7) and prints the result.
Debug the program by setting a breakpoint and inspecting variables.
Lab Practical 3: Variables and Data Types
Objective: Demonstrate understanding of Java variables and data types.
Task:
Declare variables to store:
Your age (
int
),Your height in meters (
double
),Your first initial (
char
),Whether you are a student (
boolean
).
Print all variables with descriptive messages (e.g.,
"My age is 20"
).
Lab Practical 4: Expressions and Operators
Objective: Use arithmetic and logical operators in Java.
Task:
Write a program that calculates the area of a rectangle (length = 5.5, width = 3.2).
Check if a number (input) is even or odd using the modulus operator.
Swap two numbers without a temporary variable.
Lab Practical 5: Control Statements (If-Else, Switch)
Objective: Implement decision-making in Java.
Task:
Write a program that checks if a number is positive, negative, or zero.
Use a
switch
statement to print the day of the week based on a number (1-7).Determine the largest of three numbers using nested
if-else
.
Lab Practical 6: Loops (For, While, Do-While)
Objective: Use loops for repetitive tasks.
Task:
Print numbers from 1 to 10 using a
for
loop.Calculate the factorial of a number (e.g., 5! = 120) using a
while
loop.Print all even numbers between 1 and 20 using a
do-while
loop.
Lab Practical 7: Arrays
Objective: Work with single and multi-dimensional arrays.
Task:
Create an array of 5 integers and find the sum of all elements.
Find the largest number in an array.
Transpose a 2x3 matrix (hardcoded values).
Lab Practical 8: Methods and Parameter Passing
Objective: Write reusable methods in Java.
Task:
Create a method
isPrime()
that checks if a number is prime.Write a method
calculateCircleArea()
that takes radius as input and returns the area.Overload a method
print()
to acceptint
,String
, anddouble
parameters.
Lab Practical 9: Exception Handling
Objective: Handle runtime errors using try-catch blocks.
Task:
Write a program that divides two numbers and handles
ArithmeticException
.Use
try-catch
to handleArrayIndexOutOfBoundsException
when accessing an array.Throw a custom exception if a user enters a negative number.
Lab Practical 10: Classes and Objects
Objective: Implement OOP concepts in Java.
Task:
Create a
Student
class with fields (name
,rollNo
,grade
) and methods to display details.Add a constructor to initialize the object.
Use access modifiers (
private
,public
) and provide getter/setter methods.
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.