我正在进行Java编程分配,其中对象刚刚被引入该类。 我需要编写一个接受Money类型对象的方法,该对象具有美元和美分的属性。它需要提取美元和美分,以便将它们添加到Money类型的另一个对象中。
我是否正确地说我需要编写一个支持方法,其目的是从Money对象中提取美元和美分变量?
我想也许我需要将Money对象转换为String,以便我可以提取和操纵美元和美分。这是正确的吗?
我真的很困惑如何处理这个问题。我也不希望任何人给我答案,因为我什么都学不到。
非常感谢任何建议! :)
编辑: 我得到了以下课程:
package project6; // Test program for the Money class
public class Project6 {
public static void main(String[] args) {
test setMoney and toString
testSet ();
testAdd ();
testSubtract();
testEquals();
testLessThan();
}
private static void testSet () {
System.out.println ("Testing set and toString");
Money myMoney = new Money();
myMoney.setMoney(5, 75);
System.out.println (myMoney.toString()); // $5.75
myMoney.setMoney(75, 5);
System.out.println (myMoney.toString()); // $75.05
myMoney.setMoney(0, 5);
System.out.println (myMoney.toString()); // $0.05
myMoney.setMoney (-1, 5);
System.out.println (myMoney.toString()); // $0.00
myMoney.setMoney (1, -5);
System.out.println (myMoney.toString()); // $0.00
}
private static void testAdd () {
System.out.println ("Testing add");
Money total = new Money();
Money temp = new Money();
total.setMoney (0, 0);
for (int value = 1; value < 100; value += 15) {
temp.setMoney (1, value);
System.out.print(total + " + " + temp + " = ");
total.add(temp);
System.out.println (total.toString());
}
}
private static void testSubtract () {
System.out.println ("Testing subtract");
Money total = new Money();
Money temp = new Money();
total.setMoney (10, 10);
for (int value = 1; value < 100; value += 15) {
temp.setMoney (1, value);
System.out.print(total + " - " + temp + " = ");
total.subtract(temp);
System.out.println (total.toString());
}
}
private static void testEquals () {
System.out.println ("Testing equals");
Money wallet1 = new Money();
Money wallet2 = new Money();
wallet1.setMoney(7, 7);
wallet2.setMoney(7, 7);
System.out.println (wallet1 + " = " + wallet2 + " is " +
wallet1.equals(wallet2));
System.out.println (wallet1 + " = " + wallet2 + " is " +
wallet2.equals(wallet1));
wallet2.setMoney(7, 17);
System.out.println (wallet1 + " = " + wallet2 + " is " +
wallet1.equals(wallet2));
wallet2.setMoney(17, 1);
System.out.println (wallet1 + " = " + wallet2 + " is " +
wallet1.equals(wallet2));
}
private static void testLessThan () {
System.out.println ("Testing lessThan");
Money wallet1 = new Money();
Money wallet2 = new Money();
wallet1.setMoney(7, 7);
wallet2.setMoney(7, 7);
System.out.println (wallet1 + " < " + wallet2 + " is " +
wallet1.lessThan(wallet2));
wallet2.setMoney(17, 7);
System.out.println (wallet1 + " < " + wallet2 + " is " +
wallet1.lessThan(wallet2));
wallet2.setMoney(5, 7);
System.out.println (wallet1 + " < " + wallet2 + " is " +
wallet1.lessThan(wallet2));
wallet2.setMoney(7, 20);
System.out.println (wallet1 + " < " + wallet2 + " is " +
wallet1.lessThan(wallet2));
wallet2.setMoney(7, 4);
System.out.println (wallet1 + " < " + wallet2 + " is " +
wallet1.lessThan(wallet2));
}
}
/*
* Expected output
*
Testing set and toString
$5.75
$75.05
$0.05
$0.00
$0.00
*/
我需要使用以下实例方法编写Money类:
setMoney(int dollarsIn,int centsIn) - 将美元和美分设置为参数值。如果任一输入为负数,则将美元和美分设置为0.
add(Money moneyIn) - 将参数的美元和美分添加到当前对象。如果美分超过100,则相应调整美元和美分。
减法(Money moneyIn) - 从当前对象中减去参数如果美分低于0,则必须调整美元和美分。如果美元数量低于0,则将美元和美分都设置为0
boolean equals(Money moneyIn):如果参数的美元和美分都匹配当前对象的美元和美分,则返回true,否则返回false
boolean lessThan(Money moneyIn):如果当前对象表示的钱少于参数,则返回true。否则返回false
String toString():将对象的值作为格式为$ dd.cc的字符串返回
我想我会从setMoney和toString方法开始,看看我是否可以实例化Money对象并返回对象属性。所以在我的Project6类中,除了setMoney()和toString()方法之外,我已经注释掉了所有方法。 这是我到目前为止所提出的。
package project6;
public class Money {
private int dollars;
private int cents;
public void setMoney(int dollarsIn, int centsIn){
dollars = dollarsIn;
cents = centsIn;
}
public int getDollars(){
return dollars;
}
public int getCents(){
return cents;
}
public void add(Money moneyIn){
dollars = dollars + moneyIn.getDollars();
cents = cents + moneyIn.getCents();
dollars = dollars + (cents - (cents%100))/100; // roll over cents into dollars if greater than 100
}
public String toString(Money moneyIn){
return "$" + moneyIn.getDollars() + "." + moneyIn.getCents();
}
}
当我尝试从Project6类运行testSet()方法时,我得到以下输出:
Testing set and toString
project6.Money@7885a30c
project6.Money@7885a30c
project6.Money@7885a30c
project6.Money@7885a30c
project6.Money@7885a30c
而不是以下预期输出:
Testing set and toString
$5.75
$75.05
$0.05
$0.00
$0.00
EDIT2 - 所以在setMoney()方法调用中,我需要返回money对象吗?这或许可以解释我意想不到的输出?
编辑3 - 好吧,我已经提高了一点,我成功地设置并获得美元和美分。 我修改了Project6类以确认我正确设置并获取美元和美分变量,如下所示:
System.out.println ("Testing set and toString");
Money myMoney = new Money();
myMoney.setMoney(5, 75);
System.out.println("Dollars = " + myMoney.getDollars());
System.out.println("Cents = " + myMoney.getCents());
现在,当我运行程序时,它会输出
Dollars = 5
Cents = 75
根据我给出的指令,我需要编写一个名为toString()的方法,但是这样的方法在Java中是原生的(从我的研究出发)。 所以我想我必须将一个String传递给我已经预先格式化为$ dd.cc的toString()方法
编辑4 - 调用时的toString()方法采用以下格式:
myMoney.setMoney (1, -5);
System.out.println (myMoney.toString()); // $0.00
我想我刚想通了!我没有将对象作为参数传递!我只是调用getDollars()和getCents()方法! 我之前曾以为我只能通过尊重对象调用这些方法,但似乎是在方法调用期间完成的! 好极了!快乐的舞蹈!!
编辑6:
当我从Project6类调用以下方法时:
private static void testAdd () {
System.out.println ("Testing add");
Money total = new Money();
Money temp = new Money();
total.setMoney (0, 0);
for (int value = 1; value < 100; value += 15) {
temp.setMoney (1, value);
System.out.println("TOTAL = " + total);
System.out.println("TEMP = " + temp);
System.out.print(total + " + " + temp + " = ");
total.add(temp);
System.out.println (total.toString());
}
}
它进入for循环并打印出total和temp的值,然后添加它们。 第二次循环,它打印total的值,但不打印temp,并且不添加它们。 对于循环中的所有后续迭代,它不会打印total或temp的值。 我一直在逐步调试它,但我完全不知道为什么会发生这种情况。 任何人都可以提出任何关于我错过/做错的建议吗? 这是我目前Money类的代码:
package project6;
public class Money {
private int dollars;
private int cents;
public Money(){
}
public void setMoney(int dollarsIn, int centsIn){
dollars = dollarsIn;
cents = centsIn;
if (dollars < 0 || cents < 0){
dollars = 0;
cents = 0;
}
}
private int getDollars(){
return dollars;
}
private int getCents(){
return cents;
}
public void add(Money moneyIn){
dollars = dollars + moneyIn.getDollars();
cents = cents + moneyIn.getCents();
if (cents >= 100){
dollars = dollars + (cents - (cents%100))/100; // roll over cents into dollars if greater than 100
cents = (cents%100);
moneyIn.setMoney(dollars, cents);
}
}
public void subtract(Money moneyIn){
dollars = dollars - moneyIn.getDollars();
cents = cents - moneyIn.getCents();
if (cents < 0){
dollars = dollars + (cents + (cents%100)) / 100;
cents = 100 + cents;
}
if (dollars < 0){
dollars = 0;
cents = 0;
}
}
public boolean equals(Money moneyIn){
dollars = moneyIn.getDollars();
cents = moneyIn.getCents();
Money newMoney = new Money();
return (moneyIn.equals(newMoney));
}
public String toString(){
String output = "";
dollars = getDollars();
cents = getCents();
if (cents < 10)
output = "$" + dollars + ".0" + cents;
return output;
}
}
答案 0 :(得分:2)
是的,您必须为Money
课程添加一些基本方法。
我就是这样做的:
Money
构造函数接受两个参数,一个用于美元,一个用于美分String
中并且必须使用它)Money
个对象进行比较add(Money)
方法,在该方法中传递另一个Money
对象并将其值(美元/美分)添加到当前对象<强>编辑:强> 要处理构造函数收到超过100美分的情况,我们会这样做:
this.cents = cents%100;
this.dollars = dollars + ((cents-this.cents) / 100);
这应该是你所需要的一切。
答案 1 :(得分:1)
我想也许我需要将Money对象转换为String,以便我可以提取和操纵美元和美分。这是正确的吗?
对于简单的事情来说,这似乎太复杂了,你的另一个想法似乎更好。你可能想用“getter”和“setter”方法做些什么。我确定你已经了解了这些,如果不是,它们是用于获取和设置对象的私有字段的方法。
希望这有帮助!
答案 2 :(得分:0)
你可以在金钱等级上为美元和美分创建两个getter函数...... 在您目前的课程中,根据您的要求使用这些方法。 最后你可以在money类中覆盖toString()来调试和打印信息。 我希望这可以帮到你
答案 3 :(得分:0)
我想也许我需要将Money对象转换为String,以便我可以提取和操纵美元和美分。这是正确的吗?
您无需转换为String
。 添加是属于Money
本身的行为。您可以编写一个方法add(Money m)
,它会添加金额并为您提供一个新的Money
- 对象,并返回结果。
public Money add(Money m){
//Do the adding here
}
答案 4 :(得分:0)
你可以这样做,只要美元和美分是金钱类的变量
public Money addToNewMoney(Money money) {
Money newMoney = new Money();
newMoney.setDollars(money.getDollars());
newMoney.setDollars(money.getCents());
return newMoney;
}
稍后使用新的Money对象执行其他操作。
可能会有帮助
由于
答案 5 :(得分:0)
要扩展上一个答案,在Java中,类通常将其字段定义为私有对象,并为每个字段使用公共getter和setter方法。
在你的情况下,货币对象很可能有方法getDollars()和getCents()
因此,您的方法将money对象作为其参数
public double addMoney(Money obj) {
....
}
在方法内部,您只需调用getter方法获取美元和美分即可获得其值
public double addMoney(Money obj) {
int dollars = obj.getDollars();
double cents = obj.getCents();
....
}
如果你的课程中使用getter和setter方法计算了这笔资金的运行数量,它将会是这样的
public double addMoney(Money obj) {
int dollars = obj.getDollars();
double cents = obj.getCents();
this.setTotalDollars(this.getTotalDollars() + dollars);
this.setTotalCents(this.getTotalCents() + cents);
return this.getTotalDollars() + this.getTotalCents();
}
要添加到另一个对象,只需在每个对象上使用getDollars()和getCents()方法并将它们添加到一起
除非那是必需的返回类型
,否则不应在任何地方使用字符串答案 6 :(得分:0)
好的,请密切关注这一点,你的setMoney方法的逻辑是错误的,以获得预期的输出,你的方法必须看起来像这样。
public void setMoney (int dollarsln, int centsln){
if( centsln > 0 && dollarsln > 0 || centsln >= 0 && dollarsln >= 0){
dollars = dollarsln;
cents = centsln;
}
else{
dollars = 0;
cents = 0;
}
}
答案 7 :(得分:0)
我终于找到了问题! 在我的toString()方法中,我设置了一个输出字符串,用于当时的分数&lt; 10,但我没有提出他们是> = 10的情况。
修正了&amp;现在它按预期工作了!
这是一次非常好的学习经历,谢谢大家帮助我,非常感谢!
答案 8 :(得分:0)
由于我收到了来自这里的多个人的质量指示,我认为很难将一个人归功于帮助我解决这个问题。 这种情况的协议是什么?
我成功使用的最终代码是:
/*
* This Money class is for use with Project6 class.
* It provides public methods as follows:
* setMoney(int dollarsIn, centsIn) sets a Money objects attributes
* add(Money moneyIn) adds the dollars & cents of the parameter to the current object
* subtract(Money moneyIn) subtracts the parameter from the current object
* boolean equals(Money moneyIn) returns true if both dollars and cents of the parameter matches those of the current object, otherwise false
* boolean lessThan(Money moneyIn) return true if the current object represents less money than the parameter, otherwise false
* String toString() returns the values of the object formatted as $dd.cc
*
* It provides private methods as follows:
* int getDollars() returns dollar value of the current object
* int getCents() returns cents value of the current object
*
*/
package project6;
/**
* @author Ross Satchell
* @version 1.0
*/
public class Money {
private int dollars; /** variable to hold dollar value of object*/
private int cents; /** variable to hold cents value of object*/
public Money(){ /** constructor for object of type Money */
}
/** Set the Money object's attributes. If dollarsIn or centsIn are
* less than zero, it sets both dollars and cents to zero
* @param dollarsIn
* @param centsIn
*/
public void setMoney(int dollarsIn, int centsIn){
dollars = dollarsIn;
cents = centsIn;
if (dollars < 0 || cents < 0){
dollars = 0;
cents = 0;
}
}
/** Gets dollar attribute from Money object
* @return dollars
*/
private int getDollars(){
return dollars;
}
/** Gets cents attribute from object of type Money
* @return cents
*/
private int getCents(){
return cents;
}
/**
* Adds two Money objects together
* @param moneyIn
*/
public void add(Money moneyIn){
dollars = this.getDollars() + moneyIn.getDollars();
cents = this.getCents() + moneyIn.getCents();
if (cents >= 100){
dollars = dollars + (cents - (cents%100))/100; // roll over cents into dollars if greater than 100
cents = (cents%100);
this.setMoney(dollars, cents);
}
}
/**
* Subtracts a Money object from another Money object.
* If result is less than zero, this method sets both
* dollars and cents to zero
*
* @param moneyIn
*/
public void subtract(Money moneyIn){
dollars = dollars - moneyIn.getDollars();
cents = cents - moneyIn.getCents();
if (cents < 0){ // roll over cents if less than zero
dollars -= (cents/100) + 1;
cents = 100 + cents;
}
if (dollars < 0){
dollars = 0;
cents = 0;
}
}
/**
* Determines whether 2 money objects attributes are equal
* @param moneyIn
* @return boolean
*/
public boolean equals(Money moneyIn){
return (this.getCents() == moneyIn.getCents() && this.getDollars() == moneyIn.getDollars());
}
/**
* Determines whether the monetary attributes of an Money object are less than those of another Money object
* @param moneyIn
* @return boolean
*/
public boolean lessThan(Money moneyIn){
return (this.getDollars() * 100 + this.getCents()) < (moneyIn.getDollars() * 100 + moneyIn.getCents());
}
/**
* Converts Money objects attributes to a String
* @return String
*/
public String toString(){
String output;
if (getCents() < 10)
output = "$" + this.getDollars() + ".0" + this.getCents();
else output = "$" + this.getDollars() + "." + this.getCents();
return output;
}
}
答案 9 :(得分:-1)
package test;
public class BankAccount
{
private Money balance;
public BankAccount()
{
// start with zero balance
balance = new Money(0,0);
}
public void addMoneyToBalance(Money m)
{
balance.setDollars(m.getDollars());
balance.setCents(m.getCents());
}
public String accountBalanceToString()
{
return "$" + balance.getDollars() + "." + balance.getCents();
}
// inner class defines how money is held in memory
private static class Money
{
private int dollars;
private int cents;
public Money()
{}
// overloaded constructor, for a one-shot setup
public Money(int newDollars, int newCents)
{
dollars = newDollars;
cents = newCents;
}
public void setDollars(int newDollars)
{
dollars = newDollars;
}
public void setCents(int newCents)
{
cents = newCents;
}
public int getDollars()
{
return dollars;
}
public int getCents()
{
return cents;
}
}
public static void main(String[] args)
{
BankAccount myAccount = new BankAccount();
// prepare a deposit of $69.69
Money currentDeposit = new Money(69, 69);
myAccount.addMoneyToBalance(currentDeposit);
System.out.println(myAccount.accountBalanceToString());
}
}