Lab Sheet 1: Introduction to Computer Programming and Java

Arzath AreeffArzath Areeff
3 min read

Task 1: Introduction to Java

  1. 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
      
  2. 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.

  3. 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

  1. Case Sensitivity:

    • Modify the HelloWorld.java program by changing System.out.println to system.out.println.

    • Compile the program and observe the error message.

    • Discuss why Java is case-sensitive.

  2. 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

  1. Exploring Bytecode:

    • After compiling HelloWorld.java, locate the HelloWorld.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:

  1. Write each program in a separate .java file.

  2. Compile the program using the javac command:

     javac FileName.java
    
  3. Run the program using the java command:

     java FileName
    
  4. 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.

1
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.