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](https://us1.discourse-cdn.com/flex020/uploads/ministryoftesting/original/2X/3/3cd6149c0dcf06a0793f29e19f98e9bf561864a1.png)
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.