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.javais saved.Compile the Java program using the
javaccommand:javac HelloWorld.javaRun the compiled Java program using the
javacommand:java HelloWorldObserve the output:
Hello, World!
Task 2: Exploring Java Features
Case Sensitivity:
Modify the
HelloWorld.javaprogram by changingSystem.out.printlntosystem.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.javaprogram using both single-line (//) and multi-line (/* ... */) comments.Modify the
System.out.printlnstatement to include an escape sequence (e.g.,\nfor a new line,\tfor 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.classfile in the same directory.Open the
.classfile 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
.javafile.Compile the program using the
javaccommand:javac FileName.javaRun the program using the
javacommand:java FileNameObserve 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.




