0% found this document useful (0 votes)
10 views

APPWEEK05

app

Uploaded by

clickandblush
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

APPWEEK05

app

Uploaded by

clickandblush
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

//Q.

1
package Java;
abstract class Animal {
abstract void makeSound();

abstract void eat();


}

class Dog extends Animal {


void makeSound() {
System.out.println("Woof");
}

void eat() {
System.out.println("Dog is eating");
}
}

class Cat extends Animal {


void makeSound() {
System.out.println("Meow");
}

void eat() {
System.out.println("Cat is eating");
}
}

class Bird extends Animal {


void makeSound() {
System.out.println("Chirp");
}

void eat() {
System.out.println("Bird is eating");
}
}

public class Main {


public static void main(String[] args) {
Animal dog = new Dog();
dog.makeSound();
dog.eat();

Animal cat = new Cat();


cat.makeSound();
cat.eat();

Animal bird = new Bird();


bird.makeSound();
bird.eat();
}
}

OUTPUT :-

Q.2

package Java;

abstract class Shape {


abstract double calculateArea();
abstract double calculatePerimeter();
}

class Circle extends Shape {


double radius;

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;

Rectangle(double length, double width) {


this.length = length;
this.width = width;
}

double calculateArea() {
return length * width;
}

double calculatePerimeter() {
return 2 * (length + width);
}
}

class Triangle extends Shape {


double a, b, c;

Triangle(double a, double b, double c) {


this.a = a;
this.b = b;
this.c = c;
}

double calculateArea() {
double s = (a + b + c) / 2;
return Math.sqrt(s * (s - a) * (s - b) * (s - c));
}

double calculatePerimeter() {
return a + b + c;
}
}

public class Test {


public static void main(String[] args) {
Shape circle = new Circle(5);
System.out.println("Circle Area: " + circle.calculateArea());
System.out.println("Circle Perimeter: " + circle.calculatePerimeter());

Shape rectangle = new Rectangle(4, 6);


System.out.println("Rectangle Area: " + rectangle.calculateArea());
System.out.println("Rectangle Perimeter: " + rectangle.calculatePerimeter());

Shape triangle = new Triangle(3, 4, 5);


System.out.println("Triangle Area: " + triangle.calculateArea());
System.out.println("Triangle Perimeter: " + triangle.calculatePerimeter());
}
}

OUTPUT -:

Q.3

package Java;

abstract class Employee {


String name;
int id;

Employee(String name, int id) {


this.name = name;
this.id = id;
}

abstract double calculateSalary();


abstract String getEmployeeDetails();
}

class RegularEmployee extends Employee {


double basicSalary;

RegularEmployee(String name, int id, double basicSalary) {


super(name, id);
this.basicSalary = basicSalary;
}

double calculateSalary() {
return basicSalary + (0.2 * basicSalary); // Assuming 20% bonus
}
String getEmployeeDetails() {
return "Regular Employee: " + name + ", ID: " + id;
}
}

class ContractEmployee extends Employee {


double monthlyRate;
int monthsWorked;

ContractEmployee(String name, int id, double monthlyRate, int monthsWorked) {


super(name, id);
this.monthlyRate = monthlyRate;
this.monthsWorked = monthsWorked;
}

double calculateSalary() {
return monthlyRate * monthsWorked;
}

String getEmployeeDetails() {
return "Contract Employee: " + name + ", ID: " + id;
}
}

class HourlyEmployee extends Employee {


double hourlyRate;
int hoursWorked;

HourlyEmployee(String name, int id, double hourlyRate, int hoursWorked) {


super(name, id);
this.hourlyRate = hourlyRate;
this.hoursWorked = hoursWorked;
}

double calculateSalary() {
return hourlyRate * hoursWorked;
}

String getEmployeeDetails() {
return "Hourly Employee: " + name + ", ID: " + id;
}
}

public class Emp {


public static void main(String[] args) {
Employee regular = new RegularEmployee("Alice", 101, 50000);
System.out.println(regular.getEmployeeDetails());
System.out.println("Salary: " + regular.calculateSalary());

Employee contract = new ContractEmployee("Bob", 102, 3000, 6);


System.out.println(contract.getEmployeeDetails());
System.out.println("Salary: " + contract.calculateSalary());

Employee hourly = new HourlyEmployee("Charlie", 103, 20, 120);


System.out.println(hourly.getEmployeeDetails());
System.out.println("Salary: " + hourly.calculateSalary());
}
}

II.Polymorphism:
Q.1

abstract class Animal {


abstract void makeSound();
}

class Mammal extends Animal {


void makeSound() {
System.out.println("Mammal sound");
}
}

class Bird extends Animal {


void makeSound() {
System.out.println("Chirp");
}
}

class Fish extends Animal {


void makeSound() {
System.out.println("Blub");
}
}

public class Main {


public static void main(String[] args) {
Animal[] animals = new Animal[3];
animals[0] = new Mammal();
animals[1] = new Bird();
animals[2] = new Fish();

for (Animal animal : animals) {


animal.makeSound();
}
}
}

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;

public Animal(String name, String sound, int num_legs) {


this.name = name;
this.sound = sound;
this.num_legs = num_legs;
}

public void makeSound() {


System.out.println(name + " makes a " + sound + " sound.");
}

public void walk() {


System.out.println(name + " walks on " + num_legs + " legs.");
}
}

class Dog extends Animal {


public Dog(String name) {
super(name, "bark", 4);
}
}

class Cat extends Animal {


public Cat(String name) {
super(name, "meow", 4);
}
}

class Bird extends Animal {


public Bird(String name) {
super(name, "chirp", 2);
}

public void fly() {


System.out.println(name + " flies.");
}
}

public class Main {


public static void main(String[] args) {
Dog dog = new Dog("Buddy");
Cat cat = new Cat("Whiskers");
Bird bird = new Bird("Tweety");
dog.makeSound();
dog.walk();

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;

BankAccount(String accountNumber, double initialBalance) {


this.accountNumber = accountNumber;
if (initialBalance >= MINIMUM_BALANCE) {
this.balance = initialBalance;
} else {
this.balance = MINIMUM_BALANCE;
}
}
public String getAccountNumber() {
return accountNumber;
}

public void setAccountNumber(String accountNumber) {


this.accountNumber = accountNumber;
}

public double getBalance() {


return balance;
}

public void deposit(double amount) {


if (amount > 0) {
balance += amount;
System.out.println("Deposited: " + amount);
} else {
System.out.println("Deposit amount must be positive.");
}
}

public void withdraw(double amount) {


if (amount > 0 && balance - amount >= MINIMUM_BALANCE) {
balance -= amount;
System.out.println("Withdrew: " + amount);
} else if (amount > balance) {
System.out.println("Insufficient balance.");
} else {
System.out.println("Withdrawal amount must be positive.");
}
}
}

public class Main {


public static void main(String[] args) {
BankAccount account = new BankAccount("123456789", 500.0);

System.out.println("Account Number: " + account.getAccountNumber());


System.out.println("Initial Balance: " + account.getBalance());

account.deposit(200.0);
System.out.println("Updated Balance: " + account.getBalance());

account.withdraw(100.0);
System.out.println("Updated Balance: " + account.getBalance());

account.withdraw(700.0); // Attempting to withdraw more than the balance


}
}
OUTPUT:-

You might also like