java - 使用从一个类到另一个类的方法
问题描述
我的最后一种方法有问题。我被告知我必须在我的银行类的每月费用方法中使用我的 BankAccount.class 中的调整方法,但我无法弄清楚。我尝试了多种不同的方法,但无法使其正常工作。我们需要用月费调整账户余额。
Bank.java:33:错误:BankAccount 类中的方法调整不能应用于给定类型;BankAccount.adjust(); ^
必需:双重发现:无参数原因:实际参数列表和正式参数列表长度不同 1 错误Bank.java:33: 错误:double 不能被取消引用 fee.adjust(); 1 个错误
Bank.java:33: 错误:找不到符号 bank.BankAccount.adjust(); 符号:变量 BankAccount 位置:BankAccount[] 类型的变量银行 1 错误
BankAccount.class
public class BankAccount {
String owner; // owner of account
int accountNumber; // integer account number
double balance = 0.0; // account balance
double amount; // adjusted amount to balance
String balanceFmt = "%.2f"; // string format for 2 decimal places
public BankAccount(String owner, int accountNumber) { //Constructor for the bank account
this.owner = owner;
this.accountNumber = accountNumber;
}
public double adjust(double amount) { //method to adjust balance
this.balance += amount;
return balance;
}
public String toString() { // method to print out account info
return owner + " owns the account " + accountNumber + " with the balance of $" + String.format(balanceFmt,balance);
}
public double getBalance() { // method to get balance of accounts
return balance;
}
}
银行类
public class Bank {
BankAccount bank[];
public Bank() { // constructor for making a 10 account array
bank = new BankAccount[10];
}
public void addAccount(BankAccount accounts) { // add account for giving numbers to accounts
for(int i = 0; i < bank.length; i++) {
if(bank[i] == null) {
bank[i] = accounts;
break;
}
}
}
BankAccount getAccount(int i) { //obtain account from BankAccount class
return bank[i];
}
public void printAccounts() { //prints out account statuses
for(int i = 0; i < bank.length; i++) {
if(bank[i] != null) {
System.out.println(bank[i]);
}
}
}
public void monthlyFee(double fee) { //monthly fee for bank accounts
for(int i = 0; i < bank.length; i++) {
if(bank[i] != null) {
} //I have tried BankAccount.adjust() and couldn't work, bank[i].adjust() nothing seems to work
}
}
}
BankTest.class
public class BankTest {
/*
* test - set up a bank and add accounts
*/
public static void main(String[] args) {
// Code to test Bank and BankAccount classes
int errors = 0;
double fee = -2.95;
Assignment assignment = new Assignment();
assignment.homework("Homework 2a");
System.out.println("\nCreate bank1");
Bank bank1 = new Bank();
System.out.println("\nOne account");
BankAccount bankAccount1 = new BankAccount("Joe Mac", 1234);
bankAccount1.adjust(1000.0);
bank1.addAccount(bankAccount1);
bank1.printAccounts();
System.out.println("\nTwo accounts");
BankAccount bankAccount2 = new BankAccount("Sally Ride", 2345);
bankAccount2.adjust(2000.0);
bank1.addAccount(bankAccount2);
bank1.printAccounts();
System.out.println("\nThree accounts");
BankAccount bankAccount3 = new BankAccount("Pat Armstrong", 3456);
bankAccount3.adjust(3000.0);
bank1.addAccount(bankAccount3);
bank1.printAccounts();
System.out.println("\nMonthly Fee");
bank1.monthlyFee(fee);
bank1.printAccounts();
System.out.println("\nErrors:");
if (bank1.getAccount(0).getBalance() != 997.05) {
errors += 1;
System.out.println("Balance for account at index 0 does not match $997.05");
}
if (bank1.getAccount(1).getBalance() != 1997.05)
{
errors += 1;
System.out.println("Balance for account at index 1 does not match $1997.05");
}
if (bank1.getAccount(2).getBalance() != 2997.05)
{
errors += 1;
System.out.println("Balance for account at index 2 does not match $2997.05");
}
if (errors == 0)
System.out.println("No errors found!!!");
}
}
非常感谢任何帮助和指导。谢谢你。
解决方案
很简单:当您检查您的测试代码时,您会发现在调用该adjust()方法时它传递了一个双精度值!
您的其他代码(导致错误的代码)在调用该方法时似乎没有传递任何双精度!