2016-01-23 79 views
0
import java.text.NumberFormat; 

// 1. ***** indicate that SavingsAccount inherits 
//   from BankAccount 
public class SavingsAccount 
{ 

public final double DEFAULT_RATE = .03; 

// 2. ****** define the private interestRate instance variable 
// interestRate, a double, represents an annual rate 
// Part 2 student code starts here: 
    private double interestRate; 



// Part 2 student code ends here. 

// 3 ***** write the default constructor 
/** default constructor 
* explicitly calls the BankAccount default constructor 
* set interestRate to default value DEFAULT_RATE 
* print a message to System.out indicating that 
* constructor is called 
*/ 
// Part 3 student code starts here: 
    public void BankAccount() 
    { 
     interestRate = DEFAULT_RATE; 
     System.out.println("Default constructor is called. "); 
    } 


// Part 3 student code ends here. 

// 4 ***** write the overloaded constructor 
/** overloaded constructor 
* explicitly call BankAccount overloaded constructor 
* call setInterestRate method, passing startInterestRate 
* print a message to System.out indicating that 
* constructor is called 
* @param startBalance  starting balance 
* @param startInterestRate starting interest rate 
*/ 
// Part 4 student code starts here: 

    public void BankAccount(double startBalance,double startInterestRate) 
    { 

     setInterestRate(startInterestRate); 
     System.out.println("Overloaded constructor is called. "); 
    } 
     // Part 4 student code ends here. 

// 5 ****** write this method: 
/** applyInterest method, no parameters, void return value 
* call deposit method, passing a month's worth of interest 
* remember that interestRate instance variable is annual rate 
*/ 
// Part 5 student code starts here: 

    public void applyInterest() 
    { 
     deposit (super.getBalance() * (interestRate/12.0)); 
    } 

// Part 5 student code ends here. 

/** accessor method for interestRate 
* @return interestRate 
*/ 
public double getInterestRate() 
{ 
    return interestRate; 
} 

/** mutator method for interestRate 
* @param newInterestRate new value for interestRate 
*   newInterestRate must be >= 0.0 
*   if not, print an error message 
*/ 
public void setInterestRate(double newInterestRate) 
{ 
    if (newInterestRate >= 0.0) 
    { 
    interestRate = newInterestRate; 
    } 
    else 
    { 
    System.err.println("Interest rate cannot be negative"); 
    } 
} 

// 6 ***** write this method 
/* toString method 
* @return String containing formatted balance and interestRate 
* invokes superclass toString to format balance 
* formats interestRate as percent using a NumberFormat object 
* To create a NumberFormat object for formatting percentages 
* use the getPercentInstance method in the NumberFormat class, 
* which has this API: 
*  static NumberFormat getPercentInstance() 
*/ 
// Part 6 student code starts here: 

    public String toString() 
    { 
     NumberFormat percent = NumberFormat.getPercentInstance(); 
     return super.toString() 
     + "\n" + "interestRate is " + percent.format(interestRate); 
    } 

// Part 6 student code ends here. 

} 

到目前爲止,我在第5部分遇到了問題。對於初學者來說,這不是獨立代碼。這是銀行賬戶計劃的一大部分。 '出納'部分是實際運行的代碼。通過方法繼承和傳遞參數

我的問題是我無法獲得'SavingsAccount'來清理編譯。我沒有辦法嘗試第5部分的作品。它說它不能讀取符號,但我不確定它究竟有多嚴重。我也編譯了'teller'文件,但是如果沒有一些錯誤就不會編譯。最重要的是,它無法找到我首先沒有觸及的課程和符號。我不確定這是否是由於我沒有使用SavingsAccount進行乾淨編譯。編譯器錯誤:Activity-10-1 \ SavingsAccount.java:68:error:找不到符號 deposit(getBalance()*(interestRate/12.0)); ^ 符號:方法爲getBalance() 位置:類SavingsAccount 1錯誤

工具退出碼1

+0

噢,來吧!給我多一點信用。那根本不在我的程序中。這只是從stackoverflow的東西。我粘貼代碼後仍然存在。 – CoderChic

+0

deposit()和getBalance()是什麼樣的? – MidasLefko

+0

您的問題在第5步之前開始。您尚未爲您的課程定義任何構造函數。您已經定義了兩個名爲BankAccount的方法,並返回void。閱讀https://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html。此外,如果您詢問編譯錯誤,至少應該粘貼確切的完整錯誤消息。 –

回答

1

你需要得到第1步之前完成第5步將工作完成。按照原樣,SavingsAccount繼承自Object,而不是從BankAccount繼承。嘗試改變

public class SavingsAccount 

public class SavingsAccount extends BankAccount 

在這一點上,getBalance()方法(我猜在BankAccount類中定義)應該成爲訪問。

+0

我錯過了延伸!我忘了那部分 – CoderChic