Fiza Oop 12
Fiza Oop 12
Karachi Campus
Submitted By:
Submitted To:
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)
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:
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;
}
}
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);
}
}
<<Interface>>
Eccentric Shape
Ellipse
Rectangle
Circle Square
FIZA KHAN 3
24/5/2024 Object Oriented Programming
[Interfaces]
Solution:
Main class:
package fizalab12_task1;
Eccentric class:
package fizalab12_task1;
Shape class:
package fizalab12_task1;
Ellipse class:
package fizalab12_task1;
FIZA KHAN 4
24/5/2024 Object Oriented Programming
[Interfaces]
@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;
@Override
public double area() {
return Math.PI * radius * radius;
}
@Override
public double perimeter() {
return 2.0 * Math.PI * 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;
Square class:
package fizalab12_task1;
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;
FIZA KHAN 7
24/5/2024 Object Oriented Programming
[Interfaces]
BankingSystem class:
package fizalab12_task2;
Account class:
package fizalab12_task2;
FIZA KHAN 8
24/5/2024 Object Oriented Programming
[Interfaces]
this.name = name;
}
@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 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]
@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;
}
}
FIZA KHAN 10
24/5/2024 Object Oriented Programming
[Interfaces]
Output:
FIZA KHAN 11