Java Assignment 2

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Java Assignment-2

### Inheritance:

1. **Simple Inheritance:**
Create a base class "Person" with attributes like name and age. Derive two classes
"Student" and "Teacher" from the base class. Demonstrate inheritance by accessing the
attributes of the base class in the derived classes.

2. **Multilevel Inheritance:**
Design a class hierarchy for vehicles. Create a base class "Vehicle," a derived class "Car"
from "Vehicle," and another class "SportsCar" from "Car." Demonstrate multilevel
inheritance by accessing properties from different levels.

### Abstraction:

3. **Abstract Class:**
Define an abstract class "Shape" with abstract methods for calculating area and perimeter.
Implement this class in concrete classes like "Circle" and "Rectangle." Demonstrate
abstraction by calling the abstract methods.

4. **Abstract Methods and Properties:**


Create an abstract class "BankAccount" with abstract methods for deposit and withdraw.
Implement these methods in derived classes "SavingsAccount" and "CurrentAccount."
Additionally, include an abstract property for balance.

### Encapsulation:

5. **Private Members:**
Design a class "Employee" with private attributes like salary and department. Implement
getter and setter methods to access and modify these private attributes. Demonstrate
encapsulation by restricting direct access to private members.

6. **Encapsulation in Constructors:**
Create a class "Person" with private attributes for name and age. Use a constructor to
initialize these attributes. Demonstrate encapsulation by ensuring that the attributes can only
be set during object creation.
### Polymorphism:

7. **Method Overloading:**
Develop a class "Calculator" with methods for addition, subtraction, and multiplication.
Implement method overloading to allow these methods to accept different numbers of
arguments.

8. **Operator Overloading:**
Create a class "Vector" that represents a 3D vector. Implement operator overloading for
addition and subtraction. Demonstrate how to add and subtract vectors.

### Exception Handling:

9. **Handling Divide by Zero:**


Write a program that takes two numbers as input and performs division. Implement
exception handling to handle the case where the user attempts to divide by zero.

10. **Custom Exception:**


Create a custom exception class "InvalidInputException." Use this class to handle
situations where the user enters invalid data (e.g., non-numeric values).

### Constructors:

11. **Parameterized Constructors:**


Design a class "Book" with attributes like title, author, and publication year. Implement a
parameterized constructor to initialize these attributes. Demonstrate object creation using this
constructor.

12. **Default Constructors:**


Create a class "Person" with attributes like name and age. Implement a default constructor
that initializes these attributes with default values. Demonstrate object creation without
explicitly setting attribute values.

### Interfaces:

13. **Basic Interface:**


Define an interface "Drawable" with a method "draw." Implement this interface in classes
like "Circle" and "Square." Demonstrate how objects of these classes can be treated
uniformly through the interface.

14. **Multiple Interfaces:**


Create two interfaces "Flyable" and "Swimmable." Implement these interfaces in classes
like "Bird" and "Fish." Demonstrate multiple interfaces by having a class that implements
both "Flyable" and "Swimmable."

### Multiple Inheritance Using Interface:

15. **Interface Inheritance:**


Design an interface "PersonInfo" with methods for getting name and age. Create another
interface "EmployeeInfo" with methods for getting employee ID and salary. Implement a
class "Employee" that inherits from both interfaces.

16. **Diamond Problem Solution:**


Illustrate how interfaces can be used to resolve the diamond problem in multiple
inheritance. Create interfaces "A," "B," and "C," and a class "D" that implements both "B"
and "C." Show how conflicts are resolved.

You might also like