You are on page 1of 3

Q3: Create the class Account .

In this class declare a double static variable called interest in


order to handle the interestRate. This class contains a double type private instance variable
Balance which tells the amount the on deposit. Write a function CalculateInterestMonthly to
calculate the interest by multiplying the Balance by interest and divided by 12 . Furthermore this
calculated answer should be added to Balance. Write a static method ChangeInterestRate to set
the interest to a new value. Drive class Account in such a way that Create two Account objects ,
obj1 and obj2 with Balances of 1500 and 1000 rs, respectively in main function of Driver class.
Set interest to 4%, then calculate the monthly interest and display the new balances for both obj.
Then set the Interest to 5%, calculate the next month’s interest and display the new balances for
both objects. (6 marks)

import java.util.Scanner;
public class SavingsAccount{

private static double annualInterestRate;


private double savingsBalance;

public SavingsAccount()
{
savingsBalance = 0;
annualInterestRate = 0;
}

public SavingsAccount(double balance)


{
savingsBalance = balance;
annualInterestRate = 0;
}

public void calculateMonthlyInterest()


{
System.out.println("Current savings balance: " + savingsBalance);
double monthlyInterest;
monthlyInterest = (savingsBalance * annualInterestRate)/12;
savingsBalance = monthlyInterest;
System.out.println("New savings balance: " + savingsBalance);
}

public double getBalance()


{
return savingsBalance;
}
public static void modifyInterestRate(double newInterestRate)
{
annualInterestRate = newInterestRate;
}
}
class Driver
{
public static void main(String[] args)
{
SavingsAccount saver1 = new SavingsAccount(2000);
SavingsAccount saver2 = new SavingsAccount(4000);
saver1.modifyInterestRate(.03);
saver1.calculateMonthlyInterest();
saver2.modifyInterestRate(.03);
saver2.calculateMonthlyInterest();
saver1.modifyInterestRate(.05);
saver1.calculateMonthlyInterest();
saver2.modifyInterestRate(.05);
saver2.calculateMonthlyInterest();
}
}

Ans 2

public class SavingsAccount{

private static double annualInterestRate;


private double savingsBalance;

public SavingsAccount()
{
savingsBalance = 0;
annualInterestRate = 0;
}

public SavingsAccount(double balance)


{
savingsBalance = balance;
annualInterestRate = 0;
}

public void calculateMonthlyInterest()


{
System.out.println("Current savings balance: " + savingsBalance);
double monthlyInterest;
monthlyInterest = (savingsBalance * annualInterestRate)/12;
savingsBalance = monthlyInterest;
System.out.println("New savings balance: " + savingsBalance);
}

public double getBalance()


{
return savingsBalance;
}
public static void modifyInterestRate(double newInterestRate)
{
annualInterestRate = newInterestRate;
}

public static void main(String[] args)


{
SavingsAccount saver1 = new SavingsAccount(2000);
SavingsAccount saver2 = new SavingsAccount(4000);
saver1.modifyInterestRate(.03);
saver1.calculateMonthlyInterest();
saver2.modifyInterestRate(.03);
saver2.calculateMonthlyInterest();
saver1.modifyInterestRate(.05);
saver1.calculateMonthlyInterest();
saver2.modifyInterestRate(.05);
saver2.calculateMonthlyInterest();
}
}

Ans 3

public class SavingsAccount{

private static double annualInterestRate;


private double savingsBalance;

public SavingsAccount()
{
savingsBalance = 0;
annualInterestRate = 0;
}

public SavingsAccount(double balance)


{
savingsBalance = balance;
annualInterestRate = 0;
}

public void calculateMonthlyInterest()


{
System.out.println("Current savings balance: " + savingsBalance);
double monthlyInterest;
monthlyInterest = (savingsBalance * annualInterestRate)/12;
savingsBalance += monthlyInterest;
System.out.println("New savings balance: " + savingsBalance);
}

public double getBalance()


{
return savingsBalance;
}
public static void modifyInterestRate(double newInterestRate)
{
annualInterestRate = newInterestRate;
}
}

You might also like