Results 1 to 9 of 9
 03252013, 10:53 PM #1Member
 Join Date
 Feb 2013
 Posts
 33
 Rep Power
 0
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
Java 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
Java 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(); } }
 03262013, 12:23 AM #2Senior Member
 Join Date
 Jun 2007
 Location
 Bali, Indonesia
 Posts
 762
 Rep Power
 13
Re: How can I use a loop for volume of sphere?
Hello,
Your volume() method is declared like this:
Java Code:double volume ( int r )
Java Code:v = s.volume();
Website: Learn Java by Examples
 03262013, 02:34 AM #3Member
 Join Date
 Feb 2013
 Posts
 33
 Rep Power
 0
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. (:
 03262013, 03:04 AM #4Member
 Join Date
 Feb 2013
 Posts
 33
 Rep Power
 0
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
Java 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
Java 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(); } }
 03262013, 03:15 AM #5Senior Member
 Join Date
 Jun 2007
 Location
 Bali, Indonesia
 Posts
 762
 Rep Power
 13
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:
Java Code:double volume(int r);
Now to test it for radius from 1 to 100 you can call in a loop:
Java 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 + "."); }
Website: Learn Java by Examples
 03262013, 03:29 AM #6Member
 Join Date
 Feb 2013
 Posts
 33
 Rep Power
 0
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.
 03262013, 03:46 AM #7Member
 Join Date
 Feb 2013
 Posts
 33
 Rep Power
 0
Re: How can I use a loop for volume of sphere?
Just one more question.
I can't have this right
Java Code:double volume ( int r,h)
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
Java 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
Java 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(); }
 03262013, 04:16 AM #8Senior Member
 Join Date
 Jun 2007
 Location
 Bali, Indonesia
 Posts
 762
 Rep Power
 13
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)
Website: Learn Java by Examples
 03272013, 10:49 PM #9Member
 Join Date
 Feb 2013
 Posts
 33
 Rep Power
 0
Similar Threads

Help with volume of sphere using a loop. Please & thank you.
By wheehoowaffles in forum New To JavaReplies: 3Last Post: 03252013, 01:41 AM 
I'm trying to display a sphere, but I get an error.
By Astralogic in forum New To JavaReplies: 10Last Post: 09062012, 04:37 PM 
Volume of a sphere problem
By dj_ee3 in forum New To JavaReplies: 8Last Post: 02142011, 02:10 AM 
3D Terrain on Sphere?
By quddusaliquddus in forum Advanced JavaReplies: 1Last Post: 06222009, 11:50 PM 
Please help. Trying to display a sphere.
By aknbad23 in forum New To JavaReplies: 6Last Post: 10072008, 05:37 PM
Bookmarks