[go: up one dir, main page]

0% found this document useful (0 votes)
17 views7 pages

Oop

The document contains 4 programs that demonstrate object oriented programming concepts in Java. The programs cover inheritance, polymorphism, abstract classes and interfaces. Program 1 defines geometric shapes as abstract classes with triangle and square subclasses. Program 2 defines employee roles with abstract methods for salary calculation. Program 3 defines bank account types with abstract methods for transactions. Program 4 calculates areas and perimeters of shapes using abstract classes.

Uploaded by

asfasheikh257
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)
17 views7 pages

Oop

The document contains 4 programs that demonstrate object oriented programming concepts in Java. The programs cover inheritance, polymorphism, abstract classes and interfaces. Program 1 defines geometric shapes as abstract classes with triangle and square subclasses. Program 2 defines employee roles with abstract methods for salary calculation. Program 3 defines bank account types with abstract methods for transactions. Program 4 calculates areas and perimeters of shapes using abstract classes.

Uploaded by

asfasheikh257
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
You are on page 1/ 7

UNIVERSITY OF LAYYAH

OBJECT ORIENTED PROGRAMMING


ASSIGNMENT NO.2
SUBMITTED BY: ASFA SHEIKH
ROLL NO. 02
SUBMITTED TO MR. HAFIZ MUHAMMAD ARSHAD
BS IT EVENING

1. PROGRAM 1:
public abstract class GeometricShape {
abstract void area();
int length, height;

abstract void perimeter();

public static void main(String[] args) {


triangle t = new triangle();
square s = new square();
t.area();
t.perimeter();
s.area();
s.perimeter();
}
}
class triangle extends GeometricShape{
void area() {
System.out.println("Area of triangle is;"+1/2*length*height);
}
void perimeter(){
System.out.println("Perimeter of triangle is:"+length*height);
}
}
class square extends GeometricShape{
void area() {
System.out.println("Area of square is:"+length*length);
}
void perimeter () {
System.out.println ("perimeter of square is “+length*2);
}
}
 OUTPUT:
Area of triangle is: 0
Perimeter of triangle is: 0
Area of square is: 0
Perimeter of square is: 0

Process finished with exit code 0

2. PROGRAM 2:

public abstract class Employee {


abstract void calculateSalary();

abstract void displayInfo();

int Salary = 40000;

public static void main(String[] args) {


System.out.println("Salary of employees");
Manager M = new Manager();
programmer p = new programmer();
M.displayInfo();
M.calculateSalary();
p.displayInfo();
p.calculateSalary();
}
}
class Manager extends Employee{
void displayInfo() {
System.out.println("Salary of Manager is:");
}
void calculateSalary() {
int bonus=4000;
System.out.println(Salary+bonus);
}
}
class programmer extends Employee{
void displayInfo() {
System.out.println("Salary of Programmer is:");
}
void calculateSalary() {
int bonus=2000;
System.out.println(Salary+bonus);}
}
 OUTPUT:
Salary of employees
Salary of Manager is:
44000
Salary of Programmer is:
42000

Process finished with exit code 0

3. PROGRAM 3:
abstract class BankAccount {
protected double balance;

public BankAccount(double initialBalance) {


this.balance = initialBalance;
}
public abstract void deposit(double amount);

public abstract void withdraw(double amount);

public double getBalance() {


return balance;
}
}

class SavingsAccount extends BankAccount {


private double interestRate;

public SavingsAccount(double initialBalance, double interestRate) {


super(initialBalance);
this.interestRate = interestRate;
}

@Override
public void deposit(double amount) {
balance += amount;
}

@Override
public void withdraw(double amount) {
if (balance >= amount) {
balance -= amount;
} else {
System.out.println("Insufficient funds for withdrawal.");
}
}

public void addInterest() {


balance += balance * interestRate;
}
}
class CurrentAccount extends BankAccount {
private double overdraftLimit;

public CurrentAccount(double initialBalance, double overdraftLimit) {


super(initialBalance);
this.overdraftLimit = overdraftLimit;
}

@Override
public void deposit(double amount) {
balance += amount;
}

@Override
public void withdraw(double amount) {
if (balance + overdraftLimit >= amount) {
balance -= amount;
} else {
System.out.println("Insufficient funds for withdrawal.");
}
}
}

class BankAccountManager {
public static void main(String[] args) {
SavingsAccount savingsAccount = new SavingsAccount(1000, 0.05);
CurrentAccount currentAccount = new CurrentAccount(500, 1000);

savingsAccount.deposit(500);
savingsAccount.withdraw(200);
savingsAccount.addInterest();

currentAccount.deposit(300);
currentAccount.withdraw(800);

System.out.println("Savings Account Balance: " +


savingsAccount.getBalance());
System.out.println("Current Account Balance: " +
currentAccount.getBalance());
}
}
 OUTPUT:
Savings Account Balance: 1365.0
Current Account Balance: 0.0

Process finished with exit code 0

4. PROGRAM 4:
abstract class Shape {
abstract double calculateArea();
abstract double calculatePerimeter();
}

class Circle extends Shape {


private double radius;

public Circle(double radius) {


this.radius = radius;
}

@Override
double calculateArea() {
return Math.PI * Math.pow(radius, 2);
}

@Override
double calculatePerimeter() {
return 2 * Math.PI * radius;
}
}

class Triangle extends Shape {


private double base;
private double height;
public Triangle(double base, double height) {
this.base = base;
this.height = height;
}

@Override
double calculateArea() {
return 0.5 * base * height;
}

@Override
double calculatePerimeter() {
double hypotenuse = Math.sqrt(Math.pow(base, 2) + Math.pow(height,
2));
return base + height + hypotenuse;
}
}

public class shape {


public static void main(String[] args) {
Circle circle = new Circle(5);
Triangle triangle = new Triangle(3, 4);

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:
Process finished

You might also like