• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

doubleValue++

 
Ranch Hand
Posts: 290
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can't seem to get grainCount to increment. The only time I've recieved a change is when I move grainCount below double grainCount = 1; but instead of it printing
square 1: 1 grain
square 2: 2 grain
I get
square 1: 4 grains
square 2: 4 grains
when I have the increment at the bottom grainCount remains at 1

Thanks,

Joe
[This message has been edited by Joseph Russell (edited January 11, 2001).]
[This message has been edited by Angela Poynton (edited January 12, 2001).]
 
Joseph Russell
Ranch Hand
Posts: 290
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just thought I'd mention that I've tried other things like moving the statement before my if/else statement (I can't see why I would need too). I've changed it to ++grainCount and I've also tried grainCount = grainCount + 1; and grainCount += 1; I've attempted to make grainCount an int and cast it to a double that doesn't work with the Math.pow method. and a few other things that have been unsuccessful.
I'm stumped...
Still
Joe
 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Perhaps the problem is that you are passing 1.0 as the first param to pow? 1.0 raised to the power of anything is going to be 1.0.
P.S. Looks like you are trying to fill a chessboard with grain, with 1 grain in the first square, 2 in the next, 4 in the next, etc.
P.P.S. Try taking this line out of the loop and putting it above the loop:

That is what is resetting grainCount to 1 in every loop iteration.
P.P.P.S. It's generally a bad idea to compare double primitives with == because of the precision and rounding and stuff. In other words, grainCount might actually be 1.00000000000000000001 and thus not == to 1.0.
[This message has been edited by Susan Hoover (edited January 11, 2001).]
 
Joseph Russell
Ranch Hand
Posts: 290
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank ya, Thank ya, Thank ya...
Now my only question would be what method in the Math API was I supposed to use? The problem said that this would be the bulk of the assignment. That's why I thought I need something like Math.pow. Anyways, this is what I've come up with...

Joe
[This message has been edited by Joseph Russell (edited January 11, 2001).]
[This message has been edited by Angela Poynton (edited January 12, 2001).]
 
Bartender
Posts: 783
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Joseph,
First thing you need to learn when you start working is to be clear on your requirements or at least to be as clear as possible and to give as much detail as possible. This is also true when you ask for help. The more information you can give us regarding your problem, the better our answers.
Now you said, "The problem said that this would be the bulk of the assignment." What's the problem? What's the assignment?
-Peter
 
Susan Hoover
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Math.pow will work for what you need to do, but before writing the code, you should probably think about what it is you're trying to do. In the real world we have things called "business rules" that determine the expected behavior of the system.
If I have deduced the nature of your assignment correctly, this is the problem where the king/pharoah/supreme Poo-Bah tells someone/is told to fill a chessboard with grains of rice, starting with one grain in the first square and doubling the number in each square.
One of your "business rules" is, I am guessing, that you can't have a fractional grain of rice. So, you should probably have some sort of integral value that stores the number of grains of rice. You have to make sure it's big enough to hold the numbers you need to hold, so you will have to evaluate which of the integral primitives you need (byte, char, short, int, long).
Then, if the problem really does just require doubling, then maybe you can just multiply by 2 in your loop. Or, if you have to use Math.pow, then you will have to figure out how to change the double value that Math.pow returns into your integral value so you can save it. This will require the use of casting.
Since this sounds like a homework assignment, I don't want to solve the whole problem for you, but it sounds like you need to step back and look at the algorithm you're trying to use before you code it.
Susan
 
Joseph Russell
Ranch Hand
Posts: 290
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry if I wasn't clear enough. I'm going through the JavaRanch Assignments. This one is Assignment 2.2 Grains. Here's the assingment: There once was a wise servant who saved the life of a princess. The king promised to pay whatever the servant could dream up. Knowing that the king loved chess, the servant told the king he would like to have grains of wheat. One grain on the first square of a chess board. Two grains on the next. Four on the third, and so on. To complete this assignment, you will need to find a class in the Java standard library that you have not used yet. The search for this class is the real meat of this assignment!
I spent time looking for a method and got sidetracked. I think that casting is what was supposed to be the meat of the assignment.
Thanks for all your help.
 
Ranch Hand
Posts: 3143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tut tut Joseph ... I'm going to have to remove your code examples because as you know others are going through the cattle drive too ... and we don't want them all copying your version do we?
Anyway i hope your question was answered but please remember not to post your ansers to cattle drive assignments in the future.
 
Joseph Russell
Ranch Hand
Posts: 290
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I suppose your right...Thanks for not throwing me in jail for the night Next time I'll be a lot more general.
Joe
 
Peter Tran
Bartender
Posts: 783
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Joseph,
You can be specific and detail out your questions. I believe the sheriffs don't want you to show your code. Asking detail questions is okay. To save your fingers, you can just link the assignment.
-Peter
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic