22
\$\begingroup\$

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

Questions:

  1. 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?
  2. Should I consider implementing input-validation for practice as this is a toy program?
  3. 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
\$\endgroup\$
5
  • 2
    \$\begingroup\$ 1. Could you attach some sample output? 2. Why restrict the radius to int? The class name, Circle, suggests something more general. \$\endgroup\$ Commented Jul 16, 2014 at 21:31
  • 1
    \$\begingroup\$ @GraniteRobert: It doesn't have to be restricted; I just thought about an int at first. I can change that, along with providing sample output. \$\endgroup\$
    – Jamal
    Commented Jul 16, 2014 at 21:32
  • \$\begingroup\$ I wondered if you intended separate lines in the output. I thought I was misunderstanding my JAVA. \$\endgroup\$ Commented Jul 16, 2014 at 21:41
  • 1
    \$\begingroup\$ @GraniteRobert: I actually like them on separate lines, but I didn't take the time to find out how to do it. I suppose it's another minor thing anyway. \$\endgroup\$
    – Jamal
    Commented Jul 16, 2014 at 21:42
  • 1
    \$\begingroup\$ Java needs to catch up with the times and add a Tau constant. tauday.com/tau-manifesto \$\endgroup\$
    – RubberDuck
    Commented Jul 17, 2014 at 1:07

3 Answers 3

15
\$\begingroup\$

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.

\$\endgroup\$
1
  • 2
    \$\begingroup\$ Thanks for the nitpick. ;-) I forgot that this is displayed on separate lines, so that extra space is useless. \$\endgroup\$
    – Jamal
    Commented Jul 16, 2014 at 21:38
12
\$\begingroup\$

Creating a Circle object would probably be more idiomatic Java. The code would read a bit more fluently (for example, circle.getCircumference()). It also gives you a good place to validate that the radius is nonnegative.

import java.util.Scanner;

public class Circle {
    private final double radius;

    public Circle(double radius) {
         if (radius < 0) {
             throw new IllegalArgumentException();
         }
         this.radius = radius;
    }

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

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

    public static void main(String[] args) {
        try (Scanner sc = new Scanner(System.in)) {
            double radius = sc.nextDouble();
            Circle circle = new Circle(radius);

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

A couple of minor points as well:

  • import java.lang.Math is unnecessary, because java.lang.* are always available already.
  • Since Java 7, try-with-resources gives you a way to automatically close auto-closeable objects like Scanner.
  • Neither your code nor my code above checks that there is a double to read at all. If the input is garbage, then a java.util.InputMismatchException will be thrown, which is unhandled.
\$\endgroup\$
2
  • 1
    \$\begingroup\$ Ah, so I wasn't creating an actual Circle class. I should've known otherwise since this is sort of the same as in C++. It looks like I was actually using free functions. \$\endgroup\$
    – Jamal
    Commented Jul 17, 2014 at 0:44
  • 2
    \$\begingroup\$ I would guess that for someone with good education and experience, they did whatever they did because of convenient for the purpose at that moment. You are sadly mistaken if you think one person doesn't know how to do things like this. It will be object oriented when it needs to be object oriented and scalable. \$\endgroup\$
    – InformedA
    Commented Jul 17, 2014 at 7:13
8
\$\begingroup\$

I actually didn't like having each label and result displayed on separated lines, and instead wanted to have each value on separate lines like this:

5.5
Circumference: 34.55751918948772
Area: 95.03317777109123

I've discovered that print() accomplishes this, alongside println() for newlines.

I've also incorporated @Simon's suggestion regarding a user input prompt.

System.out.print("Input radius: ");

// ...

System.out.println();
System.out.print("Circumference = ");
System.out.println(circle.getCircumference());
System.out.print("Area = ");
System.out.print(circle.getArea());

With this, I get the following output (using the same radius):

Input radius: 5.5

Circumference = 34.55751918948772
Area = 95.03317777109123
\$\endgroup\$
1
  • \$\begingroup\$ I would use : instead of = from a "conventions" point of view. \$\endgroup\$
    – Toothbrush
    Commented Oct 8, 2015 at 14:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.