# Thread: Volume of a sphere problem

1. Member
Join Date
Feb 2011
Posts
6
Rep Power
0

## 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.

Java 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!
Last edited by dj_ee3; 02-11-2011 at 05:40 PM.

2. 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.

Java 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

3. Senior Member
Join Date
Feb 2011
Posts
118
Rep Power
0
pow()'s not working because you're not passing anything to it.

Try

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

4. Member
Join Date
Feb 2011
Posts
6
Rep Power
0
Originally Posted by NRitH
pow()'s not working because you're not passing anything to it.

Try

Java 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?

5. Senior Member
Join Date
Feb 2011
Posts
118
Rep Power
0
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?

6. Member
Join Date
Feb 2011
Posts
6
Rep Power
0
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.

7. Senior Member
Join Date
Feb 2011
Posts
118
Rep Power
0
Originally Posted by dj_ee3
I mean like a class that executes this one.
Now you're not even trying.

8. Member
Join Date
Feb 2011
Posts
6
Rep Power
0
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?
Last edited by dj_ee3; 02-13-2011 at 05:15 AM.

9. 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:
Java Code:
`System.out.println(radiusOfASphere(3));`
Like so. Make sense?

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•