Lesson 10 - Activity 2 - Can you create a method to calculate a sphere's volume?

Time: 15 minutes

Purpose: This activity helps you get familiar with creating your own method

Introduction: You’ve created methods under guidance, now design and build your own method

Task: Create a method to calculate the volume of a sphere,

image

Think about the variable types you’ll need, the inputs and the outputs.

public static double calculateVolumeSphere(double sphereRadius){

    double sphereVolume;
    sphereVolume = ((4 * Math.PI) * (sphereRadius * sphereRadius * sphereRadius))/3;

    return sphereVolume;
}

Love it - that will work. To tidy up you might want to just make it,

sphereVolume = 4 * Math.PI * sphereRadius * sphereRadius * sphereRadius / 3;

You don’t need brackets as there’s no mixing of multiplication and adding/subtracting. That’s always where arithmetic precedence rules get hairy and we typically use brackets.