Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please write the following Python 3 program. Please show all output. Circle class. 1. Modify the Circle class given in 3 ways: (a), (b) &

Please write the following Python 3 program. Please show all output.

Circle class. 1. Modify the Circle class given in 3 ways: (a), (b) & (c). (a) - implement __str__ and __repr__ methods exactly as shown below.

(b) - make the radius a property and add an attribute radius_log that is a list containing radius values that have belonged to the circle, where the last item in the list is the value of the current radius. In other words, I want you to keep a log of the changes to the radius of the circle object. Each time the radius changes, add the new value to the list.

Hint: You will need to store the actual radius somewhere as an attribute of the Circle, since the radius will now be a property. I suggest using an attribute named _radius for this. This attribute should only be used in the radius methods, not in any other methods.

Note that once you add the radius properties, they apply everywhere, including the other methods inside the class, which is why you need the separate _radius attribute. The two methods for getting and setting the radius should be the only ones to read/modify this attribute. Other methods such as __init__ should only reference self.radius .It also needs to happen when the Circle instance is created, so keep in mind the DRY principle of Don't Repeat Yourself.

______________________________________________________________________________________________________________________________________________

EXAMPLE CODE TO MODIFY: for (a) & (b)

image text in transcribed

________________________________________________________________________________________________________

EXAMPLE CODE FOR (c)

(c) Once you have completed parts (a) and (b), modify the Circle class so that it will raise a ValueError if the radius or diameter is set to less than zero. All of the REPL commands from (a) and (b) should still work!

image text in transcribed

_________________________________________________________________________________

Update:

image text in transcribed

>>> circle = Circle() Circle (radius=1) Traceback (most recent call last) from HW5 import Circle circle circle.radius1 File "stdins", line 1, in >> circle . diameter =-2 raise ValueError ("Radius cannot be negative!") Traceback (most recent call last): File "stdin", line 1, in File "/Users/diane/DevProjects/ucsd/Hw5. py", line 41, in diameter self. radius diameter2 File "/Users/diane/DevProjects/ucsd/Hw5.py line 52, in radius ValueError: Radius cannot be negative! >>> circ le2 = Circle (-2) raise ValueError("Radius cannot be negative!") Traceback (most recent call last): File File " .Users/diane/DevProjects/ucsd/HW5.py", line 26, in --init self. radius radius File /Users/diane/DevProjects/ucsd/HW5. py", line 52, in radius raise ValueError("Radius cannot be negative!") ValueError: Radius cannot be negative! >>> circle = Circle() Circle (radius=1) Traceback (most recent call last) from HW5 import Circle circle circle.radius1 File "stdins", line 1, in >> circle . diameter =-2 raise ValueError ("Radius cannot be negative!") Traceback (most recent call last): File "stdin", line 1, in File "/Users/diane/DevProjects/ucsd/Hw5. py", line 41, in diameter self. radius diameter2 File "/Users/diane/DevProjects/ucsd/Hw5.py line 52, in radius ValueError: Radius cannot be negative! >>> circ le2 = Circle (-2) raise ValueError("Radius cannot be negative!") Traceback (most recent call last): File File " .Users/diane/DevProjects/ucsd/HW5.py", line 26, in --init self. radius radius File /Users/diane/DevProjects/ucsd/HW5. py", line 52, in radius raise ValueError("Radius cannot be negative!") ValueError: Radius cannot be negative

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

Concepts Of Database Management

Authors: Philip J. Pratt, Joseph J. Adamski

4th Edition

0619064625, 978-0619064624

More Books

Students also viewed these Databases questions