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

Week 5

Uploaded by

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

Week 5

Uploaded by

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

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.

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 Food");
}
}
class Cat extends Animal{
void makesound(){
System.out.println("MEOW!");
}
void eat(){
System.out.println("Cat Food");
}
}
class Bird extends Animal{
void makesound(){
System.out.println("CHIRP!");
}
void eat(){
System.out.println("Bird Seeds");
}
}
public class main{
public static void main(String []args){
Animal myDog = new Dog();
Animal myCat = new Cat();
Animal myBird = new Bird();
myDog.makesound();
myDog.eat();
myCat.makesound();
myCat.eat();
myBird.makesound();
myBird.eat();
}
}
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.

abstract class Animal {

abstract void makeSound();

class Mammal extends Animal {

void makeSound() {

System.out.println("Some generic 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();

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.

public class Calculator {

public int calculate(int a, int b) {

return a + b;

public double calculate(double a, double b) {

return a + b;

}
public int calculate(int a, int b, char operator) {

switch (operator) {

case '+': return a + b;

case '-': return a - b;

case '*': return a * b;

case '/':

if (b != 0) return a / b;

else throw new ArithmeticException("Division by zero");

default: throw new IllegalArgumentException("Invalid operator");

public static void main(String[] args) {

Calculator calc = new Calculator();

int sumInt = calc.calculate(5, 3);

System.out.println("Sum of integers: " + sumInt);

double sumDouble = calc.calculate(5.5, 3.2);

System.out.println("Sum of doubles: " + sumDouble);

int resultAdd = calc.calculate(10, 5, '+');

System.out.println("Addition result: " + resultAdd);

int resultSubtract = calc.calculate(10, 5, '-');

System.out.println("Subtraction result: " + resultSubtract);

int resultMultiply = calc.calculate(10, 5, '*');

System.out.println("Multiplication result: " + resultMultiply);

int resultDivide = calc.calculate(10, 5, '/');

System.out.println("Division result: " + resultDivide);

}
III.
Inheritance:

Qn
1:
Base Class: Animal ➢ Attributes: name, sound, num_legs
➢ Methods: make_sound(),
walk() Derived Classes: ➢ Dog: barks ➢ Cat: meows ➢ Bird: chirps, flies

Questions:
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 provide specific implementations.
4. Create objects of each derived class and demonstrate their behavior (e.g.,
call make_sound()
and walk() methods).

class Animal {

protected String name;

protected String sound;

protected int numLegs;

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

this.name = name;

this.sound = sound;

this.numLegs = numLegs;

public void makeSound() {


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

public void walk() {

System.out.println(name + " is walking on " + numLegs + " legs.");

class Dog extends Animal {

public Dog() {

super("Dog", "bark", 4);

public void makeSound() {

System.out.println(name + " barks.");

public void walk() {

System.out.println(name + " trots on " + numLegs + " legs.");

class Cat extends Animal {

public Cat() {

super("Cat", "meow", 4);

public void makeSound() {

System.out.println(name + " meows.");

public void walk() {

System.out.println(name + " prowls on " + numLegs + " legs.");

class Bird extends Animal {

public Bird() {
super("Bird", "chirp", 2);

public void makeSound() {

System.out.println(name + " chirps.");

public void walk() {

System.out.println(name + " hops on " + numLegs + " legs and flies.");

public class Main {

public static void main(String[] args) {

Animal dog = new Dog();

Animal cat = new Cat();

Animal bird = new Bird();

dog.makeSound();

dog.walk();

cat.makeSound();

cat.walk();

bird.makeSound();

bird.walk();

}
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

You might also like