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

75% Wed Apr 12 1016 46 AM QA O E Chrome File Edit View History Bookmarks People

ID: 3816176 • Letter: 7

Question

75% Wed Apr 12 1016 46 AM QA O E Chrome File Edit View History Bookmarks People Window Help x C chegg Study I Guided solutioi x C Chrome File Edit View History x M nbox jking199@students. X Lab 12.pdf ksuweb.kennesaw.edu hhaddad/Spring2017/CS1301/Lab12.pd Apps k D cs 130 Program YouTube home e CompTIA A+ Certifi IIowlLink online Ca M Inbox king 199@s PlayStation vue D Y2U40DAXNDgyZS... 5 Lab 12.pdf 1. Type and compile listing 9.8, page 345 (file CircleNithPrivateDataFields .java). otice that this is a class with no main method, you cannot run it. Page 1 of 2 2. Type, compile, and run listing 9.9, page 346 (file TestcircleWith PrivateDataFields .java). Observe the outputs and understand the displayed values. This program uses private variables (radius and numberofobjects). You have to use the get and set methods to access these private variables. 3. Next, modify the code in listing 9.9 to create another circle object, call it yourcircle with radius 40, 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 Save all files in the same folder Instructions: 1. Programs must be working correctly. 2. Programs must be completed and checked before working assignment #12. 3. Programs must be checked by the end of the designated lab session.

Explanation / Answer


public class TestCircleWithPrivateDataFields {
   public static void main(String[] args) {
       CircleWithPrivateDataFields myCircle =
               new CircleWithPrivateDataFields(5.0);
       System.out.println("The area of the circle of radius "
               +myCircle.getRadius()+" is "+myCircle.getArea());
      
       myCircle.setRadius(myCircle.getRadius() * 1.1);
       System.out.println("The area of the circle of radius "
               +myCircle.getRadius()+" is "+myCircle.getArea());
      
       System.out.println("The number of objectes created is "
               +myCircle.getNumberOfObjects());
      
       CircleWithPrivateDataFields yourCircle =
               new CircleWithPrivateDataFields(40.0);
      
      
       System.out.println("The area of the circle of radius "
               +yourCircle.getRadius()+" is "+yourCircle.getArea());
      
       //Increase the radius by 50%
       yourCircle.setRadius(yourCircle.getRadius() * 1.5);
       System.out.println("The area of the circle of radius "
               +yourCircle.getRadius()+" is "+yourCircle.getArea());
      
       System.out.println("The number of objectes created is "
               +yourCircle.getNumberOfObjects());
      
      
   }
}


public class CircleWithPrivateDataFields {
   private double radius = 1;
   private static int numberOfObjects = 0;
  
   public CircleWithPrivateDataFields(){
       numberOfObjects++;
   }
  
   public CircleWithPrivateDataFields(double newRadius){
       radius = newRadius;
       numberOfObjects++;
   }

   public double getRadius() {
       return radius;
   }

   public void setRadius(double radius) {
       this.radius = radius;
   }

   public static int getNumberOfObjects() {
       return numberOfObjects;
   }

   public double getArea() {
       return radius * radius * Math.PI;
   }  
  
  
}

----output--------------------

The area of the circle of radius 5.0 is 78.53981633974483
The area of the circle of radius 5.5 is 95.03317777109125
The number of objectes created is 1
The area of the circle of radius 40.0 is 5026.548245743669
The area of the circle of radius 60.0 is 11309.733552923255
The number of objectes created is 2

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