2016-01-23 73 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