# Volume of a sphere problem

Printable View

• 02-11-2011, 06:37 PM
dj_ee3
Volume of a sphere problem
Hey, I got this problem for my school lab and I just don't get how to do it.
The problem is this.
"Write a method that takes in the radius of a sphere and returns it's volume the formula is v=4/2*3.14*r^3"
I don't really get how to do this r^3 business I know I have to use pow(double base, double exp) but it is not working for me.
Here is my code so far.

Code:

```public class math() {   public double pow(double base, double exponent)   {     int v = (4/3)*3.14*pow;     return v;     } }```
The lab is due monday and I hope someone helps me by than. Thank you for your help in advice!
• 02-11-2011, 06:45 PM
JosAH
Quote:

Originally Posted by dj_ee3
Hey, I got this problem for my school lab and I just don't get how to do it.
The problem is this.
"Write a method that takes in the radius of a sphere and returns it's volume the formula is v=4/2*3.14*r^3"
I don't really get how to do this r^3 business I know I have to use pow(double base, double exp) but it is not working for me.
Here is my code so far.

Code:

```public class math() {   public double pow(double base, double exponent)   {     int v = (4/3)*3.14*pow;     return v;     } }```
The lab is due monday and I hope someone helps me by than. Thank you for your help in advice!

'pow' is a method that takes two parameters (as you have described yourself). You are just writing it as 'pow', i.e. no parentheses, no parameters, no nothing. b.t.w. the pow method is implemented as a static method in the Math class, so it is Math.pow(r, 3)

kind regards,

Jos
• 02-11-2011, 06:46 PM
NRitH
pow()'s not working because you're not passing anything to it.

Try

Code:

```public double getVolumeOfSphere(double radius) {   return volume = (4/3) * Math.PI * Math.pow(radius, 3); }```
• 02-13-2011, 05:51 AM
dj_ee3
Quote:

Originally Posted by NRitH
pow()'s not working because you're not passing anything to it.

Try

Code:

```public double getVolumeOfSphere(double radius) {   return volume = (4/3) * Math.PI * Math.pow(radius, 3); }```

This code compiles but I can't get how to do the driver class for this one. Can you please help me?
• 02-13-2011, 05:55 AM
NRitH
Quote:

Originally Posted by dj_ee3
This code compiles but I can't get how to do the driver class for this one. Can you please help me?

What do you mean by a driver class? A command-line class?
• 02-13-2011, 05:58 AM
dj_ee3
Quote:

Originally Posted by NRitH
What do you mean by a driver class? A command-line class?

I mean like a class that executes this one.
• 02-13-2011, 06:01 AM
NRitH
Quote:

Originally Posted by dj_ee3
I mean like a class that executes this one.

Now you're not even trying.
• 02-13-2011, 06:05 AM
dj_ee3
I am sorry... So when we do a program in my Java Class we do two types of classes the container classes which contain instance variables to hold the relevant data - getters and setters - methods to support moving data in/out of class (parsing, translation) - limited computations and such
And the driver classes that is a main method that drives the execution of the overall task (aka entry point for execution) - calls to static methods, as with procedural programming - instances of container class objects to hold different data (may be organized in other data structures, e.g. arrays; manipulated to solve overall task)

or simply how to I execute that return volume statement so I can see a result in a terminal window?
• 02-14-2011, 03:10 AM
quad64bit
I understand what you are talking about in regards to a Driver class, many beginner java classes are taught this way. The community has described how to use Math.pow(), are you still stuck with that?

To see the result of a function, you can simply print it:
Code:

`System.out.println(radiusOfASphere(3));`
Like so. Make sense?