Results 1 to 9 of 9
  1. #1
    dj_ee3 is offline Member
    Join Date
    Feb 2011
    Posts
    6
    Rep Power
    0

    Default 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. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,352
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by dj_ee3 View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    NRitH is offline Senior Member
    Join Date
    Feb 2011
    Posts
    118
    Rep Power
    0

    Default

    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. #4
    dj_ee3 is offline Member
    Join Date
    Feb 2011
    Posts
    6
    Rep Power
    0

    Default

    Quote Originally Posted by NRitH View Post
    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. #5
    NRitH is offline Senior Member
    Join Date
    Feb 2011
    Posts
    118
    Rep Power
    0

    Default

    Quote Originally Posted by dj_ee3 View Post
    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. #6
    dj_ee3 is offline Member
    Join Date
    Feb 2011
    Posts
    6
    Rep Power
    0

    Default

    Quote Originally Posted by NRitH View Post
    What do you mean by a driver class? A command-line class?
    I mean like a class that executes this one.

  7. #7
    NRitH is offline Senior Member
    Join Date
    Feb 2011
    Posts
    118
    Rep Power
    0

    Default

    Quote Originally Posted by dj_ee3 View Post
    I mean like a class that executes this one.
    Now you're not even trying.

  8. #8
    dj_ee3 is offline Member
    Join Date
    Feb 2011
    Posts
    6
    Rep Power
    0

    Default

    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. #9
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    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?

Similar Threads

  1. Volume of Intersection
    By afifi in forum Advanced Java
    Replies: 0
    Last Post: 01-07-2011, 10:42 AM
  2. 3D Terrain on Sphere?
    By quddusaliquddus in forum Advanced Java
    Replies: 1
    Last Post: 06-22-2009, 11:50 PM
  3. Volume of AudioClip
    By Fedor in forum New To Java
    Replies: 0
    Last Post: 04-25-2009, 04:16 PM
  4. Help with sphere.java - not coming out correctly
    By JavaInLove in forum Java 2D
    Replies: 3
    Last Post: 03-17-2009, 08:38 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

Posting Permissions

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