Week 5
Week 5
Programming Paradigm
1. Abstraction
Qn 1: Create an abstract class Animal with abstract methods make Sound() and
eat().Create subclasses Dog, Cat, and Bird that inherit from Animal and
implement the abstract
methods accordingly.
Output:
Output:
@Override
double calculateSalary() { return
basicSalary + bonus;
}
@Override
String getEmployeeDetails() {
return "Regular Employee: " + name + ", ID: " + id + ", Salary: " +calculateSalary();
}
}
@Override
double calculateSalary() {return
contractAmount;
}
@Override
String getEmployeeDetails() {
return "Contract Employee: " + name + ", ID: " + id + ", ContractAmount: " +
calculateSalary();
}
}
@Override
double calculateSalary() {
return hourlyRate * hoursWorked;
}
@Override
String getEmployeeDetails() {
return "Hourly Employee: " + name + ", ID: " + id + ", Hours Worked: "
+ hoursWorked + ", Salary: " + calculateSalary();
}
}
System.out.println(regEmp.getEmployeeDetails());
System.out.println(contEmp.getEmployeeDetails());
System.out.println(hourEmp.getEmployeeDetails());
}
}
Output:
II. Polymorphism
Qn 1: Create a hierarchy of animals with classes like Animal, Mammal, Bird,
and Fish. Implement polymorphism to demonstrate different animal sounds.
Hint:
Create an abstract class Animal with an abstract method makeSound().
Create subclasses Mammal, Bird, and Fish, inheriting from Animal and
overriding the makeSound() method.
Create an array of Animal objects to store different types of animals.
Iterate through the array and call the makeSound() method for each animal,
demonstrating polymorphism.
@Override
void makeSound() {
System.out.println(name + " says: Tweet!");
}
}
@Override
void makeSound() {
System.out.println(name + " says: Blub!");
}
}
Qn 2: Overload
Create a Calculator class with multiple calculate () methods that take different
combinations of arguments (e.g., calculate (int, int), calculate (double,
double), calculate (int, int, char)). Implement these methods to perform
different arithmeticoperations based on the arguments.
Output:
Qn 3: Override
Create a base class Calculator with a calculate () method that takes two double
arguments.
Implement this method to perform addition. Create subclasses SubtractionCalculator,
MultiplicationCalculator, and DivisionCalculator that inherit from Calculator
andoverride
the calculate () method to perform their respective arithmetic operations.
class Calculator {
@Override
public double calculate(double a, double b) {return a -
b;
}
}
@Override
public double calculate(double a, double b) {return a *
b;
}
}
@Override
public double calculate(double a, double b) {if (b != 0) {
return a / b;
} else {
throw new ArithmeticException("Division by zero is not allowed.");
}
}
}
Output:
III. Inheritance
Qn 1: Base Class: Animal
➢ Methods: make_sound(),
walk() Derived Classes:
➢ Dog: barks
➢ Cat: meows
➢ Bird: chirps,
fliesQuestions:
1. Create a base class Animal with the specified attributes and methods.
2. Create derived classes Dog, Cat, and Bird that inherit from Animal.
3. Override the make_sound() and walk() methods in each derived class to
providespecific
implementations.
4. Create objects of each derived class and demonstrate their behavior
(e.g., call make_sound() and walk() methods).
// Subclass Dog
class Dog extends Animal {
// Constructor
public Dog(String name) { super(name,
"barks", 4);
}
// Subclass Cat
class Cat extends Animal {
// Constructor
public Cat(String name) { super(name,
"meows", 4);
}
// Override make_sound() method
@Override
public void make_sound() {
System.out.println(name + " meows: Meow Meow!");
}
// Subclass Bird
class Bird extends Animal {
// Constructor
public Bird(String name) { super(name,
"chirps", 2);
}
// Demonstrate behavior
dog.make_sound();
dog.walk();
cat.make_sound();
cat.walk();
bird.make_sound();
bird.walk();
}
}
Output:
Qn 2: Consider a vehicle hierarchy with a base class Vehicle that has attributes
like make,
model, year, and num_wheels. We want to create derived classes Car, Truck,
andMotorcycle
that inherit from Vehicle and have additional specific attributes and methods.
Additionally, we
want to introduce another base class Engine with attributes like type,
horsepower, and
fuel_type, and have Car, Truck, and Motorcycle inherit from both Vehicle and
Engine.Implement using multiple inheritance.
class Vehicle {
String make;
String model;
int year;
int num_wheels;
public Car(String make, String model, int year, int num_wheels, StringengineType, int
horsepower, String fuelType, int numDoors) {
super(make, model, year, num_wheels);
this.engineType = engineType;
this.horsepower = horsepower; this.fuelType
= fuelType; this.numDoors = numDoors;
}
@Override
public String getType() {return
engineType;
}
@Override
public int getHorsepower() {return
horsepower;
}
@Override
public String getFuelType() {return
fuelType;
}
public void displayCarInfo() {
displayVehicleInfo();
System.out.println("Engine Type: " + getType());
System.out.println("Horsepower: " + getHorsepower());
System.out.println("Fuel Type: " + getFuelType());
System.out.println("Number of Doors: " + numDoors);
}
}
public Truck(String make, String model, int year, int num_wheels, StringengineType, int
horsepower, String fuelType, int loadCapacity) {
super(make, model, year, num_wheels);
this.engineType = engineType;
this.horsepower = horsepower; this.fuelType
= fuelType; this.loadCapacity = loadCapacity;
}
@Override
public String getType() {return
engineType;
}
@Override
public int getHorsepower() {return
horsepower;
}
@Override
public String getFuelType() {return
fuelType;
}
@Override
public String getType() {return
engineType;
}
@Override
public int getHorsepower() {return
horsepower;
}
@Override
public String getFuelType() {return
fuelType;
}
System.out.println("Car Details:");
car.displayCarInfo();
System.out.println("\nTruck Details:");
truck.displayTruckInfo();
System.out.println("\nMotorcycle Details:");
motorcycle.displayMotorcycleInfo();
}
}
Output:
IV. Encapsulation
Qn 1: A bank wants to create a class to represent bank accounts. Each
accountshould have a
unique account number, an initial balance, and methods to deposit and
withdrawfunds. The
bank wants to ensure that the account balance is always positive and that
withdrawals do not
exceed the balance.
➢ Implement public getter and setter methods for the accountNumber attribute.
class BankAccount {
// Private attributes
private String accountNumber;
private double balance;
private static final double MIN_BALANCE = 0.0; // Minimum balance allowed
// Constructor
public BankAccount(String accountNumber, double initialBalance) {
this.accountNumber = accountNumber;
if (initialBalance >= MIN_BALANCE) {
this.balance = initialBalance;
} else {
throw new IllegalArgumentException("Initial balance cannot be
negative.");
}
}
// Deposit funds
account.deposit(150.00);
// Withdraw funds
account.withdraw(200.00);
// Attempt to withdraw more funds than available
account.withdraw(600.00);
Output:
class Rectangle {
// Private instance variables
private double length;
private double width;
// Default constructor
public Rectangle() {
this.length = 0.0;
this.width = 0.0;
}
Output:
Rohit N Rajput
RA2311033010119
AK-2