Lab Sheet 1: Introduction to Computer Programming and Java


Task 1: Introduction to Java
Setting Up Java:
Ensure that the JDK is installed on your computer.
Verify the installation by running the following command in the terminal/command prompt:
java -version javac -version
Writing Your First Java Program:
Open a text editor and write the following Java program:
public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World!"); } }
Save the file as
HelloWorld.java
.
Compiling and Running the Java Program:
Open the terminal/command prompt and navigate to the directory where
HelloWorld.java
is saved.Compile the Java program using the
javac
command:javac HelloWorld.java
Run the compiled Java program using the
java
command:java HelloWorld
Observe the output:
Hello, World!
Task 2: Exploring Java Features
Case Sensitivity:
Modify the
HelloWorld.java
program by changingSystem.out.println
tosystem.out.println
.Compile the program and observe the error message.
Discuss why Java is case-sensitive.
Comments and Escape Sequences:
Add comments to your
HelloWorld.java
program using both single-line (//
) and multi-line (/* ... */
) comments.Modify the
System.out.println
statement to include an escape sequence (e.g.,\n
for a new line,\t
for a tab).Recompile and run the program to see the effects of the escape sequences.
Example:
public class HelloWorld {
public static void main(String[] args) {
// This is a single-line comment
/* This is a
multi-line comment */
System.out.println("Hello,\n\tWorld!");
}
}
Task 3: Understanding Java Bytecode and JVM
Exploring Bytecode:
After compiling
HelloWorld.java
, locate theHelloWorld.class
file in the same directory.Open the
.class
file in a text editor (it will appear as gibberish) and discuss with your friend what bytecode is and how it is used by the JVM.
Excercices
Exercise 1: Print Your Name
Question: Write a Java program to print your name on the console.
Expected Output:
My name is John Doe
Exercise 2: Print Your Index Number
Question: Write a Java program to print your index number on the console.
Expected Output:
My index number is 12345
Exercise 3: Print Your Name and Index Number
Question: Write a Java program to print both your name and index number on the console.
Expected Output:
My name is Jane Doe
My index number is 67890
Exercise 4: Print a Pattern
Question: Write a Java program to print the following pattern using System.out.println()
:
Expected Output:
*
**
***
****
*****
Exercise 5: Print a Welcome Message
Question: Write a Java program to print a welcome message for your college or university.
Expected Output:
Welcome to HNDIT College!
Exercise 6: Print Multiple Lines
Question: Write a Java program to print the following lines using a single System.out.println()
statement:
Expected Output:
Line 1
Line 2
Line 3
Exercise 7: Print a Box Using Asterisks
Question: Write a Java program to print the following box using asterisks (*
):
Expected Output:
*****
* *
* *
* *
*****
Instructions for Students:
Write each program in a separate
.java
file.Compile the program using the
javac
command:javac FileName.java
Run the program using the
java
command:java FileName
Observe the output and ensure it matches the expected result.
Learning Outcomes:
Understand the basic structure of a Java program.
Learn how to use
System.out.println()
to print output.Gain familiarity with compiling and running Java programs.
Practice writing simple Java programs to build confidence.
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.