logo CodeStepByStep logo


Language/Type: Python classes
Related Links:

Add a method named clear to the Stock class that resets the shares purchased and total cost. Suppose a class of objects named Stock has been already written to represent purchases of shares of stock. The class contains the following members:

member name type description
Stock(symbol) constructor constructs a new stock representing the given symbol such as "YHOO"
data attributes private attributes storing data inside the object
s.symbol property stock symbol as passed to the constructor, as a string (read-only)
s.total_cost property total cost of all stock shares purchased, as a real number (read-only)
s.total_shares property total number of shares purchased of this stock (initially 0) (read-only)
s.profit(current_price) method returns the total amount of profit earned on the shares of this stock, given today's current price per share (accessor)
s.purchase(shares, price) method records the purchase of the given number of shares of this stock at the given price per share (mutator)
str(s) method returns a string representation of the stock (accessor)

Add a mutator method to the Stock class named clear that resets the stock's number of shares purchased and total cost to 0.

Member function: Write a member function that will become part of an existing class. You do not need to write the complete class.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.