# How can I use a loop for volume of sphere?

• 03-25-2013, 11:53 PM
wheehoowaffles
How can I use a loop for volume of sphere?
I want to write a function that computes the volume of a sphere by using a loop to test the function when the sphere radius ranges from 1 to 100.
I want it to show every output when the radius ranges from 1 to 100. I'm having trouble doing that. I tried putting the loop in the test class but it says that variable r is not present. I would really appreciate it if you would help me so I can start doing similar problems like this one.
Is there a way that I can use set get method? if so can you give me a idea on how to start it.

Sphere
Code:

```class Sphere {         public Sphere() { }                 double volume ( int r ) {                         double v = 0;                         for (int i = 1; i <= r; i++) {                         v = (4 * Math.PI * Math.pow(r,3)) / 3;                         }                         return v;                 } }```

SphereTest
Code:

```class SphereTest {         public static void main(String [] args) {                 Sphere s = new Sphere();                 double v;         v = s.volume();         System.out.println();         System.out.println("The volume of the sphere is " + v + "." );         System.out.println();         } }```
• 03-26-2013, 01:23 AM
Re: How can I use a loop for volume of sphere?
Hello,

Your volume() method is declared like this:

Code:

`double volume ( int r )`
But you call it like this:

Code:

`v = s.volume();`
Do you spot the error?
• 03-26-2013, 03:34 AM
wheehoowaffles
Re: How can I use a loop for volume of sphere?
I know that already. I just want to know how I will be able to show all the outputs like the volume of a sphere when radius is 1, volume of a sphere when radius is 2, volume of a sphere when radius is 3, volume of a sphere when radius is 4, ... and so on. I jus found out the my teacher wants the loop on my test class. Thanks for helping anyway. (:
• 03-26-2013, 04:04 AM
wheehoowaffles
Re: How can I use a loop for volume of sphere?
I tried to print every output but keep on getting 0.0 or 4.18790.... for the volumes

I know i'm doing something wrong on my Sphere class but I don't know how to fix it.

Sphere
Code:

```class Sphere {         public Sphere() { }                 double volume () {                         double v = 0;                         double r = 1;    // I think this is what I'm doing wrong                         v = (4 * Math.PI * Math.pow(r,3)) / 3;                         return v;                 } }```

SphereTest
Code:

```class SphereTest {         public static void main(String [] args) {                 Sphere s = new Sphere();                 double v;         v = s.volume();         System.out.println();         for (int r = 1; r <= 100; r++) {         System.out.println("With radius of " + r + " the volume of the sphere is " + v + "." ); }         System.out.println();         } }```
• 03-26-2013, 04:15 AM
Re: How can I use a loop for volume of sphere?
The first declaration of your volume() method is the correct one. You should have a way to pass the radius to the volume() method. So the declaration will be:

Code:

`double volume(int r);`
Remove the statement double r = 1; from the volume() method.

Now to test it for radius from 1 to 100 you can call in a loop:

Code:

```for (int r = 1; r <= 100; r++) {     double v = s.volume(r);     System.out.println("With radius of " + r + " the volume of the sphere is " + v + "."); }```
• 03-26-2013, 04:29 AM
wheehoowaffles
Re: How can I use a loop for volume of sphere?
Thank you very much. This is going to help me do my other problems that are similar to this.
• 03-26-2013, 04:46 AM
wheehoowaffles
Re: How can I use a loop for volume of sphere?
Just one more question.

I can't have this right
Code:

`double volume ( int r,h)`
does this mean i need to do separate loops for the radius and height?

Write a function that computes the volume of a cone.
Use a loop to test your function wherein the
cone radius ranges from 1 to 100,
and the cone height ranges from 1 to 10.

This is what I have

Cone
Code:

```class Cone {         public Cone() { }                 double volume ( int r,h) {                         double v = 0;                         v = 1.0/3.0 * Math.PI * Math.pow(r,2) * h;                         return v;                 } }```

ConeTest
Code:

```class ConeTest {         public static void main(String [] args) {                 Cone c = new Cone();         System.out.println();         for (int r = 1; r <= 100; r++)         for (int h = 1; h <= 10; h++){         v = c.volume(r);         v = c.volume(h);                System.out.println("With radius of " + r + " and height of " + h + " ,the volume of the cone is " + v + ".");         }         System.out.println();         }```
• 03-26-2013, 05:16 AM