0% found this document useful (0 votes)
8 views

Lab 1

The document provides instructions for writing a simple "Hello World" Java program in 5 tasks: 1. Create a Java class called HelloWorld that contains a main method printing "Hello World" to the console. 2. Modify the program to print a different message of your choice. 3. Analyze the basic components of a Java program including classes, methods, and statements. 4. Add comments to the code to explain its functionality and the importance of documentation.

Uploaded by

fahadraufhashmi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Lab 1

The document provides instructions for writing a simple "Hello World" Java program in 5 tasks: 1. Create a Java class called HelloWorld that contains a main method printing "Hello World" to the console. 2. Modify the program to print a different message of your choice. 3. Analyze the basic components of a Java program including classes, methods, and statements. 4. Add comments to the code to explain its functionality and the importance of documentation.

Uploaded by

fahadraufhashmi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

OBJECT ORIENTATED PROGRAMMING

LAB REPORT #01


SEMESTER #02
GROUP MEMBERS
Muhammad Fahad Rauf Hashmi
Muzammil Ashraf
Afzal Ahmed
Zohaib Aasif
SUBMITTED
TO:
Mr. KAMRAN SAEED

NATIONAL UNIVERSITY OF MODERN LANGUAGES


|RAWALPINDI
Task 2: Writing Your First Java Program
• Create a new Java class called "HelloWorld."
• Write a simple Java program that displays "Hello, World!" on the console.
• Save the file and run the program to verify its output.
Objective:

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 {

public static void main(String[] args) {


System.out.println("Hello, World!");
}
}

3. Explanation of the code:


o The code defines a public class named HelloWorld.
o The class contains a public static method named main. This is the entry point of the program,
where execution begins.
o Inside the main method, the statement System.out.println("Hello, World!") is used to
print the message "Hello, World!" to the console.
4. Save the file:
o Save the HelloWorld.java file in a suitable location on your computer.
5. Run the program:
o You can run the program using a Java compiler and a command prompt or terminal:
 Open a command prompt or terminal window and navigate to the directory where you saved the
HelloWorld.java file.

 Type the following command and press Enter:


Bash
javac HelloWorld.java
 This command will compile the Java code and create a class file named HelloWorld.class.
 To run the program, type the following command and press Enter:
Bash
java 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

objects of the class.


 void: This keyword specifies that the method doesn't return any value.
 main(String[] args): This is the signature of the main method. The String[] args

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

output (usually the console).


Task 4: Experimenting with Output
• Modify the Java program to display a different message of your choice.
• Run the program again to observe the updated output.
1. Modify the message:
o Open the HelloWorld.java file in your text editor or IDE.
o Change the text inside the quotation marks of the System.out.println() statement to any
message you want to display. For example:
Java
public class HelloWorld {

public static void main(String[] args) {


System.out.println("Welcome to Java Programming!"); // Modified
message
}
}

2. Save the changes:


o Save the changes made to the HelloWorld.java file.
3. Recompile and run the program:
o Open a command prompt or terminal window and navigate to the directory where you saved the
HelloWorld.java file.

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 {

public static void main(String[] args) {


// This line displays a greeting message to the console
System.out.println("Welcome to Java Programming!");
}
}

Types of Comments in Java


 Single-line comments: Begin with // and extend until the end of the line. They're used
to add inline explanations within a code block.
 Multi-line comments: Begin with /* and end with */. They can span multiple lines and
are suitable for larger explanations or blocking out sections of code temporarily.
Importance of Comments and Documentation:
 Clarity: Clear comments make your code easier for you and others to understand. This
is especially important when you return to projects later or when collaborating on code
with others.
 Maintenance: Well-commented code is easier to maintain, debug, and modify.
Comments provide insights into the logic behind code decisions, reducing the time
needed to understand and make necessary changes.
 Readability: Good comments and documentation contribute to better code structure,
improving readability and overall code quality. This is essential for complex projects.

You might also like