Calculating the circumference and area of a circle This simple program uses Scanner to obtain a radius from the user, then calculates and outputs the circumference and area.

Questions:


Is it customary in Java to place a function call inside of a println(), or is it better to put the returned value into a separate variable and then display that?
Should I consider implementing input-validation for practice as this is a toy program?
Am I correct in making the calculation functions static?




import java.util.Scanner;
import java.lang.Math;

public class Circle {

    public static double getCircumference(double radius) {
        return 2 * Math.PI * radius;
    }

    public static double getArea(double radius) {
        return Math.PI * radius * radius;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        double radius = sc.nextDouble();

        System.out.println("Circumference: ");
        System.out.println(getCircumference(radius));
        System.out.println("Area: ");
        System.out.println(getArea(radius));

        sc.close();
    }
}


Sample output:




5.5
Circumference: 
34.55751918948772
Area: 
95.03317777109123
Is it customary in Java to place a function call inside of a println(), or is it better to put the returned value into a separate variable and then display that?


This is something I would say is primarily opinion based. For this little app, I think it's fine to put the function method call inside a println.

When it comes to more complex things, it is usually better to do the calculation separately from the output (or most importantly logging), because you don't want your program behavior to change when you remove a System.out.println or logging statement.




  Should I consider implementing input-validation for practice as this is a toy program?


I don't see any need to use input-validation when you're using nextInt on the Scanner. Perhaps you'd want to avoid a negative size, and either throw an exception or show an error message to the user, but that's probably it.




  Am I correct in making the calculation functions static?


Yes, absolutely. I see no reason for why they should not be static. Given the usefulness of them, it is a good idea to have them public as well (which you have).



Feature-request: You're currently not prompting for any input which might make the user confused. A simple System.out.println("Enter radius:"); would be nice to have.



Overall, your code looks perfectly fine. A minor minor minor nitpick is that you're writing one space more than necessary here:

System.out.println("Circumference: ");
System.out.println("Area: ");


Another minor nitpick is that your Scanner variable could be named either scanner or input instead. (Even though sc is a quite normal variable name for a Scanner) #perfectionist-naming.