Lab Assignment‐2 - California State University, Long Beach

CECS‐282 C++ For Java Programmers © H. W. Williams California State University Long Beach College Of Engineering Computer Engineering and Computer Science Department LabAssignment‐2
AssignmentDueonFebruary26,2013
This assignment is taken from Dr. Frank Murgolo Curriculum. Page 1 of 4 CECS‐282 C++ For Java Programmers © H. W. Williams Description:
Implement the class Complex so that when main() executes the output matches the desired output as shown below. Do not modify main(), you may only modify the location of your file. Also, do not modify the public section of Complex. You may modify the private section of Complex. HeaderFileCode:
#ifndef COMPLEX
#define COMPLEX
#include <string>
using namespace std;
class Complex
{
public:
// Pre Condition:
// Post Condition: the complex number (real, imaginary)
//
is createdA
// Returns: the complex
Complex(double real = 1, double imaginary = 0);
// Pre Condition:
// Post Condition: the arguments are not changed
// Returns: the complex sum
Complex plus(const Complex &other) const;
// Pre Condition:
// Post Condition: the arguments are not changed
// Returns: the complex difference
Complex minus(const Complex &other) const;
// Pre Condition:
// Post Condition: the arguments are not changed
// Returns: the complex product
Complex times(const Complex &other) const;
// Pre Condition:
// Post Condition: the arguments are not changed
// Returns: the complex quotient
Complex divide(const Complex &other) const;
// Pre Condition:
// Post Condition: the arguments are not changed
// Returns: (real, imaginary)
string askString() const;
private:
double real;
double imag;
bool
undefined;
};
#endif // COMPLEX
Page 2 of 4 CECS‐282 C++ For Java Programmers © H. W. Williams TestFile
You may save the test file on your documents. The test file will provide the program with the appropriate inputs. If for any reason you can’t get this file to work, you may create your own main file to run the following scenarios. TestCasesandExpectedResults
Case 1 Complex c1 = (1,0) Complex c2 = (0,1) Sum is = (1,1) Difference is = (1,‐1) Product is = (0,1) Quotient is = (0,‐1) Case 2 Complex c1 = (1,1) Complex c2 = (1,1) Sum is = (2,2) Difference is = (0,0) Product is = (0,2) Quotient is = (1,0) Case 3 Complex c1 = (‐1,0) Complex c2 = (1,1) Sum is = (0,1) Difference is = (‐2,‐1) Product is = (‐1,‐1) Quotient is = (‐0.5,‐0.5) Page 3 of 4 CECS‐282 C++ For Java Programmers © H. W. Williams Case 4 Complex c1 = (0,‐1) Complex c2 = (1,1) Sum is = (1,0) Difference is = (‐1,‐2) Product is = (1,‐1) Quotient is = (‐0.5,‐0.5) Case 5 Complex c1 = (‐1,‐1) Complex c2 = (0,0) Sum is = (‐1,‐1) Difference is = (‐1,‐1) Product is = (0,0) Quotient is = Undefined Case 6 Complex c1 = (1,2) Complex c2 = (3,4) Sum is = (4,6) Difference is = (‐2,‐2) Product is = (‐5,10) Quotient is = (0.44,0.08) Page 4 of 4