将static属性添加到构造函数属性

时间:2012-04-10 19:57:43

标签: java static

问题如下:

  

(储蓄帐户类)创建类SavingsAccount。使用静态   变量annualInterestRate存储所有的年利率   帐户持有人。该类的每个对象都包含一个私有实例   变量savingsBalance指示当前保护程序的数量   存款。提供方法calculateMonthlyInterest来计算   通过将savingsBalance乘以每月利息   annualInterestRate除以12 - 应该添加此兴趣   储蓄 - 平衡。提供一个静态方法modifyInterestRate设置   年度利息率达到新的价值。写一个程序来测试类   SavingsAccount。实例化两个savingsAccount对象,saver1和   saver2,余额分别为$ 2000.00和$ 3000.00。组   annualInterestRate为4%,然后计算每个月的利息   12个月,打印两个储户的新余额。接下来,设置   annualInterestRate为5%,计算下个月的利息和   打印两个储户的新余额。

我解决了所有问题,但余额没有增加 - 它保持不变。它应该随着年利率的每次变化而增加(至少,这是我所理解的)。

class SavingsAccount
{
    static double annualInterestRate;
    private double savingsBalance;

    public SavingsAccount(double balance)
    {
        savingsBalance = balance;
    }

    public double calculateMonthlyInterest()
    {
        return (savingsBalance*annualInterestRate)/12;
    }

    public static void modifyInterestRate(double rate)
    {
        annualInterestRate = rate;
    }


    public static double getannualInterestRate(){return annualInterestRate;}

    public double getsavingsBalance(){return savingsBalance;}

}

public class SavingsTest
{
    public static void main(String args[])
    {
        SavingsAccount saver1 = new SavingsAccount(2000.0);
        SavingsAccount saver2 = new SavingsAccount(3000.0);

        SavingsAccount.modifyInterestRate(4);

        System.out.printf("Balance for Saver1 = %.2f\nBalance for Saver2 = %.2f\nInterest Rate = %.2f\n\n",saver1.getsavingsBalance()+saver1.calculateMonthlyInterest(),saver2.getsavingsBalance(),SavingsAccount.getannualInterestRate());

        SavingsAccount.modifyInterestRate(5);

        System.out.printf("New Balance for Saver1 = %.2f\nNew Balance for Saver2 = %.2f\nInterest Rate = %.2f\n\n",saver1.getsavingsBalance(),saver2.getsavingsBalance(),SavingsAccount.getannualInterestRate());
    }
}

4 个答案:

答案 0 :(得分:1)

您未在此代码中修改savingsBalance的值。

答案 1 :(得分:0)

public double addMonthlyInterest()     {    
  savingsBalance += (savingsBalance*annualInterestRate)/12;
  return savingBalance;     
}  

这将返回新余额和'增量'

答案 2 :(得分:0)

在您的第一个print语句中,对于第一个参数,您在计算月利息并将它们加在一起后计算余额,并且未将新值设置为类成员savingsBalance的值。

在第二个print语句中,由于您没有通过setter将计算值设置回类成员,因此只是打印出实例化类的原始值。

另外,请勿在print语句中进行内联计算。它们令人困惑,并且不容易为任何人阅读。一种好方法是初始化方法中的本地成员,并使用它们进行计算并打印本地成员。

答案 3 :(得分:0)

只想分享我的答案。

公共类SavingsAccount {

private static float annualInterestRate = 0f;
private float savingsBalance;

public SavingsAccount(float balance) {
    savingsBalance = balance;
}

public static void setAnnualInterestRate(float t) {
    if (t >= 0 && t <= 1)
        annualInterestRate = t;
    else
        throw new IllegalArgumentException("Annual interest rate should be between 0 and 1");
}

private float calculateMonthlyInterest() {
    return savingsBalance * annualInterestRate / 12;
}

public float getSavingsBalance() {
    return savingsBalance + calculateMonthlyInterest();
}

public float getAnnualInterestRate(){
    return annualInterestRate;
}

public String toString() {
    return String.format("Balance: %.2f", getSavingsBalance());
}

}

中的

    SavingsAccount s1 = new SavingsAccount(2000);
    SavingsAccount s2 = new SavingsAccount(3000);
    SavingsAccount.setAnnualInterestRate(0.04f);

    System.out.println("S1: " + s1);
    System.out.println("S2: " + s2);

    SavingsAccount.setAnnualInterestRate(0.05f);
    System.out.println("S1: " + s1);
    System.out.println("S2: " + s2);