due in 12 hours
CIS 25 SPRING 2020
FINAL
Due 11:59 PM May 22 (this is a hard deadline)
SHORTER EXERCISES (30 pts each)
Upload your answers to these questions to canvas. Give your files sensible names (i.e. I
should be able to see from the filename which response goes with which question).
Follow the style guide. Upload separate .h and .cpp files for questions asking you to
implement classes.
1) Write a class declaration and definition for a class named Circle with three private variables:
* A double named xPosition
* A double named yPosition
* A double named radius
This class should have three constructors: a default constructor that sets the radius to 1 and
both xPosition and yPosition to 0, a one-parameter constructor that sets radius to the parameter
and xPosition and yPosition to 0, and a three-parameter constructor that allows client code to
set all three parameters.
The class should also have accessors for all three instance variables and mutators allowing
client code to set xPosition, yPosition, and radius. If at any point client code attempts to set the
radius to a value below 0, the radius should be set to 0 instead.
Put the class declaration in a header file and the class definition in a source code file. It should
not be possible to include the header file more than once in any program.
2.) Write a recursive method that takes the following parameters:
* a sorted array of ints
* an int representing the size of the array
* an int representing a value to be searched for
Your function should return a bool. If the element is in the array, return true. Otherwise, return
false. Your function should never produce memory access errors, regardless of the size of the
array.
3) A priority queue is a data structure wherein every item added to the structure has an
associated priority. When items are removed from a priority queue, the highest-priority elements
are removed first. Implement a class called PriorityQueue that stores strings. Each string in the
queue should have an associated priority (represented as an int). You may find it useful to
define a struct containing the string and its priority. You may use a vector to store elements in
your PriorityQueue.
3) Create a class named Pet with private instance variables representing the pet’s name as a
string and its cutenessLevel as an int. Create appropriate accessors, mutators, and constructors
for Pet objects. Client code should never be able to set the cuteness level of Pet objects below
3.
Write a derived class named Dog. It should not be possible to set a Dog’s cuteness level below
10, regardless of how the Dog object is accessed (i.e. even if you refer to it using a reference or
pointer to a Pet) your class should correctly refuse to set the object’s cuteness below 10.
LONGER PROGRAMMING EXERCISE: (60 pts)
Create a class named MyVector. MyVector should include a pointer named arrayPtr that
points to a dynamic array, a capacity variable that holds the dynamic array’s current capacity,
and a currentSize variable that gives the number of elements currently stored in the array.
These variables should all be private. Include a public function named changeElement. This
function should take two parameters: the value of an element, and the index at which the
element should be placed. If the index is greater than the current size, the element should be
added to the end of the array. Include another public function called pushBack, which adds an
element to the end of the array. When adding elements to the end of the array, resize the array if
necessary. Include an accessor named at that takes one int parameter and returns the element
at that index. If the index is higher than the highest used index, at should cause the program to
exit immediately.
Include a default constructor (that takes no parameters) that creates an array of capacity 16.
Include a constructor that takes a positive nonzero int parameter and creates an array of
capacity equal to that parameter.
Include a destructor that frees all allocated memory when objects of the MyVector class go out
of scope.
YOUR MyVector IMPLEMENTATION SHOULD BE ABLE TO STORE ANY ONE TYPE OF
ELEMENT (use templates for this). Create a short main function that tests the functionality of
MyVector. Your class should at no point leak memory or attempt to read from/write to
unallocated memory.