Com S 228 --- Introduction to Data Structures HOMEWORK 5: Data Abstraction and Classes (File $Date: 1995/02/22 16:56:44 $) Due: problem 3, at the beginning of your discussion section week of Feb. 23; problem 6, at the beginning of class on Feb 27. (You'll also want to look at problem 6 before your discussion section during the week of Feb. 23.) In this homework, you will learn about data abstraction and classes in C++. The readings are in the section headings below. Problems 1-3 are on the basics, the others involve operator overloading. PLEASE NOTE: For each programming problem, hand in a printout of your code files, and your transcript of testing. Your transcript must be run on the Com S department machines (use the Unix command ``script''); it should show your testing. Your transcript and each file you print should have the file name and your information (your ~/.me file as in HW0) at the top as comments. You must use comments as we are doing in class (see /home/cs228/public/assertion-notation.txt for details). We will be grading on these comments now. For this homework we are *not* asking you to hand in makefiles. You may continue to use them, and we suggest that, but it's at your option. For additional practice, we suggest that for at least some of the problems, you write out a solution on paper first. This is more like what the test will be. When you are happy with your paper version, try it on the computer. (Note that you must hand in a printout and a transcript, however.) HEADINGTON & RILEY: 3.1-4 DEITEL & DEITEL: 6, 7.1-4, 7.8 1. (suggested practice) Do some of the exercises in Headington and Riley's book for chapter 3 (pp. 157-161). I suggest exercises 3.1(a,c,e,h,i), 3.2, 3.3(a,b,d,g), 3.4(a), 3.5, 3.6, 3.8. Do what parts seem useful to you, then look at the answers, which are at the end of the book, in pages A-173 to A-174. 2. (suggested practice) Do some of the exercises in the self-review sections on page 390 and 427-8 of Deitel & Deitel's book. I suggest working exercises 6.2 and 7.2 on paper. Do what parts seem useful to you, then look at the answers, which follow the self review sections on pages 390-1 and 428. 3. (50 points) Do exercise 3.14 on page 161 of Headington and Riley's book. Your code is to go in files PassMod.h and PassMod.C, and the class is to be named Password. (You do *not* need to incorporate the changes suggested in exercise 3.13.) As stated in the problem, you must also (a) include a class constructor, and (b) state a class invariant. Also (c) adapt the test harness in figure 2.16 to test your class; change it to use the class member functions, and also test that your code can create more than one instance of the Password class. Hand in a printout of your testing and the C++ files. In your discussion section, you'll discuss your solution with another class member, and will have an opportunity to ask questions and discuss solutions with the class as a whole. So be sure to write your code clearly, and pay particular attention to the specification comments. We won't check the details of your work, as that will be the subject of the discussion. So don't worry (too much) if it isn't exactly right. We will check that what you hand in has the right parts (a printout of each file, labeled correctly with comments at the top, and a transcript). HEADINGTON & RILEY: 3.5-6 DEITEL & DEITEL: 8 4. (suggested practice) Do exercise 3.12(a,d) on page 160 of Headington and Riley's book. See p. A-174 for the answers. 5. (suggested practice) Do some of the exercises in the self-review sections on page 474 of Deitel & Deitel's book. I suggest working exercises 8.1-5 on paper. Do what parts seem useful to you, then look at the answers, which follow the self review sections on page 474. HEADINGTON & RILEY: 2 6. (80 points) (Time estimate: 4 hours) In physics, vectors of 3 numbers are often used to represent quantities directed in space (such as forces). In this problem, we will refer to a vector as a triple, (x, y, z). Vectors support the following operations. addition: (x1, y1, z1) + (x2, y2, z2) = (x1+x2, y1+y2, z1+z2) subtraction: (x1, y1, z1) - (x2, y2, z2) = (x1-x2, y1-y2, z1-z2) scalar multiplication: a * (x, y, z) = (a*x1, a*y, a*z) norm: Norm(x, y, z) = sqrt(x^2 + y^2 + z^2) where sqrt is the square root function Write a class Vector that represents the three coordinates of a vector as double values and provides the following operations. * constructors for explicit as well as default initialization. The default initial value should be (0.0, 0.0, 0.0). * operator functions to overload the operators + and - * an overload of operator * that performs scalar multiplication * a member function Norm * three access functions xCoord, yCoord, and zCoord that return the coordinates of a Vector. The operator functions can either be implemented as class members or as separate functions. You must have a separate file Vector.h with the class specification, and a file Vector.C with the implementation. The test harness for this class is specified as follows. INPUT: The program prompts the user to enter the first vector, then an operator, and then a second vector, each on a separate line. Each vector consists of 7 pieces: a left parenthesis `(', a double, a comma `,', a second double, a comma `,', a third double, and a right parenthesis `)'. For example: (1.0, 2e-12 , -7 ) Zero or more spaces separate these pieces. The operator is a single character: +, -, *, or n with `n' standing for "take the norm of the vector entered", and * standing for scalar multiplication. When the operator is `n', the second vector is not requested. When the operator is `*', the second input is a double, not a vector. End of input (and end of program execution) occurs when the user supplies the machine's end-of-file keystrokes when prompted for the first vector. OUTPUT: The following prompts for the vectors and the operator occur in the order shown. 1st vector (or to quit)? Operation? 2nd vector? The last prompt does not occur if the operation name entered is the character `n'. If the operation named is `*', then the last prompt is scalar? instead of "2nd vector? ". Each prompt has a blank after the question mark, and leaves the cursor at the end of the prompt line. The program then outputs one of the following lines, according to the operation asked for. Their sum is Their difference is Its norm is Their product is where denotes the result. The result is printed in the form of a vector input, except that a norm is printed as a single number. Finally the program outputs a blank line and prompts the user for the next vector. ERROR HANDLING: If the user enters an invalid operator, the program outputs (on cerr) Operator must be +, -, *, or n and repeatedly prompts until the operator is valid. EXAMPLE: 1st vector (or to quit)? (2, 3.4, 10.0) Operation? + 2nd vector? (5.4 , 0 , 8.97 ) Their sum is (7.4, 3.4, 18.97) 1st vector (or to quit)? (3, 0, 4) Operation? x Operator must be +, -, *, or n Operation? n Its norm is 5 1st vector (or to quit)? (14.1, -22.3, 4e-10) Operation? * scalar? 2.0 Their product is (28.2, -44.6, 8e-10) 1st vector (or to quit)? (1.1, -2.3, 10) Operation? - 2nd vector? (5.4 , 0 , 8.97 ) Their difference is (-4.3, -2.3, 1.03) 1st vector (or to quit)? ^D In the above ^D represents the user typing end-of-file (control-D on Unix). The exact format of the numbers printed for this example isn't important. The results may also differ slightly. You may also assume that the user only types vectors and scalars when prompted for them. You may thus ignore the possibility of end-of-file or other errors on cin except where end-of-file is expected. You should run the above test cases, as well as any additional ones you think are needed to properly test (or to debug) your program. Hints: you can use sqrt from the header file; when you do this, be sure to link your program with the -lm flag at the end of the Unix command line. 7. (20 points extra credit) Make your program for problem 6 so that it can handle the dot product of two vectors as an additional operation. Make any decisions about the requirements for the program you feel are reasonable. Add any additional test cases you believe are necessary. 8. (up to 200 points extra credit) Design and implement a ``vector calculator''. This should allow you to name particular vectors, and input complicated expressions.