• 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

Assignment 2.2 [Grains] (Previously 1.6)

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've fallen into the trap of being able to half do this assignment! I can get to the point where I have the following printed to screen:
square 29: 1073741824 grains
square 30: -2147483648 grains
Then my int just ins't big enough for any more! I've had a scoot around the Java Sun site (as suggested in the question) and found something called BigInteger (sounds good to me), but can't get a grip on how to implement it.
At present my method for working out the no. of grains is:

Can anyone help?
Thanks
R
[This message has been edited by Rachel Hirstwood (edited January 12, 2001).]
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A double type variable should work.
Pat B.
 
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually the double data type doesn't have the precision that this assignment requires.


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!


 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is how I initialized a variable in the ********* class.

Beware the ******* math is done differently.
***** is the stuff I deleted so too much wouldn't be given away -- Paul
[This message has been edited by Paul Wheaton (edited January 13, 2001).]
 
Daniel Olson
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Carl Trusiak:
Actually the double data type doesn't have the precision that this assignment requires.


Please explain why. I did a version of this assignment using a double and a method from the com.javaranch.common package to format the numbers, and it seemed to work fine for me. (If I did the assignment correctly.) A double can have values up to 1.7E308. Is it because it has 15 digit accuracy?

 
Carl Trusiak
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's right, You can format it to show more but beyound the 15th position, you can't rely on it to be accurate.
 
Trailboss
Posts: 23773
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do the math yourself. By hand. See what number you come up with. And compare that to the results you get with Double.
 
paul wheaton
Trailboss
Posts: 23773
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Remember folks, we can talk about the issues here, but let's not give away too much of the solution. That would rob others of an education.
The purpose of this assignment is spend a good half hour looking through the massive list of all the classes to get a vague idea of what is out there.
 
Daniel Olson
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, Sheriff. I was trying to be careful not to put too much code.
Carl is right. I tried the Grains assignment with the ******* class instead of using a double, and, with the ****** class, the answer was more accurate once the numbers got beyond the 15th position.
****** is the secret class that we all have to search for.
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Daniel Olson:
Please explain why. I did a version of this assignment using a double and a method from the com.javaranch.common package to format the numbers, and it seemed to work fine for me. (If I did the assignment correctly.) A double can have values up to 1.7E308. Is it because it has 15 digit accuracy?


You'll notice also that in the example on the assignment page, there are no decimal points in the output.

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all...
I believe I have found the class to use in the API but I have no idea of how to use it. I've written the program using double values and I was happy with that until I found all these posts.
Where can I learn to translate the API info into my programs?
Thanks!
 
Pat Barrett
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you look up the class in question here it should provide a breakdown of the associated methods that can get you what you want. It took a while for this to sink in, but the answers are there.
Good luck,
Pat B.
 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I thought I had this one right when it compiled with no errors, but when I run it I get a NumberFormatException on the line that goes:
xxxxxxx stringName = new xxxxxxxx( str );
In the API, one of the accessors is a String which is what I passed.
Please help...I'm lost!
[This message has been edited by David Junta (edited March 10, 2001).]
 
Marilyn de Queiroz
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The String that you pass in should look like a number.
xxxxxxx stringName = new xxxxxxxx( "2" );
for example.
 
David Junta
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmmm...now I'm stumped. Each iteration of the loop is going to make the String different, so how do I get it to "look like a number"? I thought maybe using...
xxxxxxx stringName = new xxxxxxxx( str.toString() );
...
would work but no dice....still a NumberFormatException.
The thing that confuses me is that in the "Say" assignment, I passed String names (not something that "looked like a number" like "2") into methods that took a String argument. Is this different because you're invoking a new Object?
Someone, please show me the light while I still have some hair left
 
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
David, I don't know if I can answer your question, but there are other mehtods in the xxxxx class that will help you with the problem of


Each iteration of the loop is going to make the String different, so how do I get it to "look like a number"?


Basically, it needs to "look like a number" once, then use other methods in the xxxxx class.
Bill
 
David Junta
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, Bill.
I got it...and it was soooooooo obvious in hindsight.
reply
    Bookmark Topic Watch Topic
  • New Topic