Loading...
Loading...
Loading...
A geometry app stores a circle's radius. A negative radius makes no sense, so the app must refuse it and keep the old radius.
Write a class Circle with a private field radius (it starts at 0), a getter, a setter boolean setRadius(int r) that refuses a negative value, and double getArea(). main may change the radius only through the setter.
set r or show.set: RADIUS SET TO r, or INVALID RADIUS r, KEPT old. For show: RADIUS r AREA a, with the area printed with two digits after the decimal point.Example
Input
4 set 7 show set -3 show
Output
RADIUS SET TO 7 RADIUS 7 AREA 153.94 INVALID RADIUS -3, KEPT 7 RADIUS 7 AREA 153.94
-3 is refused, so the radius stays 7.
Try the problem first. Open one hint at a time.
Make the field private, so c.radius = -3; in main does not even compile.
The setter checks the value first and changes the field only when it is valid.
Return true or false so main can print the right message.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor