# 20.15. Exercises¶

1. Add a method reflect_x to Point which returns a new Point, one which is the reflection of the point about the x-axis. For example, Point(3, 5).reflect_x() is (3, -5)

1. Add a method called move that will take two parameters, call them dx and dy. The method will cause the point to move in the x and y direction the number of units given. (Hint: you will change the values of the state of the point)

## 20.15.1. Contributed Exercises¶

Add a method called move that will take two parameters, call them dx and dy. The method will cause the point to move in the x and y direction the number of units given. (Hint: you will change the values of the state of the point)

Define a class called Bike that accepts a string and a float as input, and assigns those inputs respectively to two instance variables, color and price. Assign to the variable testOne an instance of Bike whose color is blue and whose price is 89.99. Assign to the variable testTwo an instance of Bike whose color is purple and whose price is 25.0.

The following sample file called studentdata.txt contains one line for each student in an imaginary class. The students name is the first thing on each line, followed by some exam scores. The number of scores might be different for each student.

joe 10 15 20 30 40
bill 23 16 19 22
sue 8 22 17 14 32 17 24 21 2 9 11 17
grace 12 28 21 45 26 10
john 14 32 25 16 89


Using the text file studentdata.txt write a program that prints out the names of students that have more than six quiz scores.

Use the code for the Vehicle class as a starting point. Add a method (function) named vehicle_info() that returns a string containing the year, make, and model like “2015 Dodge Journey”.

Create an instance of the Vehicle class. Call vehicle_info() and print the result.

Create a class named Vehicle. Its constructor should take three parameters, the year, the name of the make and the name of the model, which should be stored in variables named year, make, and model, respectively.

Create an instance of the Vehicle class to represent a vehicle of your choice.

Use your existing Vehicle class as a starting point.

Create a method named years_old(). It should return how many years old the vehicle is. You can find the current year using this code:

from datetime import datetime

current_year = datetime.now().year

Create an instance of the Vehicle class to represent a vehicle of your choice. Call the years_old() function and print the return value.

Think of a real-world object you might want to represent as a class. Write your class, providing any variables or methods you think would be appropriate for it.

Add a __repr__() function to the Vehicle class to return a string representation of object contents when used with a list. Your __repr__() function should call __str__() and return the string representation that function provides. You can copy-and-paste your __str__() function code into this question.

Create a list containing two instances of the Vehicle class to represent vehicles of your choice. Print the contents of the list.

Add a __str__() function to the Vehicle class to return a string representation of object contents. Your function should return the vehicle’s year, make, and model like “2015 Dodge Journey”.

Create an instance of the Vehicle class to represent a vehicle of your choice, and print your vehicle instance.

Add a clone() function to the Vehicle class to return a new Vehicle instance that duplicates the contents of the existing vehicle.

Create an instance of the Vehicle class to represent a vehicle of your choice. Then, use the clone() method to make a duplicate of that vehicle.

Add class variables to the Vehicle class to store the year, make, and model. Make the default value for each be None.

Create an instance of the Vehicle class to represent a vehicle of your choice. Then, print the model name of your vehicle.

Add a function called sort_order() to the Vehicle class. It should return the vehicle’s make, model, and year like “Dodge Journey 2015” to facilitate sorting vehicles in that order.

Copy-and-paste the __str__() and __repr__() functions from a previous question.

Create a list containing three or more instances of the Vehicle class to represent vehicles of your choice. Sort the list on the sort_order() results and print the sorted list.

Write a new class named Dog to inherit from the Pet class. The new class should have a class variable named breed with a default value of Unknown. Dog’s constructor must allow users to specify the dog’s name, age, and breed.

Add a method to the Pet class that generates a string representation that resembles Archie: 0.5 years old when an instance of the class is printed.

Print the string representation of the archie object.

Write code using the archie object that prints the text The pet Archie is 0.5 years old. Make use of the existing dog_info() method in your code. Do not modify the existing dog_info() method.

Write code using the archie object that prints the text The pet Archie is 0.5 years old. Make use of the existing dog_info() method in your code. Do not modify the existing dog_info() method.

Consider the class below. Write the main part of the program that asks the users for input and uses those inputs to create a new instance of User and store it in variable new_user.

2. Ask for the user’s email and assign it to variable email

3. Ask for the user’s year of birth and store it in variable yob

4. Create a new object with those value the user provided. Assign the new object to variable new_user

Hey there! Write code that makes a class called Pet. This class should have the following attributes: name, age and sound. This means that each pet you create should have a name, age and sound it makes. Then do the following in the order given. Make sure each question works until proceeding to the next one:

1. Define the initializer method __init__ that will set the properties of your pets. sound property should be “argh” by default. It should also set a new class variable called amount_of_energy to 0.

2. In the main program create an instance of Pet and assign it to a variable called my_pet with the following attributes: name is “Spock”, age is 10.

