Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!

# More grains anyone?

Carol Murphy
village idiot
Bartender
Posts: 1202
Okay- I'm goin' nuts tryin' to figure this one out... I'm using a method from ********* named add(). add() takes a parameter of some type, which I pass in, and then it returns this+ val. No matter how I change the value passed in, the total is alway 1 or 2 more than it should be. Why is this? Should I just abandon add() and write my own method for finding the total number of grains, or perhaps I should just get some sleep! Any suggestions?

Johannes de Jong
tumbleweed
Bartender
Posts: 5089
No. Stick with it. I passed my assignment using the add method.
Is the paramater of the same type as the variable you add to ?

Richard Boren
Ranch Hand
Posts: 233
Sounds like you have an additional add that is not needed. It is probably at the beginning of your logic since it is only one or two numbers over the correct total, it would be really big if it were at the end. You may want to start total number of grains out at one instead of zero. Hope this gets you going again.
And by the way, get some sleep, there's all kinds of reports out saying we are not getting enough, sleep that is

Carol Murphy
village idiot
Bartender
Posts: 1202
My assumption is that the "this" is what is adding the extra amount. I am calling add on a ** named grains whose initial string value is "0". The parameter passed in is also a **. I am using the same method that I used to calculate the number of grains in exactly the same way, and passing that into add(). My understanding of "this" is very shaky, but if I am calling add() on grains, wouldn't the value of "this" be 0? And the value of the parameter passed into add() should be the sum of all the quantities of grain calculated previously. But the last 3 digits come up to 617, when they should be 615! Help me!!!

Johannes de Jong
tumbleweed
Bartender
Posts: 5089
Carol what I did is
I set my total = 0.
Then I add the power of 2 for the number of squares.
Square 1 = 2**0, Square 2 = 2**1, etc.
2**0 = PL/1 notation for power of
Look at the method of the class you are using.
by the way the instructors solution will nock your socks off

Johannes de Jong
tumbleweed
Bartender
Posts: 5089
by the way I'm sorry that I choose to tell you how I did it as apposed to answering your question. But in you attempt not to reveal any details. I have no clue what you are trying to say/ask anymore.
[This message has been edited by Johannes de Jong (edited May 22, 2001).]

Richard Boren
Ranch Hand
Posts: 233

the value of the parameter passed into add() should be the sum of all the
quantities of grain calculated previously.
Originally posted by Carol Murphy.

Carol, you are correct.
Have you tried writing a simple program using a for loop that only uses the add() to accumlate the total number of iterations? This may help you see if you are calling add() correctly or if you have a logic problem in your code.
My first reply is in reference to what I had done except I always got exactly 2 extra grains in my total number of grains, with the number of grains per square being correct. It was a very simple logic problem, but it took me some time to see it.
Remmember "Endeavor to persevere."

Carol Murphy
village idiot
Bartender
Posts: 1202
Johannes, thanks for the response. I have sent you a copy of the code in question. I don't understand the **PL comment in your post. Please clarify. The add() method takes a parameter of the same type as the class, and returns an amount that is equal to "this + val", val being the value passed in to add(). Other than that, I can't explain any better. Carol

Marilyn de Queiroz
Sheriff
Posts: 9063
12
"2**0 = PL/1 notation for power of"
Different languages have different notations for showing exponents. PL/1 is a different language that represents 2 to the power of zero with two **'s. (So does Fortran, I think). In Java you use the pow() method.

Johannes de Jong
tumbleweed
Bartender
Posts: 5089
Carol I received your e-mail. Sorry I slept late this morning. I printed it and will respond asap.
I thought EVERYBODY knows what PL/1 stands for . never assume wise lesson that I forget most of the time !!

[This message has been edited by Johannes de Jong (edited May 24, 2001).]

Johannes de Jong
tumbleweed
Bartender
Posts: 5089
e-mail on its way. I think you are doing great. Nice little program, sorry class .