【问题标题】:Using a method from one class into another class使用从一个类到另一个类的方法
【发布时间】:2019-07-04 13:31:28
【问题描述】:

我在使用最后一种方法时遇到了问题。我被告知我必须在我的银行类的每月费用方法中使用我的 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;   
}
}

Bank.class

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!!!");
}
}

非常感谢任何帮助和指导。谢谢。

【问题讨论】:

  • 在调用给定银行账户对象的调整方法时需要传递一个双精度值
  • 并始终发布给出错误的确切代码。不放错误内容,而是在导致问题的地方有不同的代码和注释!总是有一个minimal reproducible example

标签: java arrays class methods


【解决方案1】:

您似乎忘记将费用参数传递给 adjust() 方法。以下代码运行良好

 public void monthlyFee(double fee) { //monthly fee for bank accounts
    for(int i = 0; i < bank.length; i++) {
      if(bank[i] != null) {
        System.out.println(bank[i].adjust(fee));
      } //I have tried BankAccount.adjust() and couldn't work, bank[i].adjust() nothing seems to work
    }
  }

创建银行 1

一个帐户 Joe Mac 拥有账户 1234,余额为 $1000.00

两个帐户 Joe Mac 拥有账户 1234,余额为 $1000.00 Sally Ride 拥有账户 2345,余额为 $2000.00

三个帐户 Joe Mac 拥有账户 1234,余额为 $1000.00 Sally Ride 拥有账户 2345,余额为 $2000.00 Pat Armstrong 拥有账户 3456,余额为 $3000.00

月费 997.05 1997.05 2997.05 Joe Mac 拥有账户 1234,余额为 997.05 美元 Sally Ride 拥有账户 2345,余额为 $1997.05 Pat Armstrong 拥有账户 3456,余额为 2997.05 美元

错误:

没有发现错误!!!

【讨论】:

  • 我尝试了类似的方法,但我从未将费用放在括号中。非常感谢。
【解决方案2】:

简单:当您检查您的测试代码时,您会发现在调用 adjust() 方法时它传递了一个双精度值!

调用该方法时,您的其他代码(导致错误的代码)似乎没有传递任何双精度值!

【讨论】:

  • @A.O'Mary 非常欢迎您。我很感激快速接受,即使它去了另一个答案;-)
猜你喜欢
  • 1970-01-01
  • 2014-02-07
  • 1970-01-01
  • 2012-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多