3. To your Pet class add a method called feed_me that takes one input called amount, an integer representing the amount of food for feeding. feed_me should increment the amount_of_energy property of the class by the amount amount only if that amount is smaller or equal than 10. Otherwise it prints: “Too much food” and doesn’t change the amount of energy.

4. Add a __repr__ method that will print the following “My Pet” and the amount of energy your pet currently has. For example, “My Pet. Amount of energy = 100”. Beware: 100 is fixed, and you should make it variable depending on how much energy your pet currently has.

5. To your Pet class add a method called play that takes one input called amount, an integer representing the amount of play. play should decrement the amount_of_energy property of the class by amount only if that amount is smaller or equal than amount_of_energy (your pet cannot play if it requires more energy than it has). Otherwise it prints: “Not enough energy” and doesn’t change the amount of energy.

6. In your main program, call method feed_me on my_pet with amount = 5. Print object my_pet. Look at the amount of energy there? Did it change/increase from the initial 0?

7. In your main program, call method play on my_pet with amount = 2. Print object my_pet. Look at the amount of energy there? Did it change/decrease from the previous print?

Create a class called House that has three instance variables: color, rooms, and price. To the variable name houseOne, assign an instance of House whose color is white, has 3 rooms, and costs 50000. To the variable name houseTwo, assign an instance of House whose color is red, has 9 rooms, and costs 1000000.

Create a class called math_op with one instance variable and a method. The instance variable should be numb. The method should be called squared and return the instance variable squared. Create an instance of this class with an initial number of 8. Assign to the variable output the value of the square of the numb without hardcoding. Call the method so that the value which is returned is 64.

Create a class called Animal that has two instance variables: arms and legs. Create a class method called limbs that, when called, returns the total number of limbs the animal has. To the variable name spider, assign an instance of Animal that has 4 arms and 4 legs. Call the limbs method on spider and save the result to the variable name spidlimbs.

Create a class called bank that contains two instance variables, name and amt. Add the instance method, __str__, that allows you to customize the message returned when you print the instance so that it says “Your account, [name goes here], has [start_amt goes here] dollars.” Create an instance of this class with “Bob” as the name and 100 as the amount. Save this to the variable t1.

This problem will modify your previously defined class, bank. Add two more methods, add_deposit and less_withdrawals. The add_deposit method should add the deposit amount (an input paremeter to add_deposit is required) to amt and the less_withdrawals method should subtract the withdrawal amount (again, an input parameter to less_withdrawals) from amt. Both of these methods return the new balance in addition to updating the amt state variable.

Create two instances of the class, the first assigned to bob and the second to sally. The first uses “Bob” as the name and 100 as the initial amount. The second uses “Sally” as the name and 200 as the initial amount. For bob, call add_deposit enough times with an input of 50 dollars so that the bank account is 200 dollars and save to the resulting balance to bob_amt. For sally, call less_withdrawal enough times with an input of 125 dollars so that the bank account is 75 dollars and save to the variable sally_amt.

Write the definition for the Robot class, which will provide a simple model of a robot, given the following specifications:

Attributes: A Robot has three attributes/state variables:
1. name: a string representing the robot’s name

2. build_year: an integer representing the year the robot was built

3. batttery_level: a float representing the robot’s battery level between 0.0 (empty) and 1.0 (full)

Methods: Implement the following methods in your Robot class:
1. the constructor: in addition to self the constructor requires two arguments, n for the name and b for the build_year, which it sets within the method. The battery_level should be initialized to 0.0 (and since this is the same for every robot, no argument is needed).

2. charge: requires no arguments (other than self), and it sets the battery level to 1.0.

3. introduce: requires no arguments (other than self), and it returns a string of the form “Hello, my name is ____.” where the robot puts its own name in the blank.

4. __str__: returns a string containing the robot’s name, build year, and battery level within the message: “NAME was built in YEAR and its battery level is currently LEVEL.” where the words in all caps are replaced by the state variable values.

Sample use of a Robot:

mike = Robot("Mike", 2016)
print(mike.introduce()) #prints "Hello, my name is Mike."
print(mike.battery_level) #prints 0.0
mike.charge()
print(mike.battery_level) #prints 1.0
print(mike) #prints "Mike was built in 2016 and its battery level is currently 1.0."


Fill in the code in order to build the Time class. Rather than detailed specifications, the code contains a number of assert statements. Read the assertions to understand the expected behavior of the Time class, and what kinds of methods you’ll need to code inside it. Assertions won’t print anything if their boolean expression is true. But if they are false, the system will generate an error message when you run your code. So when you’re able to run your code without triggering any assertion errors (with the assertions uncommented), you’re done with the problem! While developing your code, you may want to comment out the assertions and other commands, and then uncomment them one at a time to confirm your code passes. I recommend trying to get each assertion to work in sequence, but you may want to skip around. Make sure your code runs before you submit it, even if that means leaving some assertions commented out. This will allow you to get partial credit for the ones that pass.