A2 Report
A2 Report
UOW ID : 7433098
Tutorial Group : T06F
Assignment 2 Report
The program will receive user input (a choice from 1 - 5). 1 will ask for more input for
information about a shape, then send that shape to an array. 2 will calculate the area for all
shapes in the array. 3 will display the information of shapes in the array without any sorting.
4 will prompt user for an option (a, b, c). a will display shapes sorted from smallest area to
largest. b will display shapes sorted from largest area to smallest. c will display shapes sorted
with WS shapes first then NS shapes, further sorted by largest to smallest within each category.
2. Program design:
Class relations
Program flow
3. Summary of function implementation:
Class structure:
Vertex
This struct stores the coordinates of a vertex of a shape. It has a default constructor, another
constructor, and a comparator to sort vertices in order of y-axis then x-axis
ShapeTwoD
It contains the name of shape, a boolean containing whether it contains warpspace and a
vector containing its vertices.
The first 3 methods are constructors. One default constructor, one for circles and the last for
the other remainding shapes (as the circle does not contain a vector of vertices, only a
center coordinate).
The next 4 methods are accessor methods for private/protected variables.
Following that are 2 functions used to convert variables to a formatted string. One for the
vertex vector, and one for the warp space boolean.
The next function is a virtual function toString that is meant to be overridden subclasses of
ShapeTwoD. This function outputs all information related to the shape in a formatted string.
computeArea() is a virtual function to be overridden. It calculates the area using shape
information.
The next 2 functions are virtual functions to be overridden. They determine if the input point
is fully within or on the perimeter of the shape.
The next 2 functions after that are more virtual functions to output the points within or on
the shape into a formatted string.
The following 3 methods are mutator methods to adjust private or protected variables
Lastly, there are 3 comparators used to sort arrays of pointers to ShapeTwoD objects
Circle
In addition to inheriting all variables and methods of ShapeTwoD, Circle also has two more
variables to store data not included in ShapeTwoD: center and radius.
The methods toString, computeArea, isPointInShape, isPointOnShape, getPerimeter and
getInternal are overridden to be relevant to a shape that is a circle.
Methods are added to change and access the new variables.
Cross
The methods toString, computeArea, isPointInShape, isPointOnShape, getPerimeter and
getInternal are overridden to be relevant to a shape that is a cross.
A method isValidCross is added. This is a static method to determine if an array of vertices
makes a valid cross.
Rectangle
The methods toString, computeArea, isPointInShape, isPointOnShape, getPerimeter and
getInternal are overridden to be relevant to a shape that is a rectangle.
A method isValidRectangle is added. This is a static method to determine if an array of
vertices makes a valid rectangle.
Square
The methods toString, computeArea, isPointInShape, isPointOnShape, getPerimeter and
getInternal are overridden to be relevant to a shape that is a square.
A method isValidSquare is added. This is a static method to determine if an array of vertices
makes a valid square.
Functions in main driver file:
Main() will initialize the shape array, and display the main menu of the program, it then
receives a user input and sends that to the selection() method. Once the user decides to
quit, any shape data stored in dynamic memory is cleared before exiting.
Selection() receives user input and the shape array, it will decide what to do with the array
based on the user input. If 1, the array is passed by reference to the getInput() function. If 2,
the array is passed by reference to the calculateArea() function. If 3, the array is passed by
value to the displayShapes() function. If 4, the array is passed by value to the sortShapes()
function. Lastly if 5, the program will change a bool to tell the program to exit.
getInput() will prompt the user to enter a name, using that name, it will determine the type
of shape to be added. Then it will receive the shape data such as its vertices, and then run a
check on it to determine if it is a valid shape. Lastly the shape is added to the shape array.
calculateArea() will call computeArea() method for each shape in the array and use setArea()
to set the area variable in that shape.
displayShapes() will output the toString() method for each shape in the array.
sortShapes() will prompt the user to enter an option for sorting the array until ‘q’ is entered,
which will exit the loop and return to the main menu. If ‘a’, the array is sorted in ascending
order of area and then sent to displayShapes() to display the shape information. If ‘b’, the
array is sorted in descending order of area and then sent to displayShapes() to display the
shape information. If ‘c’, the array is sorted into two categories, shapes containing warp
space are shown first, then shapes that do not. Within each category, shapes are further
sorted in descending order of area. Lastly, the array is then sent to displayShapes() to display
the shape information.
Misc. functions:
4. Reflection:
Through this assignment, I’ve improved my knowledge of c++ along with reinforcing the
things learned through the previous assignment. Some new things used here are vectors and
classes. Owing to my knowledge about classes from java, the coding process was relatively
smooth sailing. Something new I’ve learned is the usage of the -> operator and dynamic
casting.