Possible loss of precision
'night all,
Trying to get a dollars and cents answer to this. m1 represents a dollar value inputed while m2 represents cents. I thought dividing by 100.00 would've forced a double-type answer but I guess not. Any thoughts?
publicstaticint addMoney (int m1,int m2,double sum)
{
m1 = m1*100;
sum = (m1+m2)/100.00;
return sum;
System.out.println("The sum of the two amounts is " + sum);
}
The problem is that you're returning sum, a double, but the method is declared as returning an int. It has nothing to do with the 100.00.
Also, why are you making sum a parameter?
And do you realize that the print statement will never get executed, because it comes after the return statement?
Message was edited by:
paulcw
Never mind. I set the method to double and fixed it. My thanks to those who peeked. :)
paulcw, Sorry, guess we posted at the same time. I'll change the order to get the printout. Thanks for that. Pardon my ignorance, but why should I not make sum a parameter?
Actually the problem is not fixed.
public static int addMoney (int m1, int m2, double sum)
{
m1 = m1*100;
sum = (m1+m2)/100.00;
return sum;
// unreachable statement!
System.out.println("The sum of the two amounts is " + sum);
}
Looks like another "posted at the same time" situation
Message was edited by:
flounder
Sum shouldn't be a parameter becuase you are calculating it inside the method. So in order for your method to work you need to pass some value as a parameter then immediately throw it away and replace it with the calculated value. Your method is basically saying "Tell me the sum before I calculate it"
so doing this would be okay?
public static double addMoney (int m1, int m2)
{
m1 = m1*100;
sum = (m1+m2)/100;
System.out.println("The sum of the two amounts is " + sum);
return sum;
}
Did you try it? You will get a compiler error if sum is not a static variable.
Just compiled it. JCreator didn't say anything
Another thing, I would get rid of the print statement completely. The job of the method is to perform the calculation and return that result. Where you call that method (and receive the returned value) is where you should be doing the displaying (or pass the value onto another method which has the job of displaying).
so, in the driver class, I could have a statement that says:system.out.println("The sum of the two amounts is " + addMoney.sum); assuming that the addMoney method was in the class Money?
Congrats on the new star, flounder.
addMoney.sum
Is this how you call methods?
This is saying print sum which is a public instance variable in an Object that is referenced by the addMoney variable. ie
class Foo {
public int sum;
}
class Bar {
public static void main(String[] args) {
Foo addMoney = new addMoney();
System.out.println(addMoney.sum);
}
}
> Congrats on the new star, flounder.Thanks! My star's so bright I need to wear shades.
> Did you try it? You will get a compiler error if sum
> is not a static variable.
Just to clarify...this doesn't mean that sum has to be a static variable for this to be well-written code. It just means that as the method is written, sum would have a static field of the class, if this code were to compile.
Other, possibly superior alternatives would be to make sum a local variable.
And/or, to make this an instance method (and sum an instance field).
okay, want to get something straight...
(head bowed down in shame...)I am a newb. My syntax is poor. My coding is not strong.
so please be indulgent when I do not fully understand what you're trying to get across
Would it be fair to say then...
System.out.println("The sum of the two amounts is " + Money.addMoney);
if not, why not? This is acting on the fact that the above statement is in the driver class MoneyTest and that the addMoney method is in the Money class.
Money.addMoneyOnce again is this how you call a method? You should know it is not. For example you are calling the println method in the same line, so what is different?
M ethod/\/\ ethod()() ethod
Is that meant to be a mnemonic?
> Is that meant to be a mnemonic?Yeah. It's not really working is it? :(
okay, here's what I have so far. For the driver:
import java.util.Scanner;//This will allow input from the keyboard
public class MoneyTest
{
public static void main(String[] args)// Test the no-arg constructor, setter methods, and toString
{
Scanner keyboard = new Scanner(System.in); //
System.out.print("Enter the dollar amount: ");
int m1 = keyboard.nextInt( );
System.out.print("Enter the number of cents: ");
int m2 = keyboard.nextInt( );
Money sum = new Money();
Money diff = new Money();
Money calcul = new Money(m1, m2, sum, diff);
calcul.addMoney();
calcul.minusMoney();
System.out.println();
}
}
I know I get some things wrong since the compiler is giving me:
MoneyTest.java:15: cannot find symbol
symbol : constructor Money(int,int,Money,Money)
location: class Money
Money calcul = new Money(m1, m2, sum, diff);
^
MoneyTest.java:16: addMoney(int,int,double) in Money cannot be applied to ()
calcul.addMoney();
^
MoneyTest.java:17: minusMoney(int,int,double) in Money cannot be applied to ()
calcul.minusMoney();
As for the Money class, I have:
class Money
{
public int m1, m2, dollars, cents;//Dollars and cents in this amount of money.
public Money()//no argument constructor
{
dollars = 0;
cents = 0;
}
public Money(int dollars)//constructor for dollars and no cents
{
dollars = 0;
}
public Money(int dollar, int cents)//two parameter constructor
{
dollars = 0;
cents = 0;
}
public int getDollars()
{
return dollars;
}
public void setDollars(int dollars)
{
this.dollars = dollars;
}
public int getCents()
{
return cents;
}
public void setCents(int cents)
{
this.cents=cents;
}
public static double addMoney (int m1, int m2, double sum)
{
m1 = m1*100;
sum = (m1+m2)/100;
System.out.println("The sum of the two amounts is " + sum);
return sum;
}
public static double minusMoney (int m1, int m2, double diff)
{
m1 = m1*100;
diff = (m1-m2)/100;
System.out.println("The difference between the two amounts is " + diff);
return diff;
}
}
Parameters! Parameters! Parameters!When calling methods and constructors the actual parameters must match the formal parameters.methodName(actualParameters);public void methodName(formalParameters)
flounder,
sorry, I should have specified that the add and minus methods have to be static.Thanks for the parameter matching in both classes. I'm fixing that right now.
BTW, so as to clear up the constraints I'm operating under (other than my own ignorance), here's the assignment description. Note that I have not reached the equals and toString methods yet (explaining their absence).
Define a class named Money whose objects represent amounts of U.S. money. The class will have two instance variables of type int for the dollars and cents in the amount of money. Include a constructor with two parameters of type int for the dollars and cents, one with one constructor of type int for an amount of dollars with zero cents, and a noargument constructor. Include static methods add for addition and minus for subtraction of amounts of money. These methods will each have two parameters of type Money and returns a value of type Money. Include a reasonable set of accessor and mutator methods as well as the methods equals and toString. Write a test program for your class.
@stokerI strongly feel you should seek your programming notes first before posting...
Zsefva at 2007-7-21 22:32:22 >

