COMSATS University Islamabad, Wah
Campus
Department of Computer Science
OOP
ASSIGNMENT NO:03
SUBMITTED TO:
MS SAMIA ZAFAR
SUBMITTED BY:
ASHHAR ZAWAR SYED
REGISTRATION NO:
(SP22-BCS-052)
QUESTION 1:
package com.company.savingsaccountrunner;
public class SavingsAccount {
static double annualInterestRate;
private double savingsBalance;
SavingsAccount(double savingsBalance)
{
this.savingsBalance=savingsBalance;
}
public void calculateMonthlyInterest ()
{
double monthlyInterest =(savingsBalance * annualInterestRate) / 12;
savingsBalance = savingsBalance + monthlyInterest;
}
public static void modifyInterestRate(double newRate)
{
annualInterestRate = newRate;
}
public double getSavingsBalance()
{
return savingsBalance;
}
package com.company.savingsaccountrunner;
public class SavingsAccountRunner {
public static void main(String[] args) {
SavingsAccount saver1= new SavingsAccount(2000);
SavingsAccount saver2= new SavingsAccount(3000);
SavingsAccount.modifyInterestRate(0.03);
saver1.calculateMonthlyInterest();
saver2.calculateMonthlyInterest();
System.out.println("Balance with added monthly interest of Saver1 1st Month is:
"+saver1.getSavingsBalance());
System.out.println("Balance with added monthly interest of Saver2 1st Month is:
"+saver2.getSavingsBalance());
SavingsAccount.modifyInterestRate(0.04);
saver1.calculateMonthlyInterest();
saver2.calculateMonthlyInterest();
System.out.println("Balance with added monthly interest of Saver1 2nd Month is:
"+saver1.getSavingsBalance());
System.out.println("Balance with added monthly interest of Saver2 2nd Month is:
"+saver2.getSavingsBalance());
}
}
OUTPUT:
QUESTION 2:
package com.company.distancerunner;
public class Distance {
private double inches;
private double feet;
Distance(){}
Distance(double inches, double feet)
{
this.inches=inches;
this.feet=feet;
}
public void setFeet(double feet)
{
this.feet=feet;
}
public double getFeet()
{
return feet;
}
public void setInch(double inches)
{
this.inches=inches;
}
public double getInches()
{
return inches;
}
public void display()
{
System.out.println("Distance in Feet: "+feet+"\nDistance in Inches: "+inches);
}
public Distance addDistances(Distance d)
{
Distance addedDistance=new Distance(this.inches+d.inches, this.feet+d.feet);
return addedDistance;
}
public boolean equals(Distance d)
{
if
(this.feet == d.feet & this.inches==d.inches)
return true;
else
return false;
}
}
package com.company.distancerunner;
public class DistanceRunner {
public static void main(String[] args) {
Distance d1=new Distance();
d1.setFeet(1);
d1.setInch(1);
System.out.println("----------D1------------: ");
d1.display();
Distance d2=new Distance(2,2);
System.out.println("----------D2------------: ");
d2.display();
Distance d3=d2.addDistances(d1);
System.out.println("----------ADDED DISTANCE------------: ");
d3.display();
Distance d4=new Distance(3, 3);
System.out.println("----------ADDED DISTANCE AND D4 ARE EQUAL?------------: ");
System.out.println("ADDED DISTANCE AND D4 ARE EQUAL? "+d4.equals(d3));
}
}
OUTPUT: