Compile the following file: Notice that the class has no main() method, so you c
ID: 3762569 • Letter: C
Question
Compile the following file:
Notice that the class has no main() method, so you cannot run it by itself. Now compile and run the following file that makes use of the above class:
Next, make some changes to this test class to create another circle object, call it myCircle with radius 40. Then display its radius and area, and then increase its radius by 50% and display the radius and area once again. Notice the syntax difference when accessing (displaying) the value of variables radius, area and numberOfObjects.
Explanation / Answer
public class TestCircleWithStaticMembers {
/** Main method */
public static void main(String[] args) {
System.out.println("Before creating objects");
System.out.println("The number of Circle objects is " +
CircleWithStaticMembers.numberOfObjects);
// Create c1
CircleWithStaticMembers c1 = new CircleWithStaticMembers();
// Display c1 BEFORE c2 is created
System.out.println(" After creating c1");
System.out.println("c1: radius (" + c1.radius +
") and number of Circle objects (" +
c1.numberOfObjects + ")");
// Create c2
CircleWithStaticMembers c2 = new CircleWithStaticMembers(5);
// Modify c1
c1.radius = 9;
// Display c1 and c2 AFTER c2 was created
System.out.println(" After creating c2 and modifying c1");
System.out.println("c1: radius (" + c1.radius +
") and number of Circle objects (" +
c1.numberOfObjects + ")");
System.out.println("c2: radius (" + c2.radius +
") and number of Circle objects (" +
c2.numberOfObjects + ")");
// Create mycircle
CircleWithStaticMembers myCircle = new CircleWithStaticMembers(40);
System.out.println("myCircle: radius (" + c3.radius +
") and area of myCircle is : (" +
c3.getArea() + ")");
//increasing radius by 50%
myCircle.radius = myCircle.radius+0.5*(myCircle.radius);
System.out.println("myCircle: radius (" + c3.radius +
") and area of myCircle is : (" +
c3.getArea() + ")");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.