Python Lesson 17 Exercises: More Work With Classes
For each of the following exercises, save the program as pa17-1.py, pa17-2.py, etc. You may wish to split these programs into modules.
Exercise 1:
Create a program that will take the coordinates of two points on a rectangle (the upper left corner and lower right corner) then allow the user to select one of the following options: calculate area, calculate perimeter, print an on-screen sample of the rectangle. The program should use the following class outline:
class Rect:
def __init__(corner1, corner2):
#calculate and print height and width
def area():
def perimeter():
def __repr__:
# draw a rectangle of given size
Exercise 2:
Create a Money calculator that takes numbers as input and displays all output in currency format (12345 would be displayed $12,345.00). The program must use a class and may have operator overloads for addition, subtraction, multiplication, and division so that when a number is added to the original amount, the result is displayed in money format as well.
Exercise 3:
Create a program that will keep track of the investments in a stock portfolio. The program must use classes, and each stock will be a new instance (or object) of the class. The program should give the users the options to [A]dd a new stock to portfolio, [U]pdate a stock price or the number of shares owned in a stock, [V]iew stock information, [D]elete a stock from the portfolio, or to view the [P]ortfolio summary. The portfolio summary should list all stocks in the portfolio and tell the total amount invested in your portfolio (total of all stock values). The program should use shelve or pickle to save and retrieve all information in the program.
Restricted access |