Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Previous question and answer: Q1 : Try to write class definition for the Circle class. The class has two instance variables, center and radius. Note

Previous question and answer:

Q1 : Try to write class definition for the Circle class.

The class has two instance variables, center and radius.

Note that the center is a tuple and contains two values - the x and y values. Use data mangling for naming your instance variables as discussed in class.

When the object is created, your code should check to make sure that the radius is strictly positive. If not, print an appropriate message. In addition to the constructor, your class definition should also have the following methods:

1. A constructor to initialize all instance variables.

2. Accessor methods for all instance variables

3. Mutator methods for all instance variables

4. A __str__() method to print the center and radius of each circle object.

5. The definition should also have the following methods

a. get_area(): This should compute and return the area of the circle

b. get_circumference(): This should compute and return the circumference of the circle

c. grow(): Each time this method is called, the radius of the circle object will increase by 1

d. shrink(): Each time this method is called, the radius of the circle object will decrease by 1

Quick Summary:

  • Python is used to solve this problem
  • Class Circle is made
  • All methods are mentioned in problem is included in code
  • output of code is provided

Explanation:

class Circle: __radius=0#you can even comment this line code will still work fine __center=(0,0)#you can even comment this line code will still work fine def __init__(self,center=(0,0),radius=0):#constuctor with(default values are alse provided) self.setCenter(center) self.setRadius(radius) def setCenter(self,center):#mutator method for center self.__center=center def setRadius(self,radius):#mutator method for radius if radius>=0:#is radius is greater or equal to zero only in that case value is set self.__radius=radius else: print("Radius should not be negative number") def getCenter(self):#Accessor method for center return self.__center def getRadius(self):#Accessor method for radius return self.__radius def get_area(self): area = 3.14 * self.__radius **2#computing area pi * r^2 return area def get_circumference(self): circumference= 2*3.14*self.__radius#computing circumference = 2*pi*r return circumference def grow(self): self.setRadius(self.__radius +1 )#setradius to current radius value +1 def shrink(self): self.setRadius(self.__radius - 1 )#setradius to current radius value -1 def __str__(self): return 'Circle(center= ('+str(self.__center[0])+","+str(self.__center[1]) +') , radius='+str(self.__radius)+ ')' #string method for class when object of type cirle is printed this method will be invoked. circle1=Circle((3,2),5) print(circle1) circle1.grow() print("After grow") print(circle1)#you can see change in radius print("radius of circle is %.2f"%circle1.get_area()) print("circumference of circle is %.2f"%circle1.get_circumference()) print() circle2=Circle((34,63),8) print(circle2) circle2.shrink() print("After shrink") print(circle2)#you can see change in radius print("radius of circle is %.2f"%circle2.get_area()) print("circumference of circle is %.2f"%circle2.get_circumference()) 

And my question is for question2:

Q2 : Write a driver class to test the Circle class definition.

Step 1: Create four Circle objects choosing appropriate values for the attributes and print out the four circle objects.

Step 2: For each Circle object, randomly peak either grow() or shrink() to run once. Then print out the new four circle objects

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions