38.3k views
5 votes
Can you debug the following code using the given test code?public class SavingAccount{ // interest rate for all accounts private static double annualInterestRate = 0; private final double savingsBalance; // balance for currrent account // constructor, creates a new account with the specified balance public void SavingAccount( double savingsBalance ) { savingsBalance = savingsBalance; } // end constructor // get monthly interest public void calculateMonthlyInterest() { savingsBalance += savingsBalance * ( annualInterestRate / 12.0 ); } // end method calculateMonthlyInterest // modify interest rate public static void modifyInterestRate( double newRate ) { annualInterestRate = ( newRate >= 0 && newRate <= 1.0 ) ? newRate : 0.04; } // end method modifyInterestRate // get string representation of SavingAccount public String toString() { return String.format( "$%.2f", savingsBalance ); } // end method toSavingAccountString} // end class SavingAccount Using this test codepublic class SavingAccountTest{ public static void main(String[] args) { SavingAccount s1 = new SavingAccount(400); SavingAccount s2 = new SavingAccount(1000); SavingAccount.modifyInterestRate(0.05); System.out.printf("SavingAccount 1 is: %s\\SavingAccount 2 is: %s\\", s1, s2); s1.calculateMonthlyInterest(); s2.calculateMonthlyInterest(); System.out.printf("\\SavingAccount 1 after the interest is: %s\\SavingAccount 2 after the interest is: %s\\", s1, s2); SavingAccount.modifyInterestRate(.025); s1.calculateMonthlyInterest(); s2.calculateMonthlyInterest(); System.out.printf("\\SavingAccount 1 after the new interest is: %s\\SavingAccount 2 after the new interest is: %s\\", s1, s2); }}

User Jmatthias
by
5.0k points

1 Answer

3 votes

Answer:

Step-by-step explanation:

The reason the code was not working was due to a couple of errors. First, the SavingAccount class needed to be public so that it can be accessed within the same file. Secondly, the savingsBalance variable was set to final and therefore could not be modified, the final keyword needed to be removed. Lastly, the constructor for the SavingAccount class was incorrect. Constructors do not use the keyword void and instance class variables need to be referenced using the this keyword.

class SavingAccount{

// interest rate for all accounts

private static double annualInterestRate = 0;

private double savingsBalance;

public SavingAccount(double savingsBalance) {

this.savingsBalance = savingsBalance;

}

public void calculateMonthlyInterest() {

savingsBalance += savingsBalance * ( annualInterestRate / 12.0 );

}// end method calculateMonthlyInterest

// modify interest rate

public static void modifyInterestRate( double newRate ) {

annualInterestRate = ( newRate >= 0 && newRate <= 1.0 ) ? newRate : 0.04;

} // end method modifyInterestRate

// get string representation of SavingAccount

public String toString() { return String.format( "$%.2f", savingsBalance );

} // end method toSavingAccountString

}// end class SavingAccount

// Using this test codepublic

//

class SavingAccountTest{

public static void main(String[] args) {

SavingAccount s1 = new SavingAccount(400);

SavingAccount s2 = new SavingAccount(1000);

SavingAccount.modifyInterestRate(0.05);

System.out.printf("SavingAccount 1 is: %s\\SavingAccount 2 is: %s", s1, s2);

s1.calculateMonthlyInterest(); s2.calculateMonthlyInterest();

System.out.printf("\\SavingAccount 1 after the interest is: %s\\SavingAccount 2 after the interest is: %s", s1, s2);

SavingAccount.modifyInterestRate(.025);

s1.calculateMonthlyInterest();

s2.calculateMonthlyInterest();

System.out.printf("\\SavingAccount 1 after the new interest is: %s\\SavingAccount 2 after the new interest is: %s", s1, s2); }}

Can you debug the following code using the given test code?public class SavingAccount-example-1
User YonBav
by
4.7k points