8. Consider the design of a class for managing the Olympics—to simplify things, consider management of Olympic medal awards. Design a medal class.
• Fields: Name of the athlete, country, event, medal type (gold, silver, bronze)
• Methods: get, set, and so on.
3. The human body has many organs (heart, lungs, brain, and kidneys, to name a few). We could think of the human body as a complex object made up of simpler objects (organs).
a. Create classes for heart and brain. Think about what the functions of the heart and brain are in our body and map these functions into methods.
b. Create an Organ class as the base class for the organs and derive the classes created earlier from the base class.
6. In Newtonian physics, we know that two velocities in the same direction add to each other; in the opposite direction, they subtract from each other to give a net result in the direction of the bigger velocity. Design a simple class called Velocity that has a field for the speed in meters per second. (Forget about the direction for now). The default constructor would work with meters/sec metric. Write a constructor that takes three parameters: (a) distance, (b) time, and (c) whether the velocity is in meters/sec or feet/sec. This constructor should accordingly convert the feet/sec into meters/sec. Create overloaded unary, addition, and subtraction operators for velocity. A more advanced version of this class would consider the directions other than same or opposite directions.
8. File Concatenation: Many operating systems allow one to concatenate files. Design a class called TextDocument that reads the contents of a text file only. Create a constructor that takes the path of a text file. Create an overloaded addition operator for this class that adds (concatenates) two TextDocument objects (appends the second one at the end of the first one) and creates a new text file. Handle all possible error cases, such as for file existence.
9. Consider a specific type of chemical reaction that takes in two compounds and returns a new compound. Design a class called Compound with fields for the name of the compound (such as hydrochloric acid), chemical formula of the compound (HCl). Create an addition operator for the compound class so that when you add two compounds, you get a new one. In the operator function, just print a string indicating the two compounds being added.
14. Write a class for linear equations. A generic linear equation is of the form y = mx + b where m and b are constants. Include the following methods:
a. __init__, __str__ , __repr__.
b. value(x), which returns the value of the equation given x.
c. compose(linearEquation) that composes two linear equations. That is, if y = x +1 and z = 2a+5, then y(z) = 2x +6 and will be called as y.compose(z). Note that the compose operation is not commutative.
d. __add__ returns the sum of two linear equations. That is, if y = ax + b and
z = cx + d , then y + z = (a + c )x + (b + d).
Include sample code that uses your class and demonstrates the use of all methods as well as error handling.
These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden."""
Homework Problem: Ch 12 No. 3
The Organ class is the base class containing basic
information about the organ, Brain and Heart class
derived from the organ class.
def __init__(self, name):
The super class constructor
:param name: the name of the organ.
self.__name = name
It returns the name of the organ.
:return: the name of the organ.
It returns the string representation
of the organ instance.
:return: the string containing name of the organ
def __init__(self, name):
The constructor to initialize the heart class instance,
it also calls the super class constructor.
:param name: the name of the organ
The work method which prints the working
of this organ.
By purchasing this solution you'll be able to access the following files: