Object Oriented Programming. Please do in words file. Bid only if you are confident to answer the questions correctly. refer to the attachment for instructions.
Unformatted Attachment Preview
National University of Singapore
School of Continuing and Lifelong Education
TIC1002: Introduction to Computing and Programming II
Semester II, 2018/2019
Problem Set 4
Object Oriented Programming
Release date: 4th April 2019
Due: 21st April 2019, 23:59
Task 1: Point ? Line ? Polygon [25 marks]
This problem set is to introduce / revise object oriented programming concepts in a “gentle”
way ?. The first two parts rewrite old code into OOP approach which serves both as example of
how OOP code looks like as well as a way to contrast the “procedural / function based”
approach and the OOP approach. The last part asks you to apply the OOP concepts and write a
new class from ground up.
A. 2D Point [2 marks]
Refer to the question 1 in lecture 6. ADT training on Coursemology. In that training, we
introduce a simple Point ADT that represents a location in a 2D Cartesian plane, e.g. (1,2) is a
point where the X-coordinate = 1, Y-coordinate = 2. The code was written in the procedural
style, i.e. structure + related functions.
In the given template file, the same ADT is now rewritten into a C++ Class. Other than some
minor name changes for the method (to make them sounds better and to improve readability),
the general logic is the same. Use this opportunity to contrast the procedural and OOP
approach. Pay attention to:
How the “internal representation” of a Point is now hidden from user by using private
Information (i.e. the structure) is now “inherent” in the object. User need not pass in the
information of a point for the function to work in the OOP approach.
Take note of the “distance_to()” method, which highlight a typical way to interact
with other object of the same class.
Your task is relatively simple. Implement the method “equal()”, which checks whether the
point object contains the same coordinate as another point object.
B. Line connecting two points [3 marks]
Similarly, refer to question 2 of the same Lecture 6.ADT training for the Line ADT. The template
file gives the equivalent implementation in OOP approach. Pay attention to the interaction
between Point ADT and Line ADT. Observe that in the OOP approach the Line ADT can no
longer directly access the internal information of a Point ADT. The only way for an “external
party” to access the internal representation of an object is to go through the public methods
provided, e.g. we need to use set_x() to change the x coordinate of a point now instead of
directly changing the internal data.
Your task is equally simple. Implement the “equal()” method to check whether two lines are
the same. Note that line “(1,1) (2,2)” is the same as the line “(2,2) (1,1)”, i.e. as long as the
two end points are the same, the lines are considered as the equivalent.
C. Polygon! [20 marks]
This is the “real meat” of this problem set. Part A and B should have given you enough
understanding on how a class work, now it is time for you to implement a class from ground up.
The target class, polygon, represents 2D shapes composed by connected straight lines, e.g.
The example polygon is composed of 5 lines and the corresponding 5 unique points. The
simplest polygon has 3 lines / 3 points (i.e. a triangle) and there is no upper limit for the most
complex polygon (e.g. a 2D model in a computer game nowadays can have thousands of sides).
Polygon( const Point& p1, const Point& p2, const Point& p3)
Since the simplest Polygon has 3 points, we “force” the user to pass 3 points into the
Polygon constructor. For simplicity, you can assume the 3 points will form a valid Polygon
(i.e. there is no duplicate point(s), the points are not on the same line, etc). You need to
decide an appropriate internal representation of the Polygon before you can start, e.g. do
you want to store the lines of the polygon, or just the points, or both? Should you use array,
vector, linked list or something else? etc. Declare the appropriate information in the private
section of the class.
Also, you may want to read through the entire list of tasks before making your decision.
Point p1(2,2), p2(5,2), p3(5,6);
Polygon pg(p1, p2, p3);
The above represents a polygon pg:
With the constructor implemented, you may want to complete the following helper methods
Task: Printing method
virtual void print( ostream& out )
Similar to Point and Line class, write a print method to display the information of the
polygon in the following format (the grey squares represent spaces):
(X1,Y1) -> (X2,Y2) -> (X3,Y3) -> (X1,Y1)
? There can be more than 3 points in a Polygon object.
? The first point is printed again at the end to indicate that the last point is connected
to the first point.
//Follows from the previous example
(2,2) -> (5,2) -> (5,6) -> (2,2)
Task: Calculate the perimeter
virtual double perimeter()
This method calculate and print the perimeter of the polygon, i.e. the sum of the length of
all lines composing the polygon. Again, note that the polygon can have more than 3 sides.
//Follows from the previous example
cout << pg.perimeter() << endl; (p1 to p2 = 3 units, p2 to p3 = 4 units, p3 to p1 = 5 units) With the helper methods in place, you can now implement the more challenging methods. We are going to expand the polygon by adding new points. Let's define two different versions as follows. Task: Add point (version 1) virtual bool add(Point& p) Add point p as the last point in the polygon. You need to ensure that p is not a duplicate of any of the existing points in the polygon. Return true if point p is added successfully, false otherwise. Note: You can assume that point p (if it is not a duplicate of any existing points) will form a valid polygon, i.e. we will NOT pass in a point that cause any of the line to cross each other. Example: Point p1(2,2), p2(5,2), p3(5,6), p4(5,2), p5(2,6); Polygon pg(p1, p2, p3); pg.add( p4 ); //false is returned as p4 is a duplicate of p2 pg.add( p5 ); //this will add p5 to the polygon successfully. After p5 is added, the polygon looks like: 4|Page (2,6) (2,2) (5,6) (5,2) You should test the add() method by using print() and perimeter() methods. Task: Add point (version 2) virtual bool add(Line& exist, Point& p) Remove the line exist from the polygon, then add the point p "in between" of the original end points. You need to ensure the line exist is indeed one of the existing lines in the polygon before adding point p. Return true if point p is added successfully, false otherwise. Notes: ? The line exist can be any line that forms the current polygon. ? You can assume that point p (if it is not a duplicate of any existing points) will form a valid polygon, i.e. we will NOT pass in a point that cause any of the line to cross each other. ? This is an example of method overloading. You can see that both methods are named "add()", but this is acceptable as long as the parameter(s) are different. The C++ compiler will decide which version you are calling based on the parameters you pass in. Example (continue from the example before, the polygon is now a rectangle): Point Point Line Line p1(2,2), p2(5,2), p3(5,6), p4(5,2), p5(2,6); p6(-1,6), p7(-1, 2); l5(p1, p5); l6(p1, p3); //this line doesnt exist in the current polygon pg.add( l6, p7 ); //false is returned, as l6 is not an existing line. pg.add( l5, p6 ); //this will add p6 to the polygon successfully. After p6 is added, the polygon looks like following. Note that the removed line l5 is shown 5|Page as dotted line to aid your understanding only, you dont need to keep track of removed line in anyway. (-1,6) (2,6) (5,6) (5,2) (2,2) Sample Output (For your reference) The given template duplicated some of the examples above with minor modification. Read through the tests so that you have a better understanding. If your implementation is correct, the completed program should output the following (includes output from all parts): Distance from p1 to p2 is 3 Is p1 the same as p2 = 0 Is p2 the same as p4 = 1 Is l1 the same as l2 = 1 Is l1 the same as l3 = 0 (2,2) -> (5,2) -> (5,6) -> (2,2)
Perimeter = 12
Add p1 to polygon failed!
Add p5 to polygon ok!
(2,2) -> (5,2) -> (5,6) -> (2,6) -> (2,2)
Perimeter = 14
Remove l6 and add p7 to polygon failed!
Remove l5 and add p6 to polygon ok!
(2,2) -> (5,2) -> (5,6) -> (2,6) -> (-1,6) -> (2,2)
Perimeter = 18
~~~ End of Problem Set ~~~
Purchase answer to see full
Delivering a high-quality product at a reasonable price is not enough anymore.
That’s why we have developed 5 beneficial guarantees that will make your experience with our service enjoyable, easy, and safe.
You have to be 100% sure of the quality of your product to give a money-back guarantee. This describes us perfectly. Make sure that this guarantee is totally transparent.Read more
Each paper is composed from scratch, according to your instructions. It is then checked by our plagiarism-detection software. There is no gap where plagiarism could squeeze in.Read more
Thanks to our free revisions, there is no way for you to be unsatisfied. We will work on your paper until you are completely happy with the result.Read more
Your email is safe, as we store it according to international data protection rules. Your bank details are secure, as we use only reliable payment systems.Read more
By sending us your money, you buy the service we provide. Check out our terms and conditions if you prefer business talks to be laid out in official language.Read more