Laboratory Activity: Basic OOP Principles
CCPRGG2L – Intermediate Programming
Title: Exploring Object-Oriented Programming Principles in Java
Objective:
By the end of this activity, students will:
1. Understand and implement encapsulation by creating private variables with getter and setter methods.
2. Use inheritance to establish a parent-child class relationship.
3. Apply abstraction to define abstract classes and methods.
4. Demonstrate polymorphism by overriding methods and using dynamic method dispatch.
General Instructions:
• Submit all java files.
• Screenshot all test run for each task and consolidate in just 1 PDF.
Task 1: Encapsulation
1. Create a class named Person with the following private attributes:
o name (String)
o age (int)
o address (String)
2. Implement public getter and setter methods for each attribute. Ensure that the age cannot be set to a negative value.
3. Let user enter name, age and address.
Sample run:
Task 2: Inheritance
1. Create a subclass Student that extends the Person class.
2. Add the following private attribute to Student:
o studentID (String)
3. Add a constructor to initialize all attributes, including the inherited ones.
4. Add a method displayDetails() that prints all the student’s information (name, age, address, and student ID).
Sample run:
Task 3: Abstraction
1. Create an abstract class Shape with the following:
o An abstract method calculateArea()
o A non-abstract method displayShape() that prints the name of the shape.
2. Create two concrete subclasses, Circle and Rectangle, that inherit from Shape.
3. Implement the calculateArea() method in both subclasses.
o Circle: Use radius to calculate the area (Math.PI * radius * radius).
o Rectangle: Use length and width to calculate the area (length * width).
4. Create a test class to instantiate objects of Circle and Rectangle, calculate their areas, and display the results.
Note:
• Let user enter the value for radius, length and width
• format the area of circle in 2 decimal places