PDA

View Full Version : Who knows java?


BirdofPrey
02-01-2008, 07:28 PM
Because I am Stuck. (if you can't tell form the presence of the UNIX thread I am NOT haing a good time.

My file won't compile andthe error I get is

Calc.java:28: incompatible types
found : double
required: java.lang.Double
radius = Double.parseDouble (inputLine);
^
Calc.java:29: operator * cannot be applied to double,java.lang.Double
volume = 4 / 3 * Math.PI * radius *radius * radius;
^
Calc.java:30: operator * cannot be applied to double,java.lang.Double
surfaceArea = 4 * Math.PI * radius * radius;
^


inputLine is a string and radius is a floating point number.

I am trying to convert text input to a number to calculate the surface area an volume of a sphere hen output the results

Unentschieden
02-01-2008, 07:40 PM
What kind of text do you want to convert? I have yet to see a reasonable string -> int/double conversion.

BirdofPrey
02-01-2008, 07:45 PM
The program needs to convert whatever number I type in into a floating point number in order to use it in calculations

This is the prompt we were given:

Write a complete Java program that will read as input the radius of a sphere (a double). It will then calculate and output the volume and surface area of the sphere corresponding to that radius. Use the Java constant Math.PI as the value for pi. You will need the equations  to calculate volume and  to calculate the surface area of the sphere. Your program will read and process only one sphere per run. Format the program's output to two places to the right of the radix point.


Example:
You wish to calculate the values of a sphere of radius 3.5, the program provide the following output:
            Enter the radius:
            A sphere of radius: 3.50
            Has a volume of: 179.59
            Has a surface area of: 153.94



Nevermind.  I feel so stupid now.  I capitalized the statements that create the variables



I do have a questin though.

When i did the formula for Volume ha to put the 4/3 at the end of the expression to get the right results.

How come
4 / 3 * Math.PI * radus* radius * radius prints out erroneous numbers while
Math.PI * radus* radius * radius * 4 / 3 give the right answer and how do I make them both do the same thing

Mongoose
03-21-2008, 07:11 AM
You should use 4.0 and/or 3.0 for the 4/3 part. Integer division is going to give you a wrong answer.

Ursawarrior
03-21-2008, 10:13 AM
ahhh yes................ wait....

i only know action script 3.0 :P