APPWEEK05
APPWEEK05
1
package Java;
abstract class Animal {
abstract void makeSound();
void eat() {
System.out.println("Dog is eating");
}
}
void eat() {
System.out.println("Cat is eating");
}
}
void eat() {
System.out.println("Bird is eating");
}
}
OUTPUT :-
Q.2
package Java;
Circle(double radius) {
this.radius = radius;
}
double calculateArea() {
return Math.PI * radius * radius;
}
double calculatePerimeter() {
return 2 * Math.PI * radius;
}
}
class Rectangle extends Shape {
double length, width;
double calculateArea() {
return length * width;
}
double calculatePerimeter() {
return 2 * (length + width);
}
}
double calculateArea() {
double s = (a + b + c) / 2;
return Math.sqrt(s * (s - a) * (s - b) * (s - c));
}
double calculatePerimeter() {
return a + b + c;
}
}
OUTPUT -:
Q.3
package Java;
double calculateSalary() {
return basicSalary + (0.2 * basicSalary); // Assuming 20% bonus
}
String getEmployeeDetails() {
return "Regular Employee: " + name + ", ID: " + id;
}
}
double calculateSalary() {
return monthlyRate * monthsWorked;
}
String getEmployeeDetails() {
return "Contract Employee: " + name + ", ID: " + id;
}
}
double calculateSalary() {
return hourlyRate * hoursWorked;
}
String getEmployeeDetails() {
return "Hourly Employee: " + name + ", ID: " + id;
}
}
II.Polymorphism:
Q.1
OUTPUT-:
Q.3
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 Division Calculator that inherit from
Calculator and override the calculate () method to perform their respective arithmetic
operations.
III.Inheritance
Q.1.
package Java;
class Animal {
String name;
String sound;
int num_legs;
cat.makeSound();
cat.walk();
bird.makeSound();
bird.walk();
bird.fly();
}
}
OUTPUT;-
IV. Encapsulation
Q.1.
package Java;
class BankAccount {
private String accountNumber;
private double balance;
private static final double MINIMUM_BALANCE = 0.0;
account.deposit(200.0);
System.out.println("Updated Balance: " + account.getBalance());
account.withdraw(100.0);
System.out.println("Updated Balance: " + account.getBalance());