2016-03-28 5 views
-1

私はCOMPILESであっても、このプログラムに少し問題があります。私は主な方法を追加しなければならないと言いますが、私はそれを行い、21のエラーが発生しました。誰か助けてください..SavingsAccount Program

クラスを作成するSavingsAccount。各節約者のannualInterestRateを格納するには、静的クラス変数を使用します。クラスの各オブジェクトには、セーバーが現在デポジットしている金額を示すプライベートインスタンス変数savingsBalanceが含まれています。 monthlyInterestRateを12で割った残高を掛けて月次利子を計算するcalculateMonthlyInterestメソッドを提供します。この利子は貯蓄残高に追加する必要があります。 annualInterestRateを新しい値に設定するstaticメソッドmodifyInterestRateを提供します。 SavingsAccountクラスをテストするドライバプログラムを作成します。 2つの異なるsavingsAccountオブジェクトsaver1とsaver2をそれぞれ$ 2000.00と$ 4000.00のバランスでインスタンス化します。 annualInterestRateを3%に設定し、毎月の利息を計算して、それぞれの貯蓄者の新しい残高を印刷します。その後、annualInterestRateを5%に設定し、次の月の利息を計算し、各貯蓄者の新しい残高を印刷します。

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(); 
    } 
} 
+0

ているのですか? –

+0

エラーはありません。コンパイルできます。しかし、それが実行されるとき、それはエラーがメインメソッドまたはそのような何かを言っていない – Leonardo

+1

こんにちは@レオナルドあなたのコードをチェックします。それは私が与えた答えで出力を見ることができます。 –

答えて

-1

メインメソッドをSavingsAccountクラスに入れます。このよう

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(); 
    } 
} 
+0

このような「public SavingsAccount main()」??? – Leonardo

+1

私の答えをしてください。それは仕事の完璧なメインメソッドを置く必要はない 'SavingsAccountクラス'に入れて –

+0

それはまだ動作しない私はあなたが別のファイルにドライバクラスを保存することであなたがわからないように – Leonardo

0

あなたのコードは完璧な作業。 javac SavingsAccount.javarunを使用してを使用してSavingsAccount.javacompileと保存してください。

I compilerunあなたのコードとoutputあなたはエラーがスタックトレースを追加してくださいすることができ、この

Output