0% found this document useful (0 votes)
10 views14 pages

Fiza Oop 12

This document outlines the lab assignments for the Object Oriented Programming course at Bahria University for Spring 2024, including tasks related to Java programming concepts such as classes, inheritance, and interfaces. It details specific lab experiments, including the implementation of an Ellipse class and a Banking System interface, along with example code and expected outputs. The document serves as a guide for students to complete their lab tasks and understand object-oriented principles in Java.

Uploaded by

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

Fiza Oop 12

This document outlines the lab assignments for the Object Oriented Programming course at Bahria University for Spring 2024, including tasks related to Java programming concepts such as classes, inheritance, and interfaces. It details specific lab experiments, including the implementation of an Ellipse class and a Banking System interface, along with example code and expected outputs. The document serves as a guide for students to complete their lab tasks and understand object-oriented principles in Java.

Uploaded by

fizajamshed4
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Bahria University,

Karachi Campus

Course: CSC-210 - Object Oriented Programming


Term: Spring 2024, Class: BSE- 2(C)

Submitted By:

Fiza Khan 89219


(Name) (Reg. No.)

Submitted To:

Engr. Mahawish/Engr. Saniya Sarim

Signed Remarks: Score:


INDEX
SNO DATE LAB LAB OBJECTIVE SIGN
NO
1 16/2/2024 1 Introduction To Java Programming Language

2 23/2/2024 2 Implementation Of Class and Object In OOP

3 1/3/2024 3 Access Modifiers in Java

4 8/3/2024 4 Constructors in Java


5 15/3/2024 5 Introduction to Static Variables and Methods
6 22/3/2024 7 Inheritance
7 26/4/2024 8 Polymorphism
8 3/5/2024 9 Association, Aggregation and Composition
9 10/5/2024 10 GUI in java
10 17/5/2024 11 Abstract Classes, Upcasting and Downcasting
11 24/5/2024 12 Interfaces
Bahria University,
Karachi Campus

LAB EXPERIMENT NO.


12
LIST OF TASKS
TASK NO OBJECTIVE
1 By looking at the formulae for an ellipse, provide the missing code for all of the
methods in the class Ellipse including the toString() method. Test your
program by calling the methods of all eccentric shapes. Your output should look as
follows (for an ellipse with a = 10 and b = 7)
2 Write a program which implements a interface of Banking System by having all
standard functionalities and will be implemented by branches.

Submitted On:
30/5/2024
(Date: DD/MM/YY)
24/5/2024 Object Oriented Programming
[Interfaces]

LAB # 12
Task # 01:
Below we consider an extension to our Shape class. Here we discuss about elliptical shapes which have
properties in addition to area and perimeter which are of interest to us.

Consider the Shape called Ellipse. An ellipse is basically nothing but an elongated circle. A circle has
a radius. An ellipse has two measurements: the major axis (a) and the minor axis (b)

with a > b.

Minor
axis (b)

radius
Major axis (a)

Note that in an ellipse if a = b then it becomes a circle.

The measure of “roundness” of an ellipse is given by its eccentricity e where the value of e is
always between 0 and 1. For a circle e = 0. The higher the value of e, the higher is the deviation of
the ellipse from its roundness.

Eccentricity is associated with a lot of other shapes as well e.g. hyperbola, parabola, etc.
The various formulae for the ellipse are:

Perimeter = P = π 2(a2 + b2) - (a - b)2/2 [Note that if a = b = r, then P = 2πr]


Area = A = πab
Eccentricity = e = 1 - b2/a2

We can define an interface called Eccentric for all eccentric Shapes.

Eccentric.java

FIZA KHAN 1
24/5/2024 Object Oriented Programming
[Interfaces]

/*
* Eccentric.java
*/
interface Eccentric {
double eccentricity();
}

Note here that no access modifiers have been given for the method eccentricity. The reason is that all
methods in an interface are by default public and abstract. Now we can define our class Ellipse.

Ellipse.java
/*
* Ellipse.java
*/
public class Ellipse extends Shape implements Eccentric
{
double a, b;
public Ellipse(double s1, double s2){
if(s1 < s2) {
a = s2;
b = s1;
}
else {
a = s1;
b = s2;
}
}

public double perimeter(){


//method body missing
}

public double area(){


//method body missing
}

public double eccentricity(){


//method body missing
}

public String toString(){


//method body missing
}
}

