File tree Expand file tree Collapse file tree
core-java/src/main/java/com/baeldung/area/circle Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ package com .baeldung .area .circle ;
2+
3+ public class Circle {
4+
5+ private double radius ;
6+
7+ public Circle (double radius ) {
8+ this .radius = radius ;
9+ }
10+
11+ public double getRadius () {
12+ return radius ;
13+ }
14+
15+ public void setRadius (double radius ) {
16+ this .radius = radius ;
17+ }
18+
19+ public double getArea () {
20+ return calculateArea ();
21+ }
22+
23+ private double calculateArea () {
24+ return radius * radius * Math .PI ;
25+ }
26+
27+ public String toString () {
28+ return "The area of the circle [radius = " + radius + "]: " + calculateArea ();
29+ }
30+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .area .circle ;
2+
3+ import java .util .InputMismatchException ;
4+ import java .util .Scanner ;
5+
6+ public class CircleArea {
7+
8+ public static void main (String [] args ) {
9+ if (args .length > 0 ) {
10+ try {
11+ double radius = Double .parseDouble (args [0 ]);
12+ calculateArea (radius );
13+ } catch (NumberFormatException nfe ) {
14+ System .out .println ("Invalid value for radius" );
15+ System .exit (0 );
16+ }
17+ }
18+
19+ try (Scanner scanner = new Scanner (System .in )) {
20+ System .out .println ("Please enter radius value: " );
21+ double radius = scanner .nextDouble ();
22+ calculateArea (radius );
23+ } catch (InputMismatchException e ) {
24+ System .out .println ("Invalid value for radius" );
25+ System .exit (0 );
26+ }
27+
28+ Circle circle = new Circle (7 );
29+ System .out .println (circle );
30+ }
31+
32+ private static void calculateArea (double radius ) {
33+ double area = radius * radius * Math .PI ;
34+ System .out .println ("The area of the circle [radius = " + radius + "]: " + area );
35+ }
36+ }
You can’t perform that action at this time.
0 commit comments