Lab 1
Lab 1
The objective of this task is to create a simple Java program that prints "Hello, World!"
to the console. This program serves as an introduction to basic Java syntax and
structure.
Steps:
1. Create a new Java file:
o Open a text editor or an integrated development environment (IDE) suitable for Java
programming.
o Create a new file and name it HelloWorld.java.
2. Write the Java program:
o Type the following code into the HelloWorld.java file:
Java
public class HelloWorld {
If everything is done correctly, you should see the following output on the console:
Hello, World!
Congratulations! You have successfully written and run your first Java program.
Task 3: Understanding the Basic Structure
• Analyze the Java program you wrote in Task 2.
• Identify the main components of a Java program, including the class declaration,
method declaration, and statements
Components of a Java Program:
The Java program you wrote in Task 2 demonstrates several essential components of
any Java program:
1. Class Declaration:
o public class HelloWorld { ... }: This line declares a public class named HelloWorld.
A class is the fundamental building block of a Java program. It acts as a blueprint defining the
properties (variables) and behaviors (methods) of objects.
2. Method Declaration:
o public static void main(String[] args) { ... }: This line declares a public static
method named main. Methods are defined within classes and encapsulate specific actions or
functionalities.
public: This keyword indicates that the method can be accessed from any other class.
static: This keyword signifies that the method is associated with the class itself, not individual
parameter allows the method to receive command-line arguments when the program is
executed.
3. Statements:
o System.out.println("Hello, World!");: This line is a statement within the main method.
Statements are instructions that define what the program should execute. In this case, the
System.out.println() method is used to print the message "Hello, World!" to the standard
o Run the following commands to compile and run the modified program:
Bash
java HelloWorld.java
java HelloWorld
o If everything is done correctly, you should now see your new message displayed on the
console.
Congratulations! You have successfully modified the Java program to display your
chosen message. This demonstrates how you can change the program's output by
altering the value passed to the System.out.println() method.
Task 5: Comments and Documentation
• Add comments to your Java program to explain its functionality.
• Understand the importance of code documentation for program clarity and
maintenance.
Modified Code with Comments:
Java
public class HelloWorld {