【问题标题】:odd output for an Account app in javajava中帐户应用程序的奇数输出
【发布时间】:2014-09-19 03:32:01
【问题描述】:

所以我有一个 java 编程 II 类的作业,要求我创建一个名为 Account 的类,该类将执行以下操作:

设计一个名为 Account 的类,其中包含:

数据成员:

一个名为 id 的 int 类型的私有数据字段,用于帐户;

帐户的双命名余额类型的私有数据字段;

名为 AnnualInterestRate 的 double 类型的私有数据字段 存储当前利率(假设所有账户都有相同的 利率);

一个名为 dateCreated 的 Date 类型的私有数据字段,用于存储 创建帐户的日期;

方法一个无参数的构造函数,它创建一个默认帐户并设置 以下数据成员为 0(零):id、balance 和 年利率;

创建具有指定 id 的帐户的构造函数和 初始余额;

id、balance 和 年利率;

dateCreated 的访问器方法

一个名为 getMonthlyInterestRate( ) 的方法,它返回每月 利率;

一个名为 getMonthlyInterest( ) 的方法,它返回每月 兴趣;

一个名为withdraw的方法,从 帐户;

一种名为 deposit 的方法,将指定的金额存入 帐户。

编写一个测试程序,创建一个带有帐户 ID 的 Account 对象 1122 美元,余额 20,000 美元,年利率 4.5%。 使用提款方式提取$2,500,使用存款方式提取 存入 3,000 美元,并打印余额、每月利息和 创建此帐户的日期。

教授已将测试应用程序提供给我们,我将在下面链接。所需要做的就是创建我拥有的 Account 类。一切都正确编译,但是当我运行测试应用程序时,我得到的输出很奇怪:

----jGRASP exec: java AccountTest


Current account values: 
Account@208e2fb5

Current account values: 
Account@208e2fb5

Current account values: 
Account@208e2fb5

我现在不知所措,我不知道该做什么或从这里去哪里。

测试应用:

public class AccountTest
{
   public static void main(String [] args)
   {

      Account a = new Account(1122, 20000.00);
      a.setAnnualInterestRate(4.5);

     System.out.println("\nCurrent account values: \n" + a);

      a.withdraw(2500.00);
      System.out.println("\nCurrent account values: \n" + a);

      a.deposit(3000.00);
      System.out.println("\nCurrent account values: \n" + a);
   }
}

我创建的帐户类:

import java.util.Date;
public class Account
{
   private int id;
   private double balance;
   private double annualInterestRate;
   private Date date = new Date();


   Account()
   {
      id = 0;
      balance = 0.0;
      annualInterestRate = 0.0;
   }

   Account(int newId, double bal)
   {
      id = newId;
      balance = bal;
   }

   public void setId(int newId)
   {
      id = newId;
   }

   public void setBalance( double bal)
   {
      balance = bal;
   }

   public void setAnnualInterestRate(double annualRate)
   {
      annualInterestRate = annualRate;
   }       

   public int getId()
   { 
      return id;
   }

   public double getBalance()
   {
      return balance;
   }

   public double getAnnualInterestRate()
   {
      return annualInterestRate;
   }         


   /*public void setDate(Date n)
   {
      dateCreated = n;
   }*/

   public java.util.Date getDate()
   {
      return date;
   }   

   double getMonthlyInterestRate()
   {
      return annualInterestRate/12;
   }

   double withdraw(double amount)
   {
      return balance -= amount;
   }

   double deposit(double amount)
   {
      return balance += amount;
   }    
}

为我的格式提前道歉。

【问题讨论】:

    标签: java jgrasp


    【解决方案1】:

    您正在打印对象的字符串表示形式。您不能期望 java 自己知道您想要打印什么。

    我建议您覆盖对象 Account 中的 toString 方法,该方法打印当前对象字段的详细信息/值。

    您可以检查如何覆盖 toString 方法here

    如果你只想打印一个值 比如

      System.out.println("\nCurrent account interest rate :" +  a.getAnnualInterestRate());
    

    更新您的评论。-:

     @Override
        public String toString() {
            return String.format("Here is detail of my object name : %s value : %d",stringValue,doubleValue);
        }
    

    【讨论】:

    • 我阅读了这篇文章并遵循了它的格式,但现在我收到以下错误:没有找到适合格式的方法(双)返回字符串.格式(id +余额); ^ 方法String.format(Locale,String,Object...) 不适用(实参double 无法通过方法调用转换转换为Locale) 方法String.format(String,Object...) 不适用(实参double 无法通过方法调用转换转换为字符串)我达到字符限制,所以我将代码粘贴到另一个注释中
    • 这是我在 Account 对象之后输入的代码:@Override public String toString() { return String.format(id + balance); }
    • @user3882666 你真的要检查java语言的文档/基础知识。检查String的format()方法documentation.String.format("number is %d",balance); //其中 %d 是实际值的占位符,而 balance 是例如该值的供应商/参数。如果你还不知道如何使用它,你甚至不必使用格式方法。使用普通的旧字符串连接。
    • 搞定了,我和教授谈过,他告诉我需要在tester 应用程序中填写信息才能显示所需的项目,即余额、月利率和日期帐户已创建。非常感谢你的帮助,虽然我很感激
    猜你喜欢
    • 2012-06-16
    • 1970-01-01
    • 1970-01-01
    • 2017-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-10
    • 1970-01-01
    相关资源
    最近更新 更多