有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

返回节点而不是子类值的java代码

我对编程非常陌生,我正在学习的一门课程的一些代码有问题。我需要创建一个银行账户类,它可以返回余额、添加存款、取款,并打印一份当前利率的对账单。我一直在努力让主类返回储蓄账户类的值。相反,它返回节点值,我不知道我做错了什么。任何帮助都将不胜感激。代码如下。 FinalExam。爪哇

    import java.io.*;
    import java.util.Scanner;


public class FinalExam
{
    SavingsAccount savings;
    static String name;
    static double balance;    

    public static void main (String[] args) throws NegativeAmountException, InsufficientFundsException {
    double amount = 0;
    SavingsAccount savings = null;
    Scanner keyboard = new Scanner(System.in);
   try
   {
       savings = new SavingsAccount("Penny Saved", 500.00, .05);
       System.out.println(savings);
   }
   catch(NegativeAmountException e)
   {
      System.out.println("NegativeAmountException: " + e.getMessage());
      System.exit(1);

   }

   System.out.println(savings);
   Scanner input = new Scanner(System.in);
   System.out.print("Enter your deposit amount: ");
   amount = keyboard.nextDouble();
   System.out.println(savings);
   System.out.println("Enter your withdrawal amount:  ");
   amount = keyboard.nextDouble();
   savings.postInterest();
   System.out.println(savings);
}
}

银行账户。爪哇

import java.util.InputMismatchException;
import java.util.Scanner; //Import for scanner


public class BankAccount {
  public String name;
  public double balance;     

  //set name and balance 
  // make sure balance is not negative
  // throw exception if balance is negative

  public BankAccount(String name, double balance)throws NegativeAmountException {
      if (balance < 0)
        throw new NegativeAmountException("Cannot create a BankAccount with a negative balance");


  }

    public BankAccount(String name)
            throws NegativeAmountException 
  {
    // set name and use 0 balance 
        name = null;
        balance = 0;
  }


  public void deposit(double amount) throws NegativeAmountException {
      if (amount < 0)
        throw new NegativeAmountException("Cannot deposit a negative amount: " + amount);
      balance = balance + amount;
  }

  public void withdraw(double amount) throws InsufficientFundsException, NegativeAmountException {
    if (amount > balance)
        throw new InsufficientFundsException("Cannot withdraw more than the current balance of this BankAccount");
    if (amount < 0)
        throw new NegativeAmountException("Cannot withdraw a negative amount: " + amount);
    balance = balance - amount;
  }

  public double getBalance() {

    return balance;

  }
  // print bank statement including customer name
 // and current account balance

  public void printStatement() {
      System.out.println("BankAccount owned by: " + name +  "    balance: $" + balance);
    }
}

储蓄账户。爪哇

public class SavingsAccount extends BankAccount
{
    double interest;

    public SavingsAccount(String name, double balance, double interest) throws NegativeAmountException {

      super(name, balance);


    }
    public double getInterest()
    {
    return interest;
    }
    public void postInterest() {

       balance = balance + ((balance * interest)/12);
    }

    public void printStatement() {
        super.printStatement();
      System.out.println("Account for " + name +  " Saved" + balance + "    balance: $" + "Current Interest Rate is" + interest);
      }
 }

我知道期末考试的最后部分。java是不完整的,但我正试图在讨论其他问题之前将声明打印出来。如有任何建议,将不胜感激


共 (4) 个答案

  1. # 1 楼答案

    在决赛中。java类打印SavingsAccount的值。java类,但您正在获取地址。这个id是因为在FinalExam。java类System.out.println(savings);,而不是像savings.printStatement();那样使用。也许我猜的没错

  2. # 2 楼答案

    I am stuck trying to get the the main class to return the values of the savings account class. Instead it returns the node value and I can't figure out what I a doing wrong.

    你的问题是你在打电话

    savings = new SavingsAccount("Penny Saved", 500.00, .05);
    System.out.println(savings);
    

    println自动对其参数调用toString方法。由于您没有使用@OverridetoString方法SavingsAccount,因此它会打印您所称的“节点值”

    你的SavingsAccount有一个方法

    public void printStatement()
    

    它似乎能打印出你想要的信息。所以叫它而不是println

  3. # 3 楼答案

    使用System.out.println(savings)方法打印对象时,它会在内部调用该对象上的toString()方法。您需要为类SavingsAccountBankAccount实现toString()方法

    对于SavingsAccount使用以下内容:

    @Override
    public String toString() {
      return "SavingsAccount{" +
          super.toString() +
          ", interest=" + interest +
          '}';
    }
    

    对于BankAccount,请使用以下内容:

    @Override
    public String toString() {
      return "BankAccount{" +
          "name='" + name + '\'' +
          ", balance=" + balance +
          '}';
    }
    

    当前您没有在类中设置实例变量。您正在检查负余额并抛出异常。 您还需要改进BankAccount类中的构造函数,如下所示:

    public BankAccount(String name, double balance)throws NegativeAmountException {
      if (balance < 0) {
        throw new NegativeAmountException("Cannot create a BankAccount with a negative balance");
      }
      this.name = name;
      this.balance = balance;
    }
    // Update the SavingsAccount class constructor as well
    public SavingsAccount(String name, double balance, double interest) throws NegativeAmountException {
      super(name, balance);
      this.interest = interest;  // this is missing in your code.
    }
    

    我观察到的另一件事是,您正在创建Scanner类的多个实例。在main方法中的每个地方都可以使用相同的keyboard实例。无需创建多个实例。其中一个是不必要的

    Scanner keyboard = new Scanner(System.in);
    Scanner input = new Scanner(System.in);
    
  4. # 4 楼答案

    答案已经给出,但BankAccount类构造函数中也存在问题,您没有将实例变量分配给构造函数参数,您只需检查约束

    public BankAccount(String name, double balance)throws NegativeAmountException {
          if (balance < 0)
            throw new NegativeAmountException("Cannot create a BankAccount with a negative balance");
          this.name = name;
          this.balance = balance;
    }
    

    public BankAccount(String name) {
        // set name and use 0 balance 
            this.name = name;
            this.balance = 0;   
    }
    

    谢谢:)