## Stephen J. Chapman

## Chapter 12

## User-Defined Classes and Object-Oriented Programming - all with Video Answers

## Educators

Chapter Questions

Demonstrate that multiple copies of the Timer class of Example 12.1 can function independently without interfering with each other. Write a program that creates a random $50 \times 50$ set of simultaneous equations and then solves the equations. Create three Timer objects as follows: one to time the equation creation process, one to time the equation solution process, and one to time the entire process (creation plus solution). Show that the three objects are functioning independently without interfering with each other.

Check back soon!

Improve the Timer class of Example 12.1 so that it does not fail if it is timing objects whose durations run over midnight. To do this, you will need to use function datenum, which converts a date and time into a serial date number that represents the years since year zero, including fraction parts. To calculate the elapsed time, represent the start time and elapsed time as serial date numbers and subtract the two values. The result will be elapsed time in years, which then must be converted to seconds for use in the Timer class. Create a static method to convert a date number in years into a date number in seconds, and use that method to convert both the start time and elapsed time in your calculations.

Check back soon!

Improve the Timer class of Example 12.1 in a different way so that it does not fail if it is timing objects whose durations run over midnight. To do this, you should convert each date vector into a dateTime object and calculate the difference in time between the start time and the end time in seconds using a duration calculation.

Check back soon!

Create a handle class called PolarComplex containing a complex number represented in polar coordinates. The class should contain two properties called magnitude and angle, where angle is specified in radians (see Figure 12.13).

The class should include access methods to allow controlled access to the property values, as well as methods to add, subtract, multiply, and divide two Pol arComplex objects.

PolarComplex objects can be converted to rectangular form using the following equations:

$$

\begin{aligned}

c & =a+b i=z \angle \theta \\

a & =z \cos \theta \\

b & =z \sin \theta \\

z & =\sqrt{a^2+b^2} \\

\theta & =\tan ^{-1} \frac{b}{a}

\end{aligned}

$$

Complex numbers are best added and subtracted in rectangular form.

$$

\begin{aligned}

& c_1+c_2=\left(a_1+a_2\right)+\left(b_1+b_2\right) i \\

& c_1-c_2=\left(a_1-a_2\right)+\left(b_1-b_2\right) i

\end{aligned}

$$

Complex numbers are best multiplied and divided in polar form.

$$

\begin{aligned}

c_1 \times c_2 & =z_1 z_2 \angle \theta_1+\theta_2 \\

\frac{c_1}{c_2} & =\frac{z_1}{z_2} \angle \theta_1-\theta_2

\end{aligned}

$$

Create methods that add, subtract, multiply, and divide Pol arCompl ex numbers based on Equations (12.12) through (12.15), designing them so that two objects

