0
public class Part1
{
    public static void main(String args []) 
    { 
        DecimalFormat df=new DecimalFormat("###.##");
        double v;
        Scanner sc = new Scanner( System.in );
        double radius;
        System.out.println( "Enter radius of the basketball: " ); 
        radius = sc.nextDouble(); 
        System.out.println("The volume of the basketball is " +df.format(v));
    }
    public static int volume (int v)
    {   
        v= ( 4.0 / 3.0 ) * Math.PI * Math.pow( radius, 3 ); 
    }
}

Basically, i have to let the user input the radius of the basketball and you have to calculate the volume of the basketball. the codes works perfectly, but i don't know how to do it in function method?

  • 1
    It's unclear what you mean by "how to do it in procedure method". If your code works, what problem are you trying to solve? – Wyzard Oct 29 '14 at 02:31
  • Please explain your definition of 'procedure method'? – Jason Oct 29 '14 at 02:31
  • public static double volume (double radius) { return (4.0/3) * Math.PI * radius * radius * radius; } – Prashant Ghimire Oct 29 '14 at 02:32
  • @Jason i am sorry i actually meant was function method. Basically function method is a method in which it only returns one value. Therefore, you call the function in the main method –  Oct 29 '14 at 02:40

1 Answers1

3

I'm fairly certain you need to return double and pass the radius as a double to your volume method. You also need to call the function and get the value. You should try and restrict the lexical scope of your variables. Something like,

public static void main(String args[]) {
    DecimalFormat df = new DecimalFormat("###.##");
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter radius of the basketball: ");
    double radius = sc.nextDouble();
    double v = volume(radius);
    System.out.println("The volume of the basketball is " + df.format(v));
}

public static double volume(double radius) {
    return (4.0 / 3.0) * Math.PI * Math.pow(radius, 3);
}
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • Also, i meant to say was Function Method, ur code is in function method right? Also can u explain to my why you did this double v = volume(radius);? –  Oct 29 '14 at 02:38