Skip to main content

Section 16.14 Mixed-Up Code Exercises

Checkpoint 16.14.1.

Create a Dog class and define the __init__ method that has one parameter, name. Then define the getName method, which returns the name when the method is called.

Checkpoint 16.14.2.

Create a class named Dog. Define the __init__ method with two parameters, name and age. Also create the method updateAge, that increases the age by 1 and returns age.

Checkpoint 16.14.3.

Create the class Cat. Define the __init__ method, it has two parameters, name and age. Next define the the __str__ method to return a string with the cat's information: "Name: name, Age: age". Then define the make_sound method, which should return the string "Meow".

Checkpoint 16.14.4.

Create a class named Book that has an __init__ method with the parameters title and author. Then create a __str__ method that returns "Title: title, Author: author".

Checkpoint 16.14.5.

Create a class named Dog with the __init__ method taking name as its parameter and setting self.tricks to an empty list. Then create a __str__ method that returns a string with "Name: name". Then create a method, updateTricks, that adds a new trick to the list.

Checkpoint 16.14.6.

Create a class named Square with the __init__ method taking the length, len, with a default of 10 if it is not specified. Then create a __str__ method that returns a string with "Length: length". Then create an area method that returns the area (length times length).

Checkpoint 16.14.7.

Create a class named Rectangle with the __init__ method taking a length, and width. Then create a __str__ method that returns a string with "l: length, w: width". Then create an area method that returns the area (length times width).

Checkpoint 16.14.8.

Given a class Point with a method distance_from_point(point), create a class named Triangle with the __init__ method taking three Point objects and creating an attribute points. Then create an base method that returns length of the longest side. The length of a side is the distance between two points. For example, t = Triangle(Point(0,0), Point(1,0), Point(0,2)) would return 2.23 from base.

Checkpoint 16.14.9.

Create a class named PickItem with the __init__ method taking a list of items. Then create an pick method that returns one of the items at random. You can use the random module's choice method for this. Next create add(item) that adds the item to the list of items.

Checkpoint 16.14.10.

Create a class Dice with an __init__ method that takes the number of sides, num_sides. Use a default of 6 if num_sides isn't passed in. Also set an attribute history to an empty list in the __init__ method. Then create a __str__ method that returns "Dice with num_sides sides" when num_sides is the number of sides. Next create an roll method that picks a random value from one to the number of sides, adds the value to the end of the history list, and returns it. You can use the random module's randint(start,end) method to return a random number from start to end inclusvie.