( GRAPH CAN'T COPY )

can be manipulated with ordinary math symbols. Include static methods to convert back and forth from rectangular to polar form for use with these calculations.

Check back soon!

Three-Dimensional Vectors The study of the dynamics of objects in motion in three dimensions is an important area of engineering. In the study of dynamics, the position and velocity of objects, forces, torques, and so forth are usually represented by three-component vectors $\mathbf{v}=x \hat{\mathbf{i}}+y \hat{\mathbf{j}}+z \hat{\mathbf{k}}$, where the three components $(x, y, z)$ represent the projection of the vector $\mathbf{v}$ along the $x, y$, and $z$ axes, respectively, and $\hat{\mathbf{i}}, \hat{\mathbf{j}}$, and $\hat{\mathbf{k}}$ are the unit vectors along the $x, y$, and $z$ axes (see Figure 12.14). The solutions of many mechanical problems involve manipulating these vectors in specific ways.

The most common operations performed on these vectors are:

1. Addition. Two vectors are added together by separately adding their $x, y$, and $z$ components. If $\mathbf{v}_1=x_1 \hat{\mathbf{i}}+y_1 \hat{\mathbf{j}}+z_1 \hat{\mathbf{k}}$ and $\mathbf{v}_2=x_2 \hat{\mathbf{i}}+y_2 \hat{\mathbf{j}}+z_2 \hat{\mathbf{k}}$, then $\mathbf{v}_1+\mathbf{v}_2=\left(x_1+x_2\right) \hat{\mathbf{i}}+\left(y_1+y_2\right) \hat{\mathbf{j}}+\left(z_1+z_2\right) \hat{\mathbf{k}}$.

2. Subtraction. Two vectors are subtracted by separately subtracting their $x$, $y$, and $z$ components. If $\mathbf{v}_1=x_1 \hat{\mathbf{i}}+y_1 \hat{\mathbf{j}}+z_1 \hat{\mathbf{k}}$ and $\mathbf{v}_2=x_2 \hat{\mathbf{i}}+y_2 \hat{\mathbf{j}}+z_2 \hat{\mathbf{k}}$, then $\mathbf{v}_1-\mathbf{v}_2=\left(x_1-x_2\right) \hat{\mathbf{i}}+\left(y_1-y_2\right) \hat{\mathbf{j}}+\left(z_1-z_2\right) \hat{\mathbf{k}}$.

3. Multiplication by a scalar. A vector is multiplied by a scalar by separately multiplying each component by the scalar. If $\mathbf{v}=x \hat{\mathbf{i}}+y \hat{\mathbf{j}}+z \hat{\mathbf{k}}$, then $a \mathbf{v}=a x \hat{\mathbf{i}}+a y \hat{\mathbf{j}}+a z \hat{\mathbf{k}}$.

4. Division by a scalar. A vector is divided by a scalar by separately dividing each component by the scalar. If $\mathbf{v}=x \hat{\mathbf{i}}+y \hat{\mathbf{j}}+z \hat{\mathbf{k}}$, then $\frac{\mathbf{v}}{a}=\frac{x}{a} \hat{\mathbf{i}}+\frac{y}{a} \hat{\mathbf{j}}+\frac{z}{a} \hat{\mathbf{k}}$

5. The dot product. The dot product of two vectors is one form of multiplication operation performed on vectors. It produces a scalar that is the sum of the products of the vector's components. If $\mathbf{v}_1=x_1 \hat{\mathbf{i}}+y_1 \hat{\mathbf{j}}+z_1 \hat{\mathbf{k}}$

( GRAPH CAN'T COPY )

and $\mathbf{v}_2=x_2 \hat{\mathbf{i}}+y_2 \hat{\mathbf{j}}+z_2 \hat{\mathbf{k}}$, then the dot product of the vectors is $\mathbf{v}_1 \cdot \mathbf{v}_2=x_1 x_2+y_1 y_2+z_1 z_2$.

The cross product. The cross product is another multiplication operation that appears frequently between vectors. The cross product of two vectors is another vector whose direction is perpendicular to the plane formed by the two input vectors. If $\mathbf{v}_1=x_1 \hat{\mathbf{i}}+y_1 \hat{\mathbf{j}}+z_1 \hat{\mathbf{k}}$ and $\mathbf{v}_2=x_2 \hat{\mathbf{i}}+y_2 \hat{\mathbf{j}}+z_2 \hat{\mathbf{k}}$, then the cross product of the two vectors is defined as $\mathbf{v}_1 \times \mathbf{v}_2=\left(y_1 z_2-y_2 z_1\right) \hat{\mathbf{i}}+\left(z_1 x_2-z_2 x_1\right) \hat{\mathbf{j}}+\left(x_1 y_2-x_2 y_1\right) \hat{\mathbf{k}}$.

7. Magnitude. The magnitude of a vector is defined as $\mathbf{v}=\sqrt{x^2+y^2+z^2}$.

Create a class called Vector3D having three properties $x, y$, and $z$. Define constructors to create vector objects from three input values. Define get and put access methods for each property, and define methods to perform the seven vector operations defined in the preceding list. Be sure to design the methods so that they work with operator overloading when possible. Then create a program to test all of the functions of your new class.

Shafiq Rehman

Numerade Educator

The cross product. The cross product is another multiplication operation that appears frequently between vectors. The cross product of two vectors is another vector whose direction is perpendicular to the plane formed by the two input vectors. If $\mathbf{v}_1=x_1 \hat{\mathbf{i}}+y_1 \hat{\mathbf{j}}+z_1 \hat{\mathbf{k}}$ and $\mathbf{v}_2=x_2 \hat{\mathbf{i}}+y_2 \hat{\mathbf{j}}+z_2 \hat{\mathbf{k}}$, then the cross product of the two vectors is defined as $\mathbf{v}_1 \times \mathbf{v}_2=\left(y_1 z_2-y_2 z_1\right) \hat{\mathbf{i}}+\left(z_1 x_2-z_2 x_1\right) \hat{\mathbf{j}}+\left(x_1 y_2-x_2 y_1\right) \hat{\mathbf{k}}$.

Check back soon!

If no exceptions are thrown within a try block, where does execution continue after the try block is finished? If an exception is thrown within a try block and caught in a catch block, where does execution continue after the catch block is finished?

Check back soon!

Modify the FileWriter class by adding new methods to write numerical data to the file as text strings, with one numerical value per line.

Check back soon!

Example 6.4 shows how to create a simple random number generator with a uniform distribution over the range $[0,1)$. Exercise 6.33 shows how to create a random number generator with a Gaussian distribution, and Exercise 6.36 shows how to create a random number generator with a Rayleigh distribution.

Create a random number generation class that includes a way to set the initial seed in a constructor and through a set method. The random seed should be an instance variable of the class. The class should include methods to return random numbers drawn from uniform, Gaussian, and Rayleigh distributions. Test your program by creating arrays of uniform, Gaussian, and Rayleigh distributed values, and create histograms of the data in each array. Do the distributions have the right shape?

Jessica Waggener

Numerade Educator

Demonstrate that each instance of the random number class in Exercise 12.8 is independent by performing the following steps: Create three objects from this class. Initialize two of them with the random seed 123456, and initialize the third one with the random number seed 654321 . Get the first 5 Gaussian distributed values from each object.

Neel Faucher

Numerade Educator

Create a MATLAB class called dice to simulate the throw of a fair die by returning a random integer between 1 and 6 every time that the class is called. Design the class so that it keeps track of the total number of calls to each object. When the object is destroyed, it should print out the total number of times that it was used.

Jerrah Biggerstaff

Numerade Educator