# 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¶

Create a class called Charge that describes a charged particle, and define __init__() and potential_at() methods. __init__ should initialize the x, y, and z positions of the particle and the charge, q. potential_at() should take a 3D position to calculate a potential at and return the value of the potential

$V=k_e\frac{q}{r}$

where $$k_e=8.99\times10^9\,\text{N}\cdot \text{m}^2/\text{C}^2$$. Test your class by creating a charged particle at $$(1,2,3)$$ and calculating its potential at $$(2,3,4)$$.

The wavelengths of the spectral lines from hydrogen are given by the Balmer series

$\frac{1}{\lambda} = R_\text{H}\left(\frac{1}{2^2} - \frac{1}{n^2}\right)\quad n=3,4,5,\dots$

where $$\lambda$$ is the wavelength in m and $$R_\text{H}=1.0974\times10^7~\text{m^{-1}}$$.

Compute the first 10 wavelengths from the Balmer series and write them to a file call balmer.dat with one $$n$$ value and its wavelength per line in CSV format.

Hint: if you already have a solution for a previous the Balmer series question, copy and paste here to reuse the code.

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.

Add dot_product() methods to the Vector class below. Don’t worry about the case where the vector lengths don’t match.

Try using your completed class to calculate and print the results of

$(5.89, -1.00, 6.92, 8.32, 8.38, -5.56, 8.36)\cdot(-3.42, -3.7, 4.46, -7.33, 3.72, -8.30, -4.43)$

and

$(3.47, 3.23, 6.68)\cdot(-2.98, -9.27, 7.59)$

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.