OK, are you aware that your methods for addMoney and minusMoney are nowhere near what the assignment asked for. (and by the way, "subtractMoney" would be a better name than "minusMoney")
It said:
> Include static methods add for addition and minus for subtraction
> of amounts of money.
Personally, I think that these don't make sense as static methods, but that's what he asked for. OK, you got that part.
I also think that classes like this should have immutable fields, but again, this appears to be what he asked for.
> These methods will each have two parameters of type Money
> and returns a value of type Money.
Your code doesn't do this AT ALL. They return doubles, not Money objects, and you're passing them ints and doubles, not Money objects.
You might want to think about implementing toString and equals first. Possibly easier, and you'll get a better sense of what you're doing, hopefully.
Plus the one and two parameter constructors aren't doing what they should do either.
Looks like they may have been generated by an IDE.
And the Money object has no need for those m1, m2 fields.Those belong in the test class (if they belong anywhere).
So many mistakes.Sorry if it appears that we are picking on you but most of your errors are a result of your lack of understanding. You need to go back and review the basics.
It may help to remember something: computers are stupid. They only do what you tell them, and they have no idea what they're doing. So it becomes your responsibility to have a clear idea what you're trying to accomplish overall, and what each individual step is trying to accomplish, and how each step is accomplishing it.
The computer won't know what you're trying to do, so you can't write code that vaguely suggests it and hope the computer will pick up the slack.
At every line of code you ask yourself: what is this line supposed to do, and is it doing it, and how does it fit into the larger solution to whatever problem you're trying to solve? If you do this, you'll have a clearer idea whether a line of code is correct or not.