Question
Type and compile listing 9.6, page 340 (file Circle.java). Notice that this is a class with no main method, you cannot run it. 2. Type,
Type and compile listing 9.6, page 340 (file Circle.java). Notice that this is a class with no main method, you cannot run it.
2. Type, compile, and run listing 9.7, page 341 (file TestCircle.java). Observe the outputs and understand the displayed values. Notice that Listing 9.6 uses the static variable numberOfObjects. The value of this variable is modified through different circle objects.
3. Next, modify listing 9.7 to create two more circle objects, say c3 and c4. Modify the radius of object c3 to 20 and the radius of object c4 to 100. Then display the new objects (c3 and c4) by extending the code on lines 24 through 30. Again, notice the changes to the static variable value.
radius 5 s | radius ince variables belong to the instan d by all the instances of the same class. ces and have memory ry storage independent of one another. Static To declare a static variable or define a static method, put the modifier sta able or method declaration. The static variable numberofobjects and the getNumber0fobjects() can be declared as follows: tic in the vari- static method static int numberOfobjects static int getNumberObjectso return number0f0bjects: Constants in a class are shared by all objects of the class. Thus, constants should be declared as final static. For example, the constant PI in the Math class is defined as follows: final static double PI = 3.14159265358979323846; The new circle class is defined in Listing 9.6. LISTING 9.6 Circle.java (for Circlewithstat icMembers) 1 public class Circlet 2 The radius of the circle 3 double radius; 4 5 I The number of objects created static int number0fObjects = 0; /.. Construct a circle with radius 1 9 Circle) f 8 radius 1; numberOfobjects++; 10 12 13 14 15 /.. Construct a circle with a specified radius Circle(double newRadius) t radius newRadius; numberOfobjects++ 17 18 19 20 Return numberOfobjects/ 21 static int getNumberOfobjects) ( return numberOfobjects; 23 25 /.. Return the area of this circleStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started