Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Calculating with Java

 
Shirl Diva
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a problem, in java, when it comes to calculating arithmetic problems that are more than two pronged. Why am I having so much trouble trying to multiply 2 * radius * diameter??
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know. What trouble are you having? What are the types of those variables?
 
Cain Silverbane
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hrmm... shouldnt have trouble, I wouldn't think... what exactly are you doing?
doing things such as x * y * z should be just as easy as x * y. Be sure to remember to have it all stored into something:
volume = length * height * width;
 
Shirl Diva
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was supposed to write a tiny program that prompted the user for a value, for the radius of a circle. The pgm then calcs the diameter, area, and circumference of the circle from that value. I had all of my variables in place. I tried to do something simple like circ = radius * pi * diameter. It would not take it. The error msg went something like this...I can't remember exactly. I can't find my notes either. It said something about me using 'double' int. I looked that up and used the (double) with it and it still wouold not work. I'm sorry guys but I was putting another query up - that's why I was not ready with this one. I am just loaded with problems
 
Shirl Diva
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found it!! The error msg is "possible loss of precisiion: double required int." It calculated radius = diameter * 2. When it came to the "Circumference = 2 * Math.PI * Radius - it was having no part of it.
 
Shirl Diva
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Cain Silverbane:
hrmm... shouldnt have trouble, I wouldn't think... what exactly are you doing?
doing things such as x * y * z should be just as easy as x * y. Be sure to remember to have it all stored into something:
volume = length * height * width;

The variables have been changed from string to integer and the code goes like this "Circumference = 2 * Math.PI * Radius;" The msg that I get is "possible loss of precision; double required int", I went back in and used the (double) and it still didn't accept it.
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try:
Circumference = 2.0d * Math.PI * Radius;
And make sure Circumference and Radius are both doubles.
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Math.PI is a double, so the result of your multiplication will be a double. You must store it in a double variable, not an int.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shirl,
Welcome to JavaRanch!
We ain't got many rules 'round these parts, but we do got one. Please change your display name to comply with The JavaRanch Naming Policy.
Thanks Pardner! Hope to see you 'round the Ranch!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic