Mixed-Up Code Exercises¶
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.
Write 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.
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
.
Write 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
.
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"
.
Write 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"
.
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"
.
Write a class named Book
that has an __init__
method with two parameters, title
and author. Then create a __str__
method that returns "Title: title, Author: author"
.
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.
Write 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.
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
).
Write 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
).
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
).
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
).
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
.
Given a class Point
with a method distance_from_point(point)
, write 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
.
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.
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.
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.
Write 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.