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

4 77% Wed Apr 12 10:12:13 AM Q O E Preview File Edit View Go Tools Window Help S

ID: 3816172 • Letter: 4

Question

4 77% Wed Apr 12 10:12:13 AM Q O E Preview File Edit View Go Tools Window Help Screen Shot 2017-04-12 at 9.25.58 AM Introduction to java programming by Y Daniel Liang.10th edition.pdf (page 361 of 1,345) a Search The new circle class, named CircleWithStaticMembers, is defined in Listing 9.6 V Introduction to java programmin... LISTING 9.6 CircleWithStaticMembers java 1 public class CirclewithstaticMembers The radius of the circle 3 double radius The number of objects created static int numberofobjects 0; static variable 8 Construct a circle with radius 1 9 Circle withstaticMembersO radius 1; 10 increase by 1 11 numberofobjects 12 13 14 Construct a circle with a specified radius 15 CirclewithstaticMembers Cdouble ewRadiu s) 16 radius new Radius; increase by 17 number ofobjects++; 338 18 19 20 Return numberofobjects 21 static int getNumberofobjectsO static method 22 return numberofobjects; 23 24 25 Return the area of this circle 26 double getArea C) 27 return radius radius Math.PI 28 29 Method getNumberofobjectsO in CirclewithStaticMembers is a static method. All the methods in the Math class are static, The nain method is static, too. 339 9.7 Static Variables. Constants. and Methods 339

Explanation / Answer

Excercise 3:

1. Yes, you can type and compile the class CircleWithStaticMember with no main method but you can not run it because main method is the starter method for .java file and this method is only visible to JVM this method loaded to JVM during compile time only due to static.

// Code of CircleWithStaticMember class is here...

package com.circle;

public class CircleWithStaticMember {

   double radius;
  
   //No of objects created
   static int nuOfObjects;
  
   //constructor a circle with radius 1
   CircleWithStaticMember(){
       radius=1;
       nuOfObjects++;
   }
   //constructor a circle with specified radius
   CircleWithStaticMember(double newRadius){
       radius=newRadius;
       nuOfObjects++;
   }

   //return no of objects
   public static int getNuOfObjects() {
       return nuOfObjects;
   }
  
   //return the area of circle
   double getArea(){
      
       return radius*radius*Math.PI;
   }
  
  
}


2. Find here for code of class TestCircleWithStaticMember and output analysis

package com.circle;

public class TestCircleWithStaticMember {
  
   //Main method
   public static void main(String[] args) {
      
       System.out.println("Before creating Objects");
      
       System.out.println("The number of circle object is: "+CircleWithStaticMember.nuOfObjects);
      
       //creating c1 object
       CircleWithStaticMember c1=new CircleWithStaticMember();
      
       //Display c1 before c2 created
       System.out.println("After creating c1");
       System.out.println("c1:radius("+c1.radius+") and number of circle objects ("+c1.nuOfObjects+")");
      
      
       //creating c2
       CircleWithStaticMember c2=new CircleWithStaticMember(5);
      
       //modify c1
       c1.radius=9;
      
       //Display c1 and c2 after c2 created
       System.out.println("After creating c2 and modifying c1");
       System.out.println("c1:radius("+c1.radius+") and number of circle objects ("+c1.nuOfObjects+")");
       System.out.println("c2:radius("+c2.radius+") and number of circle objects ("+c2.nuOfObjects+")");
      
       System.out.println();
      
      
      
   }

}

//Output

Before creating Objects
The number of circle object is: 0 //Before object creating default value (0) assigned to static variable nuOfObjects
After creating c1
c1:radius(1.0) and number of circle objects (1) //After c1 object created radius assigned through default constructor and nuOfObjects incremented by 1
After creating c2 and modifying c1
c1:radius(9.0) and number of circle objects (2) //Radius modified in c1 object by 9 after c2 object created nuOfObjects again incremented by 1 so finally becomes 2. Thsi is static variable so its value will be same for all objects
c2:radius(5.0) and number of circle objects (2) // Rdius passed through parametoried onstructor 5 so radius is 5 and number objects will be same 2 because its static variable will be same for all objects

3. Find below code and output analysis by extending code of object creating c3 and c4

//Code starts here...

package com.circle;

public class TestCircleWithStaticMember {
  
   //Main method
   public static void main(String[] args) {
      
       System.out.println("Before creating Objects");
      
       System.out.println("The number of circle object is: "+CircleWithStaticMember.nuOfObjects);
      
       //creating c1 object
       CircleWithStaticMember c1=new CircleWithStaticMember();
      
       //Display c1 before c2 created
       System.out.println("After creating c1");
       System.out.println("c1:radius("+c1.radius+") and number of circle objects ("+c1.nuOfObjects+")");
      
      
       //creating c2 of radius 5
       CircleWithStaticMember c2=new CircleWithStaticMember(5);
      
       //modify c1
       c1.radius=9;
      
       //Display c1 and c2 after c2 created
       System.out.println("After creating c2 and modifying c1");
       System.out.println("c1:radius("+c1.radius+") and number of circle objects ("+c1.nuOfObjects+")");
       System.out.println("c2:radius("+c2.radius+") and number of circle objects ("+c2.nuOfObjects+")");
      
       //creating c3 object of radius 20
       CircleWithStaticMember c3=new CircleWithStaticMember(20);
      
       //creating c3 object of radius 100
       CircleWithStaticMember c4=new CircleWithStaticMember(100);
      
       System.out.println("c3:radius("+c3.radius+") and number of circle objects ("+c3.nuOfObjects+")");
       System.out.println("c4:radius("+c4.radius+") and number of circle objects ("+c4.nuOfObjects+")");
      
      
   }

}

//Output is here...

//Explained in second question
Before creating Objects
The number of circle object is: 0
After creating c1
c1:radius(1.0) and number of circle objects (1)
After creating c2 and modifying c1
c1:radius(9.0) and number of circle objects (2)// number of object is 2 because displayed before object created c3 and c4 if you display again then here also nuOfObjects will be 4 only
c2:radius(5.0) and number of circle objects (2) // number of object is 2 because displayed before object created c3 and c4 if you display again then here also nuOfObjects will be 4 only

c3:radius(20.0) and number of circle objects (4) // Now radius becomes 20 and total objects created 4
c4:radius(100.0) and number of circle objects (4) // Now radius becomes 100 and total objects created 4 . This value will be same for all objects

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