Java Object-Oriented Programming (OOP)
Programming Assignment
Course: Computer Science and Engineering | Topic: OOP in Java
Instructions: Complete the following programming tasks using the concepts from your Java
OOP notes. Ensure all code is properly commented. Submit .java files for each task along
with console output screenshots where indicated.
Part 1: Classes, Objects, and Constructors
Task 1 — The Book Class
1. Create a class named Book with three fields:
• String title
• String author
• double price
2. Implement three different ways to assign data to these variables:
• Method 1: Using Object Reference Variables directly in the main class.
• Method 2: Using a User-defined Method (e.g., setBookData).
• Method 3: Using a Parameterized Constructor.
3. Create a displayInfo() method to print all book details.
Task 2 — Constructor Overloading
1. Create a class named Smartphone.
2. Implement Constructor Overloading using three constructors:
• A Default Constructor that sets the brand to "Unknown".
• A Parameterized Constructor that accepts brand and model.
• A Parameterized Constructor that accepts brand, model, and price.
Part 2: The Four Pillars of OOP
Task 3 — Encapsulation (Data Hiding)
1. Create an Employee class.
2. Declare all variables as private: id (int), name (String), salary (double).
3. Generate Getter and Setter methods for each variable.
4. In a Main class, demonstrate setting the salary and retrieving it via the getter.
Task 4 — Inheritance and Method Overriding
1. Create a parent class Device with a method powerOn() that prints:
"Device is starting..."
2. Create a child class Laptop that extends Device.
3. Override the powerOn() method in Laptop to print:
"Laptop is booting Windows/macOS..."
4. Use the @Override annotation on the overriding method.
Task 5 — Polymorphism (Method Overloading)
1. Create a class named MathOperations.
2. Implement Method Overloading for an add() method with the following signatures:
• add(int a, int b)
• add(double a, double b)
• add(int a, int b, int c)
Part 3: Advanced Concepts
Task 6 — Interface and Multiple Inheritance
1. Define an interface Camera with an abstract method takePhoto().
2. Define another interface Phone with an abstract method makeCall().
3. Create a class SmartDevice that implements both interfaces, demonstrating multiple inheritance.
Task 7 — Exception Handling
1. Write a program that asks a user for two numbers and performs division.
2. Use a try-catch-finally block to handle a potential ArithmeticException (division by zero).
3. Ensure the finally block prints: "Transaction Processed".
Submission Note: Include a screenshot of the console output showing both normal execution and
the exception case.
Part 4: Collections Framework
Task 8 — ArrayList vs. HashSet
1. Create an ArrayList of student names. Add a duplicate name to the list and print the final size.
2. Create a HashSet of student names. Add the same duplicate name and print the final size.
3. Short Answer: Based on your output, explain in 2–3 sentences the difference between ArrayList
and HashSet regarding:
• Handling of duplicate elements
• Preservation of insertion order
Submission Note: Include a screenshot of the console output for both the ArrayList and HashSet
results.
Task 9 — HashMap Implementation
1. Create a HashMap where the Key is a Student ID (Integer) and the Value is their Grade
(Character).
2. Add 5 entries to the HashMap.
3. Use an Iterator or a for-each loop with entrySet() to print all students and their grades.
Submission Requirements
Task Deliverable Screenshot Required?
Tasks 1–6 .java source file No
Task 7 .java source file Yes — normal + exception case
Task 8 .java source file Yes — ArrayList and HashSet output
Task 9 .java source file No
Note: All code must be original and properly commented. Plagiarism will result in a zero grade. Reference your Java
OOP notes where applicable.