LAB MANUAL
LAB #02
Course Title: Programming Fundamentals LAB
Course Code: IT24-313
Topic: Basic Syntax and Debugging: Practicing Java syntax, understanding errors, and
using debugging tools.
Instructor: Uzair Anees Bhutto | (IT) UMPK
Lab Objective: The objective of this lab is to familiarize students with the basic syntax of Java,
help them understand common errors, and introduce them to debugging tools in Eclipse. By
the end of this lab, students will be able to:
1. Write Java programs using correct syntax.
2. Identify and fix common syntax and runtime errors.
3. Use Eclipse debugging tools to trace and resolve issues in their code.
Technologies/Tools Required:
JDK (Java Development Kit): The software development environment for writing
Java applications.
Eclipse IDE: An integrated development environment for Java.
Step 1: Open the Existing Project
1. Launch Eclipse:
o Open Eclipse IDE on your computer
2. Open the Project:
o If the project ITPROGRAMMING FUNDAMENTALS_LAB is not already
open, go to File > Open Projects from File System.
o Select the project directory and click "Finish."
Step 2: Review Basic Java Syntax
1. Structure of a Java Program:
o A Java program consists of classes and methods.
o The main method is the entry point of the program.
o Example:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
2. Variables and Data Types:
o Variables store data.
o Common data types: int, double, String, boolean, etc.
o Example:
int number = 10;
double pi = 3.14;
String name = "Java";
boolean isFun = true;
3. Control Structures:
o if-else, for, while, and switch statements.
o Example:
int age = 18;
if (age >= 18) {
System.out.println("You are an adult.");
} else {
System.out.println("You are a minor.");
}
Step 3: Create a New Java Class in the Existing Project
1. Create a New Class:
o Right-click on the src folder in the ITPROGRAMMING
FUNDAMENTALS_LAB project.
o Select New > Class.
o Name the class lab_02.java.
o Check the box for public static void main(String[] args) to create the main
method.
o Click "Finish."
2. Write the Code:
o Add the following code to practice basic syntax:
public class SyntaxExample {
public static void main(String[] args) {
// Variables and Data Types
int num1 = 5;
int num2 = 10;
int sum = num1 + num2;
System.out.println("Sum: " + sum);
// Control Structures
if (sum > 10) {
System.out.println("Sum is greater than 10.");
} else {
System.out.println("Sum is 10 or less.");
}
}
}
3. Run the Program:
o Right-click on the code editor and select Run As > Java Application.
Step 4: Understanding Common Errors
1. Syntax Errors:
o Caused by incorrect syntax (e.g., missing semicolons, unmatched braces).
o Example:
int x = 5 // Missing semicolon
o Fix: Add a semicolon at the end of the line.
2. Runtime Errors:
o Occur during program execution (e.g., division by zero, null pointer).
o Example:
int result = 10 / 0; // Division by zero
o Fix: Ensure the divisor is not zero.
3. Logical Errors:
o The program runs but produces incorrect results.
o Example:
int sum = num1 - num2; // Incorrect operation
o Fix: Use the correct operation (+ instead of -)
Step 5: Use Debugging Tools in Eclipse
1. Set Breakpoints:
o Click on the left margin of the code editor next to the line where you want to
pause execution (e.g., int sum = num1 + num2;).
2. Start Debugging:
o Right-click on the code editor and select Debug As > Java Application.
o The program will pause at the breakpoint.
3. Inspect Variables:
o In the Debug perspective, use the Variables tab to inspect the values
of num1, num2, and sum.
4. Step Through Code:
o Use the following buttons to control execution:
Step Over (F6): Execute the current line and move to the next.
Step Into (F5): Move into a method call.
Step Return (F7): Return from a method call.
5. Resume Execution:
o Click the Resume button (F8) to continue execution until the next breakpoint or
the end of the program.
Conclusion: In this lab, you practiced basic Java syntax, learned to identify and fix common
errors, and used Eclipse debugging tools to trace and resolve issues in your code. These skills
are essential for writing efficient and error-free programs.
"In the world of Java, every line of code is the beginning of a masterpiece. Just like a 'main
method' is the entry point of a program, your first year is the entry point to endless possibilities.
Compile your dreams, debug your doubts, and run toward success. Remember, even the most
complex algorithms start with a single line of code. Keep coding, keep growing!"