gpt4 book ai didi

java - 继承和通过方法传递参数

转载 作者:行者123 更新时间:2023-12-01 10:33:08 25 4
gpt4 key购买 nike

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 部分中遇到了麻烦。对于初学者来说,这不是独立的代码。它是银行账户计划的较大部分。 “teller”部分是实际运行的代码。

我的问题是我无法让“SavingsAccount”进行干净编译。我为第 5 部分尝试的流程没有成功。它说它无法读取该符号,但我不确定它到底错在哪里。我还编译了“teller”文件,但编译时会出现一些错误。最大的问题是它找不到我一开始没有接触过的类和符号。我不确定这是否是因为我没有让 SavingsAccount 干净地编译。

编译器错误:Activity-10-1\SavingsAccount.java:68:错误:找不到符号 存款 (getBalance() * (利率/12.0)); ^ 符号:方法 getBalance() 位置:类 SavingsAccount1 个错误

工具已完成,退出代码为 1

最佳答案

您需要先完成第 1 步,然后第 5 步才能发挥作用。就目前情况而言,SavingsAccount 继承自 Object,而不是BankAccount。尝试改变

public class SavingsAccount

public class SavingsAccount extends BankAccount

此时,getBalance() 方法(我猜测是在 BankAccount 类中定义的)应该可以访问。

关于java - 继承和通过方法传递参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34970126/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com