Lab Manual: Chapter 1 - Introduction to Object-Oriented Programming
(OOP)
Course Information
•
•
•
•
•
•
Course Title: Object Oriented Programming (SEng6022)
Chapter: 1 - Introduction to OOP
Weeks: 1–2
Lab Duration: 3 hours per session (two sessions recommended for Chapter 1)
Prerequisites: Basic structured programming knowledge (from Fundamentals of Programming II)
Instructor: Belay A.
Lab Objectives
By the end of this lab, students will be able to:
•
•
•
•
•
Set up a Java development environment and understand the role of the JVM and bytecode.
Create and compile simple Java classes and objects.
Apply basic OOP concepts: classes, objects, members, visibility, encapsulation, inheritance, and
polymorphism.
Debug and test Java programs using an IDE.
Demonstrate platform independence through bytecode exploration.
Required Tools and Setup
•
•
•
•
Hardware: Computer with at least 4GB RAM.
Software:
o Java Development Kit (JDK) version 21 or latest LTS (download from oracle.com or
adoptium.net).
o Integrated Development Environment (IDE): IntelliJ IDEA Community Edition (preferred)
or Eclipse.
o Terminal/Command Prompt for manual compilation (optional but recommended).
Preparation:
o Install JDK and verify with java -version in terminal.
o Install IDE and create a new Java project named OOP_Chapter1_Lab.
o Ensure you can run a simple "Hello World" program.
Submission Guidelines:
o Submit all .java files, screenshots of outputs, and a lab report (PDF/Word) via [e.g.,
Moodle or email].
o Lab report should include: Code snippets, explanations, outputs, and answers to
reflection questions.
o Due: End of Week 2.
Lab Structure
This lab is divided into two sessions:
•
•
Session 1 (Week 1): Focus on setup, basic classes/objects, and JVM/bytecode exploration.
Session 2 (Week 2): Focus on members, visibility, encapsulation, and intro to
inheritance/polymorphism. Each exercise includes step-by-step instructions, expected
outcomes, and reflection questions.
Session 1: Setup, Basics, and JVM Exploration (1.5–2 hours)
Exercise 1: Environment Setup and First Java Program (30 minutes)
Objective: Install tools and run a basic program to verify setup.
Step-by-Step Instructions:
1. Download and install JDK if not already done. Open terminal and run java -version to confirm
(expected: version 21+).
2. Download and install IntelliJ IDEA. Launch it and create a new project:
o Select "Java" as the project type.
o Name it OOP_Chapter1_Lab.
o Set JDK to the installed version.
3. In the project, create a new Java class file named HelloOOP.java.
4. Write the following code:
Java
public class HelloOOP {
public static void main(String[] args) {
System.out.println("Hello, OOP World!");
System.out.println("JVM Version: " +
System.getProperty("java.version"));
}
}
5. Run the program (use IDE's run button or terminal: javac HelloOOP.java then java HelloOOP).
6. Take a screenshot of the output.
Expected Output:
text
Hello, OOP World!
JVM Version: 21.0.1 (or similar)
Reflection Questions:
1. What happens if you remove public static void main? Explain the error.
2. Why does Java require a main method?
Grading: 10% (Setup complete and program runs).
Exercise 2: Exploring JVM and Bytecode (45 minutes)
Objective: Understand compilation to bytecode and JVM execution.
Step-by-Step Instructions:
1.
2.
3.
4.
5.
6.
Use the HelloOOP.java from Exercise 1.
Compile manually in terminal: Navigate to project src folder, run javac HelloOOP.java.
Observe the generated HelloOOP.class file (bytecode).
Disassemble bytecode: Run javap -c HelloOOP (or -verbose for more details).
Copy 5–10 lines of the output into your lab report.
Modify the code to print more JVM info:
Java
System.out.println("JVM Vendor: " + System.getProperty("java.vendor"));
System.out.println("OS: " + System.getProperty("os.name"));
Recompile and run.
7. (Optional Advanced): Run with JVM argument java -Xmx128m HelloOOP and note any changes
(heap size limit).
Expected Output (Bytecode Sample):
text
Compiled from "HelloOOP.java"
public class HelloOOP {
public HelloOOP();
Code:
0: aload_0
1: invokespecial #1
// Method
java/lang/Object."<init>":()V
4: return
public static void main(java.lang.String[]);
Code:
0: getstatic
#7
// Field
java/lang/System.out:Ljava/io/PrintStream;
3: ldc
#13
5: invokevirtual #15
// String Hello, OOP World!
// Method
java/io/PrintStream.println:(Ljava/lang/String;)V
...
}
Reflection Questions:
1. What is bytecode, and why is it platform-independent?
2. How does the JVM contribute to Java's "Write Once, Run Anywhere" principle?
Grading: 20% (Bytecode analysis and explanations).
Exercise 3: Basic Class and Object Creation (45 minutes)
Objective: Create classes, objects, and simple members.
Step-by-Step Instructions:
1.
2.
3.
4.
Create a new class Person.java.
Add private fields: private String name; private int age;.
Add a constructor: public Person(String name, int age) { this.name = name; this.age = age; }.
Add methods: public String getName() { return name; }, public int getAge() { return age; }, public
void introduce() { System.out.println("Hi, I'm " + name + ", " + age + " years old."); }.
5. In a new Main.java class:
Java
public class Main {
public static void main(String[] args) {
Person p1 = new Person("Alice", 20);
Person p2 = new Person("Bob", 22);
p1.introduce();
p2.introduce();
}
}
6. Run and debug if needed (e.g., fix any compilation errors).
7. Take screenshots of code and output.
Expected Output:
text
Hi, I'm Alice, 20 years old.
Hi, I'm Bob, 22 years old.
Reflection Questions:
1. What is the difference between a class and an object?
2. Why use constructors?
Grading: 20% (Correct implementation and output).
Session 2: OOP Core Concepts in Practice (1.5–2 hours)
Exercise 4: Encapsulation and Visibility (45 minutes)
Objective: Implement access modifiers and encapsulation.
Step-by-Step Instructions:
1. Create BankAccount.java.
2. Add private fields: private String accountNumber; private double balance;.
3. Constructor: public BankAccount(String accountNumber) { this.accountNumber =
accountNumber; this.balance = 0; }.
4. Methods:
o public void deposit(double amount) { if (amount > 0) balance += amount; }
o public void withdraw(double amount) { if (amount > 0 && amount <= balance) balance = amount; else System.out.println("Insufficient funds!"); }
o public double getBalance() { return balance; }
5. In Main.java, create an account, deposit 100, withdraw 50, withdraw 60 (should fail), print
balance.
6. Attempt direct access to balance outside the class (e.g., account.balance = -100;) – note
compilation error due to private.
Expected Output:
text
Insufficient funds!
Balance: 50.0
Reflection Questions:
1. How does private enforce encapsulation?
2. Why not make all members public?
Grading: 15% (Validation and error handling).
Exercise 5: Introduction to Inheritance and Polymorphism (45 minutes)
Objective: Preview inheritance hierarchies and polymorphic behavior.
Step-by-Step Instructions:
1. Create Animal.java: private String name; public Animal(String name) { this.name = name; } public
void makeSound() { System.out.println("Some sound"); }.
2. Create Dog.java: public class Dog extends Animal { public Dog(String name) { super(name); }
@Override public void makeSound() { System.out.println("Woof!"); } }.
3. Create Cat.java: Similar to Dog, but "Meow!".
4. In Main.java:
Java
Animal a1 = new Dog("Buddy");
Animal a2 = new Cat("Whiskers");
a1.makeSound();
// Woof!
a2.makeSound();
// Meow!
5. Run and observe polymorphism (superclass reference calls subclass method).
Expected Output:
text
Woof!
Meow!
Reflection Questions:
1. What is method overriding?
2. How does polymorphism make code more flexible?
Grading: 15% (Correct hierarchy and overriding).
Exercise 6: Chapter 1 Mini-Project – Basic Contact Manager (30–45 minutes)
Objective: Integrate all concepts in a small application.
Step-by-Step Instructions:
1. Create Contact.java: Private fields (name, phone, email), constructor, getters/setters
(encapsulation).
2. Create ContactManager.java: Use an array or ArrayList<Contact>, methods: addContact(Contact
c), displayAll(), searchByName(String name) (returns Contact or null).
3. In Main.java: Create manager, add 4–5 contacts, display all, search one and print.
4. Bonus: Add inheritance – FriendContact extends Contact with extra birthday field.
5. Include comments explaining visibility choices and print JVM info at start.
6. Test thoroughly and document outputs.
Expected Output Example:
text
JVM Version: 21.0.1
All Contacts:
Name: Alice, Phone: 123-456, Email: alice@example.com
...
Search for 'Alice': Found - Phone: 123-456
Reflection Questions:
1. How did OOP concepts make this project easier than procedural code?
2. What challenges did you face with visibility or inheritance?
Grading: 20% (Functionality, integration, and report).
Overall Lab Assessment
•
•
•
•
Total Points: 100% (as distributed above).
Tips for Success: Comment your code, handle edge cases (e.g., invalid inputs), and use IDE
debugging tools.
Common Pitfalls: Forgetting super() in subclasses, direct access to private fields, compilation
without JDK.
Extensions for Advanced Students: Add file I/O preview (save contacts to text file) or explore
more JVM properties.
This completes the lab manual for Chapter 1.
Lab Assignment
•
•
Create a Bank System using all OOP principles
Submit source code + screenshots
Lab manual 2 loading……