• Home
  • Textbooks
  • The Practice of Computing Using Python
  • Introduction to Classes

The Practice of Computing Using Python

William F. Punch, Richard Enbody

Chapter 12

Introduction to Classes - all with Video Answers

Educators


Chapter Questions

Problem 1

In your own words, describe the purpose of the _._init _.. method.

Check back soon!

Problem 2

Write a shopping cart class to implement a shopping cart that you often find on websites where you could purchase some goods. Think about what things you could store in a cart and also what operations you could perform on the cart.
To simplify matters, you could consider the website to be an electronics e-store that has goods like flat-panel TVs, boomboxes, iPods, camcorders, and so on.

Check back soon!
02:49

Problem 3

Consider a table fan. Write a table fan class. What would be the attributes of this class? Examples of attributes could be speed levels of fan, side-to-side movement (on/off, and degrees of movement), manufacturer name, cost, used/new, and so on. Think about it this way. You want to be able to get information about the fan; e.g., if you want to buy or sell a fan from some website such as Craigslist, what features would you be interested in? Also, consider control (operations) you might want to have over the fan, such as setting the fan speed or having it pan from side to side.

Narayan Hari
Narayan Hari
Numerade Educator

Problem 4

There are many websites where you could listen to music-e.g., rhapsody.com. A music album contains many tracks, and each track has a name, time duration, name of artist, year, and other data.
Design classes for the album and tracks. Think about the methods appropriate for the classes. For the tracks, some methods could be play, pause, stop, etc.

Check back soon!

Problem 5

Design a class for an airline ticket. Some of the fields of an airline ticket are start, destination, date of travel, class (economy, first class, business class), price of ticket, number of people traveling, and name of the primary person. Think about the methods appropriate for the class.

Check back soon!
04:04

Problem 6

Design a class for a book that an online retailer (such as Amazon.com) might use to keep track of the book. Fields might include name of the book, publisher, price, author, and ISBN. Think about the methods appropriate for the class that an online retailer might use.

SS
Sarvesh Somasundaram
Numerade Educator

Problem 7

Consider a hospital scenario. Design classes for:
$\bullet$ Patients. The class might have fields like unique ID for the parient, name, male or female, age, address, phone number, date of birth, height, and weight.
$\bullet$ Doctor. Fields might include the doctor's name, unique registration number, qualification (DO or MD), specialization (surgeon, pediatrician, etc.), phone number, office hours, and office location.
$\bullet$ Patients' records in a hospital. The record might have fields like last date of checkup, doctor's unique ID, patient's unique ID, list of health problems in the patient, list of medicines prescribed, cost of the checkup, final report, and so on.
Methods of these classes would be mostly get and set methods for the corresponding fields.

Check back soon!
02:17

Problem 8

Consider the design of a class for managing the Olympics - to simplify things, consider management of Olympic medal awards. Design a medal class.
$\bullet$ Fields: Name of the athlete, country, event, medal type (gold, silver, bronze)
$\bullet$ Methods: get, set, and so on.

Sneha Ravi
Sneha Ravi
Numerade Educator
01:17

Problem 9

Design a schedule class for classes that a student (you!) take during a semester.

Foster Wisusik
Foster Wisusik
Numerade Educator
03:53

Problem 10

In a supermarket, all vegetables have a product code, name, some description, and price per unit. Design a class called Vegetable to represent the vegetables. Provide get and set methods for the class.

SS
Sarvesh Somasundaram
Numerade Educator

Problem 11

Design a class called Sentence that has a constructor that takes a string representing the sentence as input. The class should have the following methods:
$\bullet$ get_first_word
$\bullet$ get_all_words
$\bullet$ replace(index, new_word)—Change a word at a particular index to "new_word." E.g. If the sentence is "I'm going back." and set_word_at_index (2, "home"), then the sentence becomes "I'm going home."

Check back soon!