Results 1 to 9 of 9
  1. #1
    Join Date
    Feb 2013
    Posts
    33
    Rep Power
    0

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

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    757
    Rep Power
    7

    Default 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 )
    But you call it like this:

    Java Code:
    v = s.volume();
    Do you spot the error?

  3. #3
    Join Date
    Feb 2013
    Posts
    33
    Rep Power
    0

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

  4. #4
    Join Date
    Feb 2013
    Posts
    33
    Rep Power
    0

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

  5. #5
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    757
    Rep Power
    7

    Default 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);
    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:

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

  6. #6
    Join Date
    Feb 2013
    Posts
    33
    Rep Power
    0

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

  7. #7
    Join Date
    Feb 2013
    Posts
    33
    Rep Power
    0

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

  8. #8
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    757
    Rep Power
    7

    Default 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)

  9. #9
    Join Date
    Feb 2013
    Posts
    33
    Rep Power
    0

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

    thank you I just figure out what was missing.

Similar Threads

  1. Help with volume of sphere using a loop. Please & thank you.
    By wheehoowaffles in forum New To Java
    Replies: 3
    Last Post: 03-25-2013, 01:41 AM
  2. I'm trying to display a sphere, but I get an error.
    By Astralogic in forum New To Java
    Replies: 10
    Last Post: 09-06-2012, 04:37 PM
  3. Volume of a sphere problem
    By dj_ee3 in forum New To Java
    Replies: 8
    Last Post: 02-14-2011, 02:10 AM
  4. 3D Terrain on Sphere?
    By quddusaliquddus in forum Advanced Java
    Replies: 1
    Last Post: 06-22-2009, 11:50 PM
  5. Please help. Trying to display a sphere.
    By aknbad23 in forum New To Java
    Replies: 6
    Last Post: 10-07-2008, 05:37 PM

Tags for this Thread

Posting Permissions

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