Assignment
Name=Saira Aslam
Class=ADP-IT
Subject=Object oriented programming
Roll no=52
Java programs
1. Write a java program to create an abstract class shape with abstract methods
calculateArea() and calculateperimeter().create subclasses Circle and Triangle that
extends the shape class and implement the respective methods to calculate the area
and perimeter of the each shape.
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 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 ShapeTest {
public static void main(String[] args) {
Shape circle = new Circle(5);
Shape triangle = new Triangle(3, 4, 5);
System.out.println("Circle Area: " + circle.calculateArea());
System.out.println("Circle Perimeter: " + circle.calculatePerimeter());
System.out.println("Triangle Area: " + triangle.calculateArea());
System.out.println("Triangle Perimeter: " + triangle.calculatePerimeter());
Output:
Circle Area: 78.53981633974483
Circle Perimeter: 31.41592653589793
Triangle Area: 6.0
Triangle Perimeter: 12.0
2. Write a java program to create an abstract class Person with abstract methods eat() and
exercise().Create subclasses Athlete and LazyPerson that extend the Person class and
implement the respective methods to describe how each person eats and exercises.
abstract class Person {
abstract void eat();
abstract void exercise();
class Athlete extends Person {
void eat() {
System.out.println("Athlete eats a balanced diet.");
}
void exercise() {
System.out.println("Athlete exercises daily.");
class LazyPerson extends Person {
void eat() {
System.out.println("Lazy person eats junk food.");
void exercise() {
System.out.println("Lazy person rarely exercises.");
public class PersonTest {
public static void main(String[] args) {
Person athlete = new Athlete();
Person lazy = new LazyPerson();
athlete.eat();
athlete.exercise();
lazy.eat();
lazy.exercise();
}
Output:
Athlete eats a balanced diet.
Athlete exercises daily.
Lazy person eats junk food.
Lazy person rarely exercises.
3. Write a java program to create an abstract class GeometricShape with abstract methods
area() and perimeter().create subclasses Triangle and square that extend the GeometricShape
class and implements respective methods to calculate the area and perimeter of each shape.
abstract class GeometricShape {
abstract double area();
abstract double perimeter();
class TriangleShape extends GeometricShape {
double a, b, c;
TriangleShape(double a, double b, double c) {
this.a = a;
this.b = b;
this.c = c;
double area() {
double s = (a + b + c) / 2;
return Math.sqrt(s * (s - a) * (s - b) * (s - c));
}
double perimeter() {
return a + b + c;
class Square extends GeometricShape {
double side;
Square(double side) {
this.side = side;
double area() {
return side * side;
double perimeter() {
return 4 * side;
public class GeometricShapeTest {
public static void main(String[] args) {
GeometricShape triangle = new TriangleShape(3, 4, 5);
GeometricShape square = new Square(6);
System.out.println("Triangle Area: " + triangle.area());
System.out.println("Triangle Perimeter: " + triangle.perimeter());
System.out.println("Square Area: " + square.area());
System.out.println("Square Perimeter: " + square.perimeter());
Output:
Triangle Area: 6.0
Triangle Perimeter: 12.0
Square Area: 36.0
Square Perimeter: 24.0
4. Write a java program to create a Animal interface with a method called bark() that takes no
arguments and returns void .Create a Dog class that implements Animal and overrides speak()
to print “Dog is barking”.
interface Animal {
void bark();
class Dog implements Animal {
@Override
public void bark() {
System.out.println("Dog is barking");
public class AnimalTest {
public static void main(String[] args) {
Animal dog = new Dog();
dog.bark();
Output:
Dog is barking
5. ds.Write a java programming to create a banking system with three
classes_bankAccount,SavingsAccount,and CurrentAccount.The bank should have a list of
accounts and methods for adding them.Accounts should be an interface with methods to
deposit,withdraw,calculate interface and have their own unique metho
interface Account {
void deposit(double amount);
void withdraw(double amount);
void calculateInterest();
double getBalance();
class SavingsAccount implements Account {
private double balance;
private double interestRate = 0.05;
public void deposit(double amount) {
balance += amount;
}
public void withdraw(double amount) {
if (balance >= amount) {
balance -= amount;
} else {
System.out.println("Insufficient balance");
public void calculateInterest() {
balance += balance * interestRate;
public double getBalance() {
return balance;
class CurrentAccount implements Account {
private double balance;
public void deposit(double amount) {
balance += amount;
public void withdraw(double amount) {
if (balance >= amount) {
balance -= amount;
} else {
System.out.println("Overdraft not allowed");
}
public void calculateInterest() {
System.out.println("No interest on Current Account.");
public double getBalance() {
return balance;
class Bank {
private List<Account> accounts = new ArrayList<>();
public void addAccount(Account account) {
accounts.add(account);
public void showBalances() {
for (Account acc : accounts) {
System.out.println("Balance: " + acc.getBalance());
public class BankTest {
public static void main(String[] args) {
Bank bank = new Bank();
Account savings = new SavingsAccount();
Account current = new CurrentAccount();
savings.deposit(1000);
savings.calculateInterest();
current.deposit(2000);
current.withdraw(500);
bank.addAccount(savings);
bank.addAccount(current);
bank.showBalances();
Output:
Balance: 1050.0
Balance: 1500.0