Java Programming Labs

Master Object-Oriented Programming with classes, inheritance, and ArrayList collections.

Object-Oriented Programming & Collections - Module 3

Build real-world applications using OOP principles and dynamic data structures.

Lab 7: Classes & Objects
Advanced
Coding Challenge
Your Task: Create a Person class that encapsulates personal data with private fields, a constructor, getters/setters, and a display method.

Detailed Requirements:
1. Create a class named "Person" with private fields:
   � private String name;
   � private int age;

2. Create a constructor that takes name and age parameters and initializes the fields.
   Syntax: public Person(String name, int age) { this.name = name; this.age = age; }

3. Create getter and setter methods for each field:
   � getName(), setName(String name)
   � getAge(), setAge(int age)

4. Create a displayInfo() method that prints the person's details.

5. In main(), create Person objects and call their methods.

OOP Concepts:
Encapsulation: Private fields protect data; access via methods
Constructor: Special method to initialize objects
this keyword: Refers to the current object instance

Expected Output:
Person: Alice, Age: 25 Person: Bob, Age: 30 After birthday: Bob, Age: 31

Requirements Checklist

Create Person class with private fields (name, age)
Create constructor with parameters
Implement getter methods (getName, getAge)
Implement setter methods (setName, setAge)
Create displayInfo() method
Create objects and call methods in main()
Output
// Click "Run Code" to compile and execute
Hints & Tips
� Private field: private String name;
� Constructor: public Person(String name, int age) { this.name = name; }
� Getter: public String getName() { return name; }
� Setter: public void setAge(int age) { this.age = age; }
Progress: 0/6
Score: 0/100
0%

Lab Results

Review feedback below

Lab 8: Inheritance & Polymorphism
Advanced
Coding Challenge
Your Task: Create an inheritance hierarchy with a base Animal class and derived Dog and Cat classes that override methods.

Detailed Requirements:
1. Create base class "Animal" with:
   � protected String name; (accessible by subclasses)
   � Constructor taking name parameter
   � makeSound() method printing generic sound

2. Create class "Dog" extending Animal:
   � Call parent constructor using super(name)
   � @Override makeSound() to print "Woof!"

3. Create class "Cat" extending Animal:
   � Call parent constructor using super(name)
   � @Override makeSound() to print "Meow!"

4. Demonstrate polymorphism by storing Dog/Cat in Animal variables.

Inheritance Concepts:
extends - inherits from parent class
super() - calls parent constructor
@Override - indicates method overriding
protected - accessible in subclasses

Expected Output:
Buddy says: Woof! Whiskers says: Meow! -- Polymorphism Demo -- Woof! Meow!

Requirements Checklist

Create Animal base class with name and makeSound()
Create Dog class extending Animal
Create Cat class extending Animal
Use super() to call parent constructor
Override makeSound() in both subclasses
Demonstrate polymorphism with Animal references
Output
// Click "Run Code" to compile and execute
Hints & Tips
� Extend class: class Dog extends Animal { }
� Call parent constructor: super(name);
� Override method: @Override public void makeSound() { }
� Polymorphism: Animal a = new Dog("Rex");
Progress: 0/6
Score: 0/100
0%

Lab Results

Review feedback below

Lab 9: ArrayList Collections
Advanced
Coding Challenge
Your Task: Use ArrayList to create a dynamic list of students, demonstrating add, remove, iterate, and search operations.

Detailed Requirements:
1. Import ArrayList: import java.util.ArrayList;

2. Create an ArrayList of Strings for student names:
   ArrayList<String> students = new ArrayList<>();

3. Add students using add() method - add at least 4 names.

4. Print all students using enhanced for loop:
   for (String student : students) { ... }

5. Remove a student using remove() method.

6. Check if student exists using contains() method.

7. Get ArrayList size using size() method.

ArrayList vs Array:
� ArrayList grows/shrinks dynamically (arrays are fixed size)
� ArrayList has built-in methods (add, remove, contains, etc.)
� ArrayList only holds objects (use Integer, not int)

Expected Output:
All students: - Alice - Bob - Charlie - Diana Total: 4 students After removing Bob: - Alice - Charlie - Diana Contains Alice? true Contains Bob? false

Requirements Checklist

Import java.util.ArrayList
Create ArrayList<String> for students
Add at least 4 students using add()
Iterate using enhanced for loop
Remove a student using remove()
Check existence using contains()
Output
// Click "Run Code" to compile and execute
Hints & Tips
� Import: import java.util.ArrayList;
� Create: ArrayList<String> list = new ArrayList<>();
� Add: list.add("item");
� Enhanced for: for (String s : list) { }
� Size: list.size() - Contains: list.contains("x")
Progress: 0/6
Score: 0/100
0%

Lab Results

Review feedback below