0% found this document useful (0 votes)
73 views11 pages

Java OOP Programs: Shapes, Persons, Banking

Uploaded by

aslamsaira437
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
73 views11 pages

Java OOP Programs: Shapes, Persons, Banking

Uploaded by

aslamsaira437
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

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) {

[Link] = radius;

double calculateArea() {
return [Link] * radius * radius

double calculatePerimeter() {

return 2 * [Link] * 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 [Link](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);


[Link]("Circle Area: " + [Link]());

[Link]("Circle Perimeter: " + [Link]());

[Link]("Triangle Area: " + [Link]());

[Link]("Triangle Perimeter: " + [Link]());

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() {

[Link]("Athlete eats a balanced diet.");

}
void exercise() {

[Link]("Athlete exercises daily.");

class LazyPerson extends Person {

void eat() {

[Link]("Lazy person eats junk food.");

void exercise() {

[Link]("Lazy person rarely exercises.");

public class PersonTest {

public static void main(String[] args) {

Person athlete = new Athlete();

Person lazy = new LazyPerson();

[Link]();

[Link]();

[Link]();

[Link]();

}
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 [Link](s * (s - a) * (s - b) * (s - c));

}
double perimeter() {

return a + b + c;

class Square extends GeometricShape {

double side;

Square(double side) {

[Link] = 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);

[Link]("Triangle Area: " + [Link]());

[Link]("Triangle Perimeter: " + [Link]());


[Link]("Square Area: " + [Link]());

[Link]("Square Perimeter: " + [Link]());

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() {

[Link]("Dog is barking");

public class AnimalTest {


public static void main(String[] args) {

Animal dog = new Dog();

[Link]();

Output:

Dog is barking

5. [Link] a java programming to create a banking system with three


classes_bankAccount,SavingsAccount,and [Link] bank should have a list of
accounts and methods for adding [Link] 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 {

[Link]("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 {

[Link]("Overdraft not allowed");


}

public void calculateInterest() {

[Link]("No interest on Current Account.");

public double getBalance() {

return balance;

class Bank {

private List<Account> accounts = new ArrayList<>();

public void addAccount(Account account) {

[Link](account);

public void showBalances() {

for (Account acc : accounts) {

[Link]("Balance: " + [Link]());

public class BankTest {

public static void main(String[] args) {


Bank bank = new Bank();

Account savings = new SavingsAccount();

Account current = new CurrentAccount();

[Link](1000);

[Link]();

[Link](2000);

[Link](500);

[Link](savings);

[Link](current);

[Link]();

Output:

Balance: 1050.0

Balance: 1500.0

Common questions

Powered by AI

The benefits of using abstract classes, such as Shape, include the ability to define a common protocol for a group of related classes and to facilitate code reuse by allowing specific implementations in multiple subclasses, such as Circle and Triangle. They also allow for partial implementations, providing default behaviors that subclasses can override. However, potential drawbacks include restricted multiple inheritance (as a class can only inherit from one abstract class), and they may lead to unnecessary complexity and rigidity if overused. Concrete classes, on the other hand, might offer simplicity and direct implementation but lack the flexibility and scalability offered by abstract class hierarchies .

Polymorphism in Java simplifies geometric modeling by allowing the use of a uniform interface to interact with objects of different shapes like Circle, Triangle, and Square, through their abstract superclass GeometricShape. This enables writing generic code for handling different shapes; for example, calculating the area and perimeter without knowing the specific type of shape at compile time. By calling methods area() and perimeter(), the appropriate implementation is dynamically chosen at runtime based on the actual object type, which leads to cleaner and easily extendable code. This is exemplified in the GeometricShapeTest class, where a TriangleShape and a Square are treated as GeometricShapes without needing to handle them separately .

The banking system example illustrates polymorphism by using the Account interface, which is implemented by different classes such as SavingsAccount and CurrentAccount. This allows for dynamic method dispatch, where the actual method implementations (deposit, withdraw, calculateInterest) are determined at runtime based on the specific Account object type. This enables generic handling of account types through the abstract interface, allowing the Bank class to manage various account types in a uniform manner. As such, the system can add new account types without altering the existing structure, enhancing extensibility and maintainability .

Encapsulation contributes to robustness by restricting access to the internal states of objects, thereby reducing system vulnerability to unauthorized access and modifications. In the Java banking system example, encapsulation is achieved by using private fields like balance in the SavingsAccount and CurrentAccount classes. Accessor methods such as deposit(), withdraw(), and calculateInterest() control how these private fields are manipulated. This ensures that critical operations, like deposits and withdrawals, are performed only through well-defined channels, protecting the state of the accounts from inconsistent modifications and enhancing the system's stability and reliability .

Abstract classes in Java, such as the Shape and Person examples, allow for code reuse by providing a base structure that can be inherited by subclasses. This mechanism enforces a contract for the subclasses, requiring them to provide concrete implementations of abstract methods like calculateArea(), calculatePerimeter(), eat(), and exercise(). This ensures that all subclasses adhere to a common interface, while also allowing them to add their unique functionality. For example, Circle and Triangle are subclasses of Shape, required to implement the calculation methods for area and perimeter, but they provide different implementations specific to their geometric properties .

Java interfaces, like the Animal interface, are different from abstract classes in that they can only declare methods without any implementation, while abstract classes can have both abstract and concrete methods. An interface defines a contract that implementing classes agree to follow, which allows for multiple inheritance of type. The Dog class implements the Animal interface, providing a specific implementation for the bark() method as 'Dog is barking'. In contrast, abstract classes like Shape or Person can provide some functionality directly, making them partially implemented classes .

Concrete methods in abstract classes allow for shared implementation details within the subclasses that extend from this base, while still allowing subclasses to provide specific implementations for other methods. In the example of the Person class, although it defines abstract methods eat() and exercise(), it could have concrete methods that define common behaviors for all types of Person (Athlete and LazyPerson), such as a method to output the type of Person or basic personal statistics. This setup allows the reuse of shared code while maintaining the subclasses' need for unique behaviors .

Primary considerations include ensuring accurate calculations of geometric properties like area and perimeter, and designing a flexible class structure that can support various types of shapes (like circles, triangles, squares) by leveraging polymorphism and inheritance effectively. For instance, using a base abstract class like GeometricShape allows for a uniform interface across different shapes, which can help handle operations polymorphically. It's crucial to manage numerical precision and potential computational pitfalls with specific calculations (such as Heron's formula in TriangleShape) to maintain robust and reliable output. Additionally, the class structure should be extendable, easily allowing the addition of new shapes without significant code restructuring .

An interface should be preferred over an abstract class when you need to define a contract for unrelated classes, or when you require multiple inheritance of behavior across different class hierarchies, as Java does not support multiple inheritance with classes. For example, the Animal interface demonstrates how diverse classes unrelated by inheritance can implement a contract like bark(), allowing polymorphic behavior irrespective of their inheritance hierarchies. Interfaces provide the advantage of defining methods without enforcing any unwanted common functionality, promoting more flexible and loosely-coupled designs .

A limitation of inheritance is that it creates a tight coupling between the base and derived classes, which can lead to decreased flexibility and increased maintenance if the base class changes. This can also lead to issues with complexity and ambiguity, particularly when using deep inheritance hierarchies. Such limitations can be mitigated by following principles such as composition over inheritance, which involves using interfaces and delegation instead of extending classes. For example, in the banking system described where SavingsAccount and CurrentAccount implement an Account interface, this design emphasizes interface implementation over class hierarchy, allowing for greater flexibility and easier maintenance .

You might also like