FIZA KHAN 2
24/5/2024 Object Oriented Programming
[Interfaces]

1. By looking at the formulae for an ellipse, provide the missing code for all of the methods in the class
Ellipse including the toString() method. Test your program by calling the methods of all
eccentric shapes. Your output should look as follows (for an ellipse with a = 10 and b = 7)
2. Square
3. Area=100.0
4. Perimeter=40.0
5.
6. Ellipse
7. Area=219.9114857512855
8. Perimeter=53.8212680240788
9. Eccentricity=0.714142842854285
10. Press any key to continue...

How about the following class Circle. Since a Circle is a special case of an Ellipse, will the output of
TestShapes.java be affected if the following class is used instead of the class Circle used previously:
Circle.java
public class Circle extends Ellipse {
public Circle(double radius){
super(radius, radius);
}
}

With this modification, the class diagram would look as follows:

<<Interface>>
Eccentric Shape

Ellipse
Rectangle

Circle Square

FIZA KHAN 3
24/5/2024 Object Oriented Programming
[Interfaces]

Solution:
Main class:
package fizalab12_task1;

public class FizaLab12_Task1 {

public static void main(String[] args) {


Rectangle r=new Rectangle(6,2);
Square s=new Square(4);
Ellipse e=new Ellipse(9, 14);
Circle c=new Circle(3);
System.out.println(r);
System.out.println(s);
System.out.println(e);
System.out.println(c);

Eccentric class:
package fizalab12_task1;

public interface Eccentric {


double eccentricity();

Shape class:

package fizalab12_task1;

public abstract class Shape {

public String name(){


return getClass().getName();
}
public abstract double area();

public abstract double perimeter();

public String toString() {


return "\n" +name() +"\nArea=" +area() +"\nPerimeter=" +perimeter();

Ellipse class:
package fizalab12_task1;

public class Ellipse extends Shape implements Eccentric {


double a, b;

public Ellipse(double s1, double s2) {

FIZA KHAN 4
24/5/2024 Object Oriented Programming
[Interfaces]

if (s1 < s2) {


a = s2;
b = s1;
} else {
a = s1;
b = s2;
}
}

@Override
public double perimeter() {
return 4 * (Math.PI * a * b + (a - b)) / (a + b);
}

@Override
public double area() {
return Math.PI * a * b;
}

@Override
public double eccentricity() {
return Math.sqrt(1 - (b * b) / (a * a));
}

@Override
public String toString() {
return "\nEllipse: \nMajor axis a: " + a + "\nMinor axis b: " + b +
"\nPerimeter: " +perimeter() + " \nArea: " +area () + "\nEccentririty: " +
eccentricity() ;
}
}

Circle class:
package fizalab12_task1;

public class Circle extends Ellipse {

private double radius;

public Circle(double radius) {


super(radius, radius);
this.radius = radius;
}

@Override
public double area() {
return Math.PI * radius * radius;
}

@Override
public double perimeter() {
return 2.0 * Math.PI * radius;
}

public double getRadius() {


return radius;
}

FIZA KHAN 5
24/5/2024 Object Oriented Programming
[Interfaces]

@Override
public String toString() {
return "\nCircle: \nRadius: " + radius + "\nArea: " + area() + "\nPerimeter: "
+ perimeter() + "\nEccentririty: " + eccentricity();
}
}

Rectangle class:
package fizalab12_task1;

public class Rectangle extends Shape{


private double length;
private double width;

public Rectangle(double length, double width){


this.length = length;
this.width = width;
}
public double area(){
return length * width;
}

public double perimeter(){


return 2*(length+width);
}

public double getLength(){


return length;
}

public double getWidth(){


return width;
}
}

Square class:
package fizalab12_task1;

public class Square extends Rectangle{

public Square(double length) {


super(length, length);
}

FIZA KHAN 6
24/5/2024 Object Oriented Programming
[Interfaces]

Output:

Task # 02:
Write a program which implements a interface of Banking System by having all standard functionalities
and will be implemented by branches.

Hint:(Interface Methods)
CreateAccount()
Search Account details()
Update CustInfo()
Cash Withdraw()
Cash Deposit()

Main class:
package fizalab12_task2;

public class Fizalab12_Task2 {

FIZA KHAN 7
24/5/2024 Object Oriented Programming
[Interfaces]

public static void main(String[] args) {

Gulshanbranch branch = new Gulshanbranch();


branch.CreateAccount("7865", "Fiza", "70000");
System.out.println("-------------------------------------------------");
branch.CreateAccount("123", "Zunaira", "25000");
System.out.println("-------------------------------------------------");
System.out.println(branch.SearchAccountdetails("7865"));
System.out.println("-------------------------------------------------");
branch.UpdateCustInfo("7865", "Alishba");
System.out.println("-------------------------------------------------");
System.out.println(branch.SearchAccountdetails("123"));
System.out.println("-------------------------------------------------");
branch.CashDeposit("7865", 3000);
System.out.println("-------------------------------------------------");
branch.CashWithdraw("123", 50000);
}
}

BankingSystem class:
package fizalab12_task2;

public interface BankingSystem {


void CreateAccount(String acnum, String name,String am);
String SearchAccountdetails(String acnum);
void UpdateCustInfo(String acnum,String name);
double CashWithdraw(String acnum,double am);
double CashDeposit(String acnum,double am);

Account class:
package fizalab12_task2;

public class Account {


private String acnum;
private String name;
private double balance;

public Account(String acnum, String name, double balance) {


this.acnum = acnum;
this.name = name;
this.balance = balance;
}

public String getAcnum() {


return acnum;
}

public String getName() {


return name;
}

public void setName(String name) {

FIZA KHAN 8
24/5/2024 Object Oriented Programming
[Interfaces]

this.name = name;
}

public double getBalance() {


return balance;
}

public void setBalance(double balance) {


this.balance = balance;
}

@Override
public String toString() {
return "Account Number: " + acnum + ", Name: " + name + ", Balance: " +
balance;
}
}

Gulshanbranch class:
package fizalab12_task2;
import java.util.ArrayList;
import java.util.List;

public class Gulshanbranch implements BankingSystem {

private List<Account> accounts;

public Gulshanbranch() {
accounts = new ArrayList<>();
}

@Override
public void CreateAccount(String acnum, String name, String am) {
if (findAccountByNumber(acnum) == null) {
double initialAmount = Double.parseDouble(am);
Account newAccount = new Account(acnum, name, initialAmount);
accounts.add(newAccount);
System.out.println("Account created successfully.");
} else {
System.out.println("Account number already exists.");
}
}

@Override
public String SearchAccountdetails(String acnum) {
Account account = findAccountByNumber(acnum);
if (account != null) {
return account.toString();
} else {
return "Account not found.";
}
}

@Override
public void UpdateCustInfo(String acnum, String name) {

FIZA KHAN 9
24/5/2024 Object Oriented Programming
[Interfaces]

Account account = findAccountByNumber(acnum);


if (account != null) {
account.setName(name);
System.out.println("Customer information updated successfully.");
} else {
System.out.println("Account not found.");
}
}

@Override
public double CashWithdraw(String acnum, double am) {
Account account = findAccountByNumber(acnum);
if (account != null) {
if (account.getBalance() >= am) {
account.setBalance(account.getBalance() - am);
System.out.println("Withdrawal successful.");
return am;
} else {
System.out.println("Insufficient balance.");
return 0;
}
} else {
System.out.println("Account not found.");
return 0;
}
}

@Override
public double CashDeposit(String acnum, double am) {
Account account = findAccountByNumber(acnum);
if (account != null) {
account.setBalance(account.getBalance() + am);
System.out.println("Deposit successful.");
return account.getBalance();
} else {
System.out.println("Account not found.");
return 0;
}
}

private Account findAccountByNumber(String acnum) {


for (Account account : accounts) {
if (account.getAcnum().equals(acnum)) {
return account;
}
}
return null;
}
}

FIZA KHAN 10
24/5/2024 Object Oriented Programming
[Interfaces]

Output:

FIZA KHAN 11

You might also like