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();

}

}

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:

Do you spot the error?

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. (:

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();

}

}

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 + ".");

}

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.

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?

I'm asked to do this:

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();

}

Re: How can I use a loop for volume of sphere?

When you create a method you should define all the types for its arguments. In your example you can't declare a method like double volume(int r, h). The h argument must have a type. And when you call the method like volume(r, h), and not volume(r) and volume(h). I think it it better for you to spent more time to get the basic of Java first. Jump to the following tutorials to begin: Trail: Learning the Java Language (The Java™ Tutorials)

Re: How can I use a loop for volume of sphere?

thank you I just figure out what was missing.