• Home
  • Textbooks
  • The Practice of Computing Using Python
  • More on Classes

The Practice of Computing Using Python

William F. Punch, Richard Enbody

Chapter 13

More on Classes - all with Video Answers

Educators


Chapter Questions

01:38

Problem 1

Describe what the isinstance function does, and give an example of its use.

Adam Conner
Adam Conner
Numerade Educator

Problem 2

Augment the Rational number class to include multiplication and division. Include the ability to accommodate operands of type int.

Check back soon!
02:30

Problem 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

Dr. Anas Syed
Dr. Anas Syed
Numerade Educator

Problem 4

There are websites such as ht tp://www, vehix. com that provide information about secondhand vehicles. Design a base class for vehicle with fields such as model year, total mileage, Vehicle Identification Number (VIN), EPA class, EPA mileage, engine, transmission, and options.
Design subclasses for car, truck, SUV, and minivan. Think about the specific fields and methods required for the subclasses.

Check back soon!

Problem 5

Design a class called Bi11 that has fields to store the list of items, their prices per unit, quantity of each item in the bill, and the grand total of the items. Create an overloaded add operator so that when you add two bills, you get a new bill with all the items in the two bills (there may be duplicates in the final bill) and a new grand total.
Would you have multiply and divide overloaded operators in this case? Think!
For example, a McDonald's bill might include 2 burgers $\$ 4.00$ each, 4 strawberry milkshakes $\$ \$ 2.50$ each.

Check back soon!
01:38

Problem 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, c) whether the velocity is in meters/sec or feet/sec.
This constructor should accordingly convert the feet/sec into meters/sec. Create addition and subtraction operators for velocity.
A more advanced version of this class would consider the directions other than same or opposite directions.

Dheeraj Sharma
Dheeraj Sharma
Numerade Educator
02:44

Problem 7

Every semester, students take a number of courses, and at the end of the semester, they get their final grades. Design a class called SemTranscript that stores the name of the student, list of courses taken by the student and the corresponding grades, and the average grade for that semester. Also, create a overloaded addition operator so that you can add two semester grades to get courses taken in two semesters and calculate the final grade for a year.
Note: In the overloaded operator, you cannot just directly take an average of the grades of the two SemTranscript objects to get the average final grade of the year.

Akash M
Akash M
Numerade Educator
01:55

Problem 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.

Foster Wisusik
Foster Wisusik
Numerade Educator

Problem 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) and chemical formula of the compound ( $\mathrm{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.

Check back soon!

Problem 10

Design a class called color. The fields of the class are three decimals for Red, Green, and Blue components in the range 0 to 1 , inclusive ( 0 indicates Black and 1 indicates White). Add checks to ensure that the values are always in the given range. Provide addition and subtraction operators for the color class. Include saturation in the addition and subtraction: if any component goes less than 0 or greater than 1 , assign them 0 or 1 , respectively.

Check back soon!

Problem 11

Design a Sound class with a field for decibels. Provide addition and subtraction operators as well as get and set methods.

Check back soon!
00:40

Problem 12

Design a Logarithm dass. Fields should be the base and number. Provide addition and subtraction operators - remember to adjust bases appropriately. Hint: Use base 2 or 10 as a canonical base for operations.

Heather Zimmers
Heather Zimmers
Numerade Educator

Problem 13

Write a class called WholeNumber class. The whole numbers are the non-negative integers: $0,1,2, \ldots$ Your class must handle addition, subtraction, and multiplication of whole numbers - no division or mixed-type (whole number and integer) operations need be handled. Your class must also handle printing - e.g., if $\mathrm{x}$ is an instance of the WholeNumber class, you must be able to write print $\mathrm{x}$.
Two cases must not be allowed: (1) you must not be able to create a WholeNumber that has a negative value; (2) an arithmetic operation cannot be allowed to have a negative result. In both cases, an error message must be printed.
Remember that arithmetic must return a whole number. That is, if $x$ and $y$ are whole numbers, the result of $x+y$ must be a whole number.
Include sample code that uses your class and demonstrates the use of all methods as well as error handling.

Check back soon!
03:53

Problem 14

Write a class for linear equations. A generic linear equation is of the form $y=m x+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 (LinearBquation) that composes two linear equations. That is, if $y=x+1$ and $z=2 a+5$, then $y(z)=2 x+6$ and will be called as $y \cdot$ compose $(z)$. Note that the compose operation is not commutative.
(d) _-_add_- returns the sum of two linear equations. That is, if $y=a x+b$ and $z=c x+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.

Morgan Cheatham
Morgan Cheatham
Numerade Educator
02:45

Problem 15

Write an Odometer class. An odometer is the gauge on your car that measures distance traveled. In the United States, an odometer measures miles; elsewhere else, it measures kilometers. Many vehicles with electronic odometer interfaces have the ability to switch between miles and kilometers. The accuracy is $1 / 10$ of a mile (kilometer).
Something to consider: if an odometer gets replaced, a new one must be able to be set to some specified mileage.
Include the following methods:
(a) _-_init_., _._str_.., _-repr_..
(b) The constructor must take two arguments that both have default values; one is mileage, and the other specifies units.
(c) Addition and subtraction both have one odometer operand and one numeric operand, where the numeric operand represents the miles being added/subtracted (not two odometer operands).
(d) Addition should be commutative (but not subtraction).
(e) Output should always be rounded to $1 / 10$ mile (kilometer), but the odometer itself should maintain full floating-point accuracy.
Include sample code that uses your class and demonstrates the use of all methods as well as demonstrating error handling.

Shoukat Ali
Shoukat Ali
Other Schools
05:12

Problem 16

Write a Clock dass that measures hours, minutes, and seconds. Include the following methods:
(a) _._init _., _._str_.., ._repr_..
(b) Addition allows for both clock-to-clock operations and clock-to-integer operations. If one argument is an integer, the integer is assumed to represent hours. Addition should be commutative!
(c) Output should always be rounded to the second.
(d) If inappropriate values are passed to the constructor, the created clock instance should get a value of 0 hours, 0 minutes, and 0 seconds.
Include sample code that uses your class and demonstrates the use of all methods as well as error handling.

Willis James
Willis James
Numerade Educator

Problem 17

Write a Compass class. A compass provides bearings. A bearing is a measure of direction as indicared by clockwise rotation around a circle. There are 360 degrees in a bearing, and within each degree are 60 minutes. Thus, a legal bearing would be 270 degrees, 36 minutes (approximately due west). There are never more than 359 degrees in a bearing or more than 59 minutes in a bearing. Both degrees and minutes are integer values. Include the following methods:
(a) ._init_.., .._str_.., ... repr_..
(b) Addition allows for both bearing-to-bearing operations and bearing-to-integer operations. If one argument is an integer, the integer is assumed to represent degrees. Addition should be commutative!
(c) Output should always be rounded to the minute.
(d) If inappropriate values are passed to the constructor, the created bearing instance should get a value of 0 degrees, 0 minutes.
Include sample code that uses your class and demonstrates the use of all methods as well as error handling.

Check back soon!