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

79% Wed Apr 12 10:08:05 AM CR E Preview File Edit View Go Tools Window Help Scre

ID: 3816166 • Letter: 7

Question

79% Wed Apr 12 10:08:05 AM CR E Preview File Edit View Go Tools Window Help Screen Shot 2017-04-12 at 9.25.58 AM ntroduction to java programming by Y Daniel Liang 10th edition.pdf (page 347 of 1,345 a Search v Introduction to java programmin... LISTING 9. I TestSimpleCircle.java 1 public class TestSimpleCircle main class Main method 3 public static void main(String args) main method Create a circle with radius 1 5 Simple Circle circlel new Simple Circle C create object System.out.printin The area of the circle of radius circle radius is circle1.getArea. O) Create a circle with radius 25 10 Simple Circle circle2 new Simple Circle(25) create object System out.printlnC The area of the circle of radius 11 12 circle2. radius is circle2. getAreaO) 13 14 Create a circle with radius 125 15 SimpleCircle circle new SimpleCircle (125 create object 16 System out.printin The area of the circle of radius 324 17 circle3. radius is circle .getAreaO) 18 19 Modify circle radius 20 circle2 radius 100; or circle2.setRadius C100) 21 System.out.printin The area of the circle of radius 22 circle2. radius is circle 2. getAreaO) 9.3 Example: Defining Classes and Creating Objects 325 23 24 25 26 Define the circle class with two constructors 27 class SimpleCircle class SimpleCircle 28 double radius data field 29 325 30 Construct a circle with radius 1 31 Simple Circle no-arg constructo 32 radius 1; 33

Explanation / Answer

Hi,

In 9.1 we have class SimpleCircle where we are defining all the properties of circle. Where as test sample circle is class which is just used to test our SimpleCircle class by giving field valus and manipulating it.

Copy the following code in any IDE (e.g. Eclipse) and run the program. You have to create a class with name TestSimpleCircle and paste following code:

package com.example.circle;

public class TestSimpleCircle {

   public static void main(String[] args) {
       //Circle with radius 1
       SimpleCircle circle1 = new SimpleCircle();
       System.out.println("The area of circle of radius"+circle1.radius+" is "+circle1.getarea());
      
       SimpleCircle circle2 = new SimpleCircle(25);
       System.out.println("The area of circle of radius"+circle2.radius+" is "+circle2.getarea());
      
       SimpleCircle circle3 = new SimpleCircle(25);
       System.out.println("The area of circle of radius"+circle3.radius+" is "+circle3.getarea());
      
       circle2.radius=100;
       System.out.println("The area of circle of radius"+circle2.radius+" is "+circle2.getarea());
      
   }
  
}
   //This is Simple class
   class SimpleCircle{
       double radius;
      
      
       SimpleCircle(){
           radius=1;
       }
      
       SimpleCircle(double newRadius){
           radius=newRadius;
       }
      
       //To get area of circle
      
       double getarea(){
           return radius*radius*Math.PI;
       }
       double getPerimeter(){
           return 2*radius*Math.PI;
       }
       void setRadius(double newRadius){
           radius=newRadius;
       }
   }

============================================================

Listing 9.2. The main difference between listing 9.1 and 9.2 is, in 9.2 there is only one class in file which contains main methos which is printing everything. So here in same class we are defining the properties of the circle and in same file with the help of main method we are printing it.

I have added two more objects to print their details:

package com.example.circle;

public class SimpleCircle{

   public static void main(String[] args) {
       //Circle with radius 1
       SimpleCircle circle1 = new SimpleCircle();
       System.out.println("The area of circle of radius"+circle1.radius+" is "+circle1.getarea());
      
       SimpleCircle circle2 = new SimpleCircle(25);
       System.out.println("The area of circle of radius"+circle2.radius+" is "+circle2.getarea());
      
       SimpleCircle circle3 = new SimpleCircle(25);
       System.out.println("The area of circle of radius"+circle3.radius+" is "+circle3.getarea());
      
       circle2.radius=100;
       System.out.println("The area of circle of radius"+circle2.radius+" is "+circle2.getarea());
      
       SimpleCircle circle4 = new SimpleCircle(30);
       System.out.println("The area of circle of radius"+circle4.radius+" is "+circle4.getarea());
       SimpleCircle circle5 = new SimpleCircle(120);
       System.out.println("The area of circle of radius"+circle5.radius+" is "+circle5.getarea());
   }
  
   double radius;

       SimpleCircle(){
           radius=1;
       }
      
       SimpleCircle(double newRadius){
           radius=newRadius;
       }
      
       //To get area of circle
      
       double getarea(){
           return radius*radius*Math.PI;
       }
       double getPerimeter(){
           return 2*radius*Math.PI;
       }
       void setRadius(double newRadius){
           radius=newRadius;
       }
   }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote