1 | import java.text.DecimalFormat; |
2 | /** |
3 | * class for circle computations |
4 | */ |
5 | public class Circle |
6 | { |
7 | private double radius; |
8 | private double area; |
9 | |
10 | |
11 | /* construct a circle. |
12 | * @param radius the radius of the circle if known, otherwise 0 |
13 | * @param area the area of the circle if known, otherwise 0 |
14 | */ |
15 | public Circle(double radius, double area) |
16 | { |
17 | if (radius == 0) |
18 | { |
19 | this.area = area; |
20 | this.radius = computeRadius (area); |
21 | |
22 | } |
23 | else if (area == 0) |
24 | { |
25 | this.radius = radius; |
26 | this.area = computeArea ( radius); |
27 | } |
28 | } |
29 | |
30 | private double computeRadius (double area) |
31 | { |
32 | if (area >= 0.0) |
33 | { |
34 | return Math.sqrt (area / Math.PI); |
35 | } |
36 | else |
37 | { |
38 | return 0; |
39 | } |
40 | |
41 | } |
42 | |
43 | private double computeArea (double radius) |
44 | { |
45 | return Math.PI * radius * radius; |
46 | } |
47 | |
48 | // Gets a double value from a text field. If the text in the field |
49 | // is a legal integer, converts and returns it as a double. If not, |
50 | // pops up an error message and returns zero instead. Blank fields |
51 | // are interpreted as zeros. |
52 | // Parameters: |
53 | // field: the field containing the value |
54 | // fieldName: the name of the field, for error messages |
55 | public static double toDouble(String field) |
56 | { |
57 | // the contents of the text field as a string, minus any leading |
58 | // or trailing blanks |
59 | String fieldContents = field.trim(); |
60 | if (fieldContents.length() == 0) |
61 | return 0; |
62 | |
63 | // Calling Double.parseDouble will raise a NumberFormatException if |
64 | // the parameter is not in double format. We will catch that |
65 | // exception and report an error, instead of letting the program |
66 | // end. |
67 | try |
68 | { |
69 | return Double.parseDouble(fieldContents); |
70 | } |
71 | catch (NumberFormatException ex) { return 0; } |
72 | } |
73 | |
74 | // Formatting pattern for displaying the area with a limited |
75 | // number of decimal digits |
76 | private DecimalFormat fixedFormat = new DecimalFormat("0.0##"); |
77 | // Another pattern for large numbers, using scientific notation |
78 | private DecimalFormat sciFormat = new DecimalFormat("0.0##E0"); |
79 | |
80 | public String getFormattedRadius() |
81 | { |
82 | return fixedFormat.format(radius); |
83 | } |
84 | |
85 | // convert double to String, using formatting pattern to |
86 | // round to 3 digits. If the result won't fit into the area |
87 | // field, it's because the number is large and there are |
88 | // too many digits before the decimal point. Use the alternate |
89 | // pattern with scientific notation. |
90 | public String getFormattedArea() |
91 | { |
92 | String areaString = fixedFormat.format(area); |
93 | if (areaString.length() > 10) |
94 | { |
95 | areaString = sciFormat.format(area); |
96 | } |
97 | return areaString; |
98 | } |
99 | |
100 | public String toString() |
101 | { |
102 | return " " + radius + " : " + area; |
103 | } |
104 | } |
105 | |