Object-Oriented Programming Lab
Manual in Java
Lab 1: Basic Java Program - Display "Hello, World!"
Objective: Understand basic syntax and display a message in Java.
Theory
Java Program Structure: Every Java application starts with a class definition, followed by the
main method, which serves as the entry point of the program.
Example Code
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Exercises
1. Modify the program to display 'Welcome to Java Programming!'.
2. Add an additional line to display your name.
Lab 2: Classes and Objects
Objective: Learn to define a class and create an object of that class.
Theory
Class: A blueprint for creating objects that encapsulates data and behavior.
Object: An instance of a class, with its own set of attributes and methods.
Example Code
class Greeting {
// Method to display a greeting message
void sayHello() {
System.out.println("Hello from the Greeting class!");
}
}
public class Main {
public static void main(String[] args) {
// Creating an object of the Greeting class
Greeting greeting = new Greeting();
greeting.sayHello();
}
}
Exercises
1. Create a new class called Introduction with a method introduce that displays your name
and age.
2. In Main, create an object of the Introduction class and call the introduce method.
Lab 3: Selection Statements
Objective: Learn to use selection statements (if, else if, else, and switch) to control the flow
of a program based on conditions.
Theory
if: Executes a block of code if a specified condition is true.
else if: Tests another condition if the previous one is false.
else: Executes if all preceding conditions are false.
switch: Provides a way to select one of many code blocks to execute, based on a specific
value.
Example Code 1: Using if-else
import java.util.Scanner;
public class SelectionExample {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = scanner.nextInt();
if (number > 0) {
System.out.println("The number is positive.");
} else if (number < 0) {
System.out.println("The number is negative.");
} else {
System.out.println("The number is zero.");
}
scanner.close();
}
}
Example Code 2: Using switch
import java.util.Scanner;
public class DayOfWeek {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number (1-7) for the day of the week: ");
int day = scanner.nextInt();
switch (day) {
case 1:
System.out.println("Sunday");
break;
case 2:
System.out.println("Monday");
break;
case 3:
System.out.println("Tuesday");
break;
case 4:
System.out.println("Wednesday");
break;
case 5:
System.out.println("Thursday");
break;
case 6:
System.out.println("Friday");
break;
case 7:
System.out.println("Saturday");
break;
default:
System.out.println("Invalid input. Please enter a number between 1 and 7.");
break;
}
scanner.close();
}
}
Exercises
1. Modify the SelectionExample to check if the number is even or odd.
2. Update the DayOfWeek program to display weekend (Saturday, Sunday) or weekday for
each day input.