Week 5 Java
Week 5 Java
[Assignment Week 2]
21CSC203P-ADVANCED PROGRAMMING PRACTICE
JULY–DECEMBER 2024
[RA2311003010291]
[SAVIO BINO]
[2nd/E1]
I. 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.
CODE-
abstract class Animal {
abstract void makeSound();
abstract void eat();
}
void eat() {
System.out.println("Dog is eating");
}
}
void eat() {
System.out.println("Cat is eating");
}
}
void eat() {
System.out.println("Bird is eating");
}
}
CODE-
abstract class Shape {
abstract double calculateArea();
abstract double calculatePerimeter();
}
Circle(double radius) {
this.radius = radius;
}
double calculateArea() {
return Math.PI * radius * radius;
}
double calculatePerimeter() {
return 2 * Math.PI * radius;
}
}
double calculateArea() {
return length * width;
}
double calculatePerimeter() {
return 2 * (length + width);
}
}
double calculateArea() {
double s = (a + b + c) / 2; // semi-perimeter
return Math.sqrt(s * (s - a) * (s - b) * (s - c)); // Heron's formula
}
double calculatePerimeter() {
return a + b + c;
}
}
CODE-
abstract class Employee {
String name;
int id;
double calculateSalary() {
return basicSalary + bonus;
}
String getEmployeeDetails() {
return "Regular Employee - Name: " + name + ", ID: " + id;
}
}
double calculateSalary() {
return contractAmount;
}
String getEmployeeDetails() {
return "Contract Employee - Name: " + name + ", ID: " + id;
}
}
double calculateSalary() {
return hourlyRate * hoursWorked;
}
String getEmployeeDetails() {
return "Hourly Employee - Name: " + name + ", ID: " + id;
}
}
II.Polymorphism:
Qn 1: Create a hierarchy of animals with classes like Animal, Mammal, Bird, and Fish.
Implement polymorphism to demonstrate different animal sounds.
CODE-
class Animal {
void makeSound() {
System.out.println("Some generic animal sound");
}
}
genericAnimal.makeSound();
mammal.makeSound();
bird.makeSound();
fish.makeSound();
dog.makeSound();
cat.makeSound();
}
}
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 arithmetic operations based
on the arguments.
CODE-
class Calculator {
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 and override
the calculate () method to perform their respective arithmetic operations.
CODE-
class Calculator {
double calculate(double a, double b) {
return a + b;
}
}
III. Inheritance:
CODE-
// Base class Animal
class Animal {
String name;
String sound;
int num_legs;
void make_sound() {
System.out.println(name + " makes a sound: " + sound);
}
void walk() {
System.out.println(name + " walks on " + num_legs + " legs.");
}
}
Dog(String name) {
super(name, "Bark", 4);
}
@Override
void make_sound() {
System.out.println(name + " barks: " + sound);
}
@Override
void walk() {
System.out.println(name + " walks on " + num_legs + " legs, wagging its tail.");
}
}
Cat(String name) {
super(name, "Meow", 4);
}
@Override
void make_sound() {
System.out.println(name + " meows: " + sound);
}
@Override
void walk() {
System.out.println(name + " walks on " + num_legs + " legs gracefully.");
}
}
Bird(String name) {
super(name, "Chirp", 2);
}
@Override
void make_sound() {
System.out.println(name + " chirps: " + sound);
}
@Override
void walk() {
System.out.println(name + " hops on " + num_legs + " legs and flies.");
}
}
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, and Motorcycle
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.
CODE-
// Base class Vehicle
class Vehicle {
String make;
String model;
int year;
int num_wheels;
void displayInfo() {
System.out.println("Make: " + make + ", Model: " + model + ", Year: " + year + ", Wheels: " +
num_wheels);
}
}
// Interface Engine
interface Engine {
String type = "Unknown"; // default type
int horsepower = 0; // default horsepower
String fuel_type = "Unknown"; // default fuel type
Car(String make, String model, int year, int horsepower, String fuel_type) {
super(make, model, year, 4); // Cars have 4 wheels
this.horsepower = horsepower;
this.fuel_type = fuel_type;
}
@Override
public void engineDetails() {
System.out.println("Engine Type: Petrol, Horsepower: " + horsepower + ", Fuel Type: " + fuel_type);
}
}
Truck(String make, String model, int year, int horsepower, String fuel_type) {
super(make, model, year, 6); // Trucks have 6 wheels
this.horsepower = horsepower;
this.fuel_type = fuel_type;
}
@Override
public void engineDetails() {
System.out.println("Engine Type: Diesel, Horsepower: " + horsepower + ", Fuel Type: " + fuel_type);
}
}
Motorcycle(String make, String model, int year, int horsepower, String fuel_type) {
super(make, model, year, 2); // Motorcycles have 2 wheels
this.horsepower = horsepower;
this.fuel_type = fuel_type;
}
@Override
public void engineDetails() {
System.out.println("Engine Type: Electric, Horsepower: " + horsepower + ", Fuel Type: " + fuel_type);
}
}
car.displayInfo();
car.engineDetails();
truck.displayInfo();
truck.engineDetails();
motorcycle.displayInfo();
motorcycle.engineDetails();
}
}
IV.Encapsulation
Qn 1: A bank wants to create a class to represent bank accounts. Each account should have a
unique account number, an initial balance, and methods to deposit and withdraw funds. The
bank wants to ensure that the account balance is always positive and that withdrawals do not
exceed the balance.
➢ Create a class named BankAccount with private attributes for accountNumber, balance,
and a constant for the minimum balance allowed.
➢ Implement public getter and setter methods for the accountNumber attribute.
➢ Implement public methods deposit and withdraw that update the balance appropriately,
ensuring that the balance remains positive and that withdrawals do not exceed the
balance.
CODE-
class BankAccount {
private String accountNumber;
private double balance;
private final double MIN_BALANCE = 0.0;
public BankAccount(String accountNumber, double initialBalance) {
this.accountNumber = accountNumber;
if (initialBalance >= MIN_BALANCE) {
this.balance = initialBalance;
} else {
this.balance = MIN_BALANCE;
}
}
// Deposit funds
account.deposit(150.0);
System.out.println("Balance after deposit: " + account.getBalance());
// Withdraw funds
account.withdraw(200.0);
System.out.println("Balance after withdrawal: " + account.getBalance());
Qn 2: Write a Java program to create a class called Rectangle with private instance variables
length and width. Provide public getter and setter methods to access and modify these
variables.
CODE-
class Rectangle {
private double length;
private double width;