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
int
? The class name,Circle
, suggests something more general. \$\endgroup\$int
at first. I can change that, along with providing sample output. \$\endgroup\$