Sie sind auf Seite 1von 3

July 16, 2011

Object Oriented Programming 1 (INFR 2140) Assignment 2


Due Date: 11:59 p.m., July 25, 2011 This assignment involves material from the notes of Chapters 9, 10 and 11 of the textbook. Please name your files as follows: Rectangle2D.h, Rectangle2D.cpp, testRectangel.cpp, Complex.h, Complex.cpp and testComplex.cpp respectively. Make sure your programs compile and run without errors.

Program evaluation Criteria


1. Program design correctness: [ 30% ] 2. Proper commenting and documenting: [ 10% ] a. your code is commented, has a header section that shows the author, date and the assignment that it belongs to. It further describes the purpose of the program. b. In your code, use comments to explain the purpose of the variables declared, and the intention with each, or almost each line of your code. 3. Compilation: [ 10% ] your code compiles without error . 4. Execution and output: [ 30% ] your code is executable, does not crash and produces the expected output. 5. Code appearance and appropriate indentation: [ 10% ] your code must be properly formatted. Matching pairs of {} are in the same column, code in the main function is uniformly indented to enhance readability, sections of code that belong together are visually set apart from other parts. As an example, you should leave an empty line after the variable declaration). 6. Modular design: [ 5% ] 7. UML class diagram: [5%] - All submissions must be submitted through the WebCT system.

1. Define a Rectangle2D class that contains: Two double data fields named x and y that specify the center of the rectangle with get and set functions. (Assume that the rectangle sides are parallel to x or y axes). The data fields width and height with get and set functions. A no-arg constructor that creates a default rectangle with (0, 0) for (x, y) and 1 for both width and height. A constructor that creates a rectangle with the specified x, y, width, and height. A function getArea() that returns the area of the rectangle. A function getPerimeter() that returns the perimeter of the rectangle. A function contains(double x, double y) that returns true if the specified point (x, y) is inside this rectangle. See Figure (a).

(a) (b) (c) Figure: (a) A point is inside the rectangle. (b) A rectangle is inside another rectangle (c) A rectangle overlaps another rectangle. A function contains(Rectangle2D &r) that returns true if the specified rectangle is inside this rectangle. See Figure (b). A function overlaps(Rectangle2D &r) that returns true if the specified rectangle overlaps with this rectangle. See Figure (c). Draw the UML diagram for the class. Implement the class. Write a test program that creates three Rectangle2D objects r1(2, 2, 5.5, 4.9) , r2(4, 5, 10.5, 3.2), and r3(3, 5, 2.3, 5.4) , and displays r1's area and perimeter and displays the result of rl.contains(3 , 3) , rl.contains (r2) , and rl.overlaps(r3). 2. [Extension of Assignment 1] A complex number has the form a + bi, where a and b are real numbers and i is . You can perform addition, subtraction, multiplication, and division for complex numbers using the following formulas: a + bi + c + di = (a + c) + (b + d)i a + bi (c + di) = (a c) + (b d)i (a + bi) * (c + di) = (ac bd) + (bc + ad)i (a + bi) / (c + di) = (ac + bd) / (c2 + d2) + (bc ad)i / (c2 + d2) Design a class named Complex for representing complex numbers and the functions add, subtract, multiply, divide for performing complex-number operations, and the tostring function for returning a string representation for a complex number. The tostring function returns a + bi as a string. If b is 0, it simply returns a.

Provide three constructors Complex(a, b), Complex(a) , and Complex(). Complex() creates a Complex object for number 0 and Complex(a) creates a Complex object with 0 for b. Overload the operators +, ,*, /, +=, =, [], unary + and , prefix ++ and , postfix ++ and , <<, >>. Overload the operators +, , *, /, <<, >> as nonmember functions. Overload [] so that [0] returns a and [1] returns b. Draw the UML diagram for the class. Submission Guideline: Please follow these steps to submit your project/files. These instructions will allow you to submit proper projects with header/source files. Delete everything in your Visual Studio Project folder except for the following files (folders that say Debug or Release can be safely deleted): *.cpp *.h *.vcxproj You do not need any other file or folder. You can now zip up the project folder which contains these files. Your project, when zipped up should be a few kilobytes in size. If the zipped up file is in megabytes, you are including files that are redundant. You can also test your zipped up file (that is extract it, and then compile/run the project) before submission. The name of the zip files must be in the following form: Rectangle2D.zip and Complex.zip.

Das könnte Ihnen auch gefallen