Reputation:
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?
Upvotes: 0
Views: 1118
Reputation: 201467
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);
}
Upvotes: 3