0% found this document useful (0 votes)
11 views3 pages

Static Method Qs

Uploaded by

eman063895
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)
11 views3 pages

Static Method Qs

Uploaded by

eman063895
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/ 3

Static method qs

public class SavingsAccount {

private static double annualInterestRate; // Static data member to store annual interest rate

private double savingsBalance; // Private data member indicating the balance of account

// Constructor

public SavingsAccount(double savingsBalance) {

this.savingsBalance = savingsBalance;

// Static method to modify the annual interest rate

public static void modifyInterestRate(double newRate) {

annualInterestRate = newRate;

// Method to calculate monthly interest and update savings balance

public void calculateMonthlyInterest() {

double monthlyInterest = (savingsBalance * annualInterestRate) / 12;

savingsBalance += monthlyInterest;

// Getter for savings balance

public double getSavingsBalance() {

return savingsBalance;

}
}

public class SavingsAccountTest {

public static void main(String[] args) {

// Instantiate two different objects of class SavingsAccount

SavingsAccount saver1 = new SavingsAccount(2000.00);

SavingsAccount saver2 = new SavingsAccount(3000.00);

// Set the annualInterestRate to 3 percent

SavingsAccount.modifyInterestRate(0.03);

// Calculate and print the new balances for each saver after one month

System.out.println("Monthly interest for saver1:");

saver1.calculateMonthlyInterest();

System.out.println("Updated balance: $" + saver1.getSavingsBalance());

System.out.println("Monthly interest for saver2:");

saver2.calculateMonthlyInterest();

System.out.println("Updated balance: $" + saver2.getSavingsBalance());

// Set the annualInterestRate to 4 percent

SavingsAccount.modifyInterestRate(0.04);

// Calculate and print the new balances for each saver after one more month

System.out.println("Monthly interest for saver1 after changing interest rate:");

saver1.calculateMonthlyInterest();
System.out.println("Updated balance: $" + saver1.getSavingsBalance());

System.out.println("Monthly interest for saver2 after changing interest rate:");

saver2.calculateMonthlyInterest();

System.out.println("Updated balance: $" + saver2.getSavingsBalance());

}}

You might also like