=========================preview======================
(comp151h)[2006](s)midterm~1712^_10138.pdf
Back to COMP151H Login to download
======================================================
COMP 151H: Object Oriented Programming (Honor Track)
Spring Semester 2006
Midterm Exam
Instructor: Chi Keung Tang
Wednesday, March 15, 2006
7:00 C 9:00pm
Room 2502
This is a CLOSED-BOOK-CLOSED-NOTES exam consisting of three (3) problems. Follow the instructions carefully. Please write legibly in the space provided. Keep the exam booklet stapled.
KEY
Problem
Points
your score
1 INFORMATION HIDING
4
2 CONTAINER CLASS
8
3 OBJECT CONSTRUCTION AND DESTRUCTION
3
Total
15
1 Information hiding
Figure 1: Polygon examples
A
A
F
B
B
F
E
C
E
C
D
current_pointer
D Doubly linked list representation
Polygon of the Polygon
Figure 2: Polygon representation
In this question, you are required to implement two member functions for a class Polygon to manipulate a 2D polygon.
For example, Figure 1 shows some typical polygons. Actually, a polygon can be represented by a circular doubly linked list
(see Figure 2). An edge connecting a node (Point) A and B is represented by a next-pointer in node A and a previous-pointer
in node B.
The Polygon class contains a circular doubly linked list that stores the points or vertices of a polygon.
Here is the structure of a Pointfor a 2D point or vertex:
struct Point{
int x; // x.coordinate
int y; // y.coordinate
}
And, here is the de.nition of the Polygon class:
class Polygon {
public:
Polygon(); // default constructor
.Polygon(); // destructor
bool isCollide ( Polygon& inPolygon); // returns true if the inPolygon
// collides with this Polygon;
// returns false if otherwise
// setPolygon takes an array of points and forms a polygon
void setPolygon (Point pts[], int size)
{
vertexList.clear();
for (int i=0; i<size; i++)
{
vertexList.insertToNext ( pts[i] );
vertexList.pointToNext ();
}
}
// Two member functions to be implemented by you
Polygon* cutPolygon();
bool trimPolygon ( Polygon& inPolygon );
private:
LinkedList vertexList; // The circular doubly linked list
};
As you can see, the polygon class contains a private variable called vertexList which is a LinkedList object. This is the circular doubly linked list. The de.nition of the LinkedList is de.ned as follow:
class LinkedList{
public:
LinkedList(); // default constructor
~LinkedList(); // destructor
int getSize() const; // return the number of elements(node) of the linked list
bool isEmpty() const; // return true if the list is empty
void clear(); // make the circular doubly linked list empty
void deleteCurrentNode(); // delete the current node. The current pointer will
// point to the next node of the deleted node
void pointToNext(); // make the current_pointer point to the next node
void pointToPrev(); // make the current_pointer point to the previous node
Point getCurrentPoint() const; // return the Point pointed by the current_pointer
void insertToNext( const Point& ptr ); // insert a Point next t