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)\]
You have attempted of activities on this page
Next Section - 20.16. Chapter Assessment