Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Type, compile, and run listing 9.1, page 324 (file TestSimpleCircle.java). No

ID: 3689898 • Letter: 1

Question

1. Type, compile, and run listing 9.1, page 324 (file TestSimpleCircle.java). Notice that this program has 2 classes in the same file (Class TestSimpleCircle and Class SimpleCircle). Understand how objects circle1, circle2, and circle3 are created and manipulated.

2. Now, type, compile, and run listing 9.2, page 326 (file SimpleCircle.java). Notice that the main method of this class is acting as the test class in listing 9.1. Next, make some changes to this class to create 2 more objects, say circle4 and circle5. Print out the radius and area of each circle object similar to other circle objects.

CODE FOR SIMPLECIRCLE: public class SimpleCircle { /** Main method */ public static void main(String[] args) { // Create a circle with radius 1 SimpleCircle circle1 = new SimpleCircle(); System.out.println("The area of the circle of radius " + circle1.radius + " is " + circle1.getArea()); // Create a circle with radius 25 SimpleCircle circle2 = new SimpleCircle(25); System.out.println("The area of the circle of radius " + circle2.radius + " is " + circle2.getArea()); // Create a circle with radius 125 SimpleCircle circle3 = new SimpleCircle(125); System.out.println("The area of the circle of radius " + circle3.radius + " is " + circle3.getArea()); // Modify circle radius circle2.radius = 100; System.out.println("The area of the circle of radius " + circle2.radius + " is " + circle2.getArea()); } double radius; /** Construct a circle with radius 1 */ SimpleCircle() { radius = 1; } /** Construct a circle with a specified radius */ SimpleCircle(double newRadius) { radius = newRadius; } /** Return the area of this circle */ double getArea() { return radius * radius * Math.PI; } /** Return the perimeter of this circle */ double getPerimeter() { return 2 * radius * Math.PI; } /** Set a new radius for this circle */ void setRadius(double newRadius) { radius = newRadius; } }

Explanation / Answer

public class SimpleCircle
{
   /** Main method */
  
   public static void main(String[] args)
   {
       // Create a circle with radius 1
      
       SimpleCircle circle1 = new SimpleCircle();       
       System.out.println("The area of the circle of radius " + circle1.radius + " is " + circle1.getArea());
  
       // Create a circle with radius 25
      
       SimpleCircle circle2 = new SimpleCircle(25);       
       System.out.println("The area of the circle of radius " + circle2.radius + " is " + circle2.getArea());
  
       // Create a circle with radius 125
      
       SimpleCircle circle3 = new SimpleCircle(125);               
       System.out.println("The area of the circle of radius " + circle3.radius + " is " + circle3.getArea());
  
       // Modify circle radius
      
       circle2.radius = 100;
      
       System.out.println("The area of the circle of radius " + circle2.radius + " is " + circle2.getArea());
      
       // Circle4 object created

       SimpleCircle circle4 = new SimpleCircle(225);               
       System.out.println("The area of the circle of radius " + circle4.radius + " is " + circle4.getArea());

       // Circle5 object created

       SimpleCircle circle5 = new SimpleCircle(325);               
       System.out.println("The area of the circle of radius " + circle5.radius + " is " + circle5.getArea());

   }
   double radius;

   /** Construct a circle with radius 1 */   

   SimpleCircle()
   {
       radius = 1;
   }
   /** Construct a circle with a specified radius */
   SimpleCircle(double newRadius)
   {
       radius = newRadius;
   }
   /** Return the area of this circle */
   double getArea()
   {
       return radius * radius * Math.PI;
   }
   /** Return the perimeter of this circle */
   double getPerimeter()
   {
       return 2 * radius * Math.PI;
   }
   /** Set a new radius for this circle */
   void setRadius(double newRadius)
   {
       radius = newRadius;
   }
}