Question: 3 2 . 3 Midterm Exam Problems: Classes This section contains resources for you to complete the questions related to classes on the exam. All
Midterm Exam Problems: Classes
This section contains resources for you to complete the questions related to classes on the exam. All problems deal with a class, PointList that stores a collection of Point objects like the ones we discussed in class. Make sure you run your code in submit mode in zyBooksyou do not need to submit code to Blackboard!
The PointList class consists of two data members:
list is an array of at most Point objects
np is an unsigned integer representing the number of Point objects in the list.
The default constructor for this class initializes np to ; it is incremented in the addPoint function every time a new Point is added.
You must write the following member functions for the PointList class:
printList
isFull
isLine
The only file you need to modify is PointList.cpp which contains space to write the definition for each function. All other files are marked as readonly. The Point files Pointh Point.cpp contain the definition for the Point class that includes overloaded operators from Lecture
You can download the files if you'd like to write your code outside of zyBooks.
Brief notes on each function:
void printListostream &out: Prints every Point in list, one per line. Formatting of each Point is determined by Point output operator.
If the list is empty, this function should print "List empty", then move the cursor to the next line.
bool isFull: Returns true if the list array in the calling object is filled to capacity and false otherwise.
bool isLine: Returns true if the collection of Point objects in the list represents a straight line and false otherwise.
For a list of points to represent a line, the slope between any pair of points must be the same. Your function should therefore check the slope between each consecutive pair of points to make sure they all match. You don't have to check every pair of pointsif the slope between list and list matches the slope between list and list you can assume the slope between list and list is the same, also.
Also, because the slope is a value of type double, it's possible that small rounding errors would make two "equal" slopes appear to be unequal, leading your program to fail a test case. I therefore recommend testing equality by checking if the difference between slopes is relatively small. For example, the simple conditional statement below prints "Good enough" if the variable diff is between and :
if diff && diff
cout "Good enough
;
code:
#include "PointList.h
#include
#include For std::abs
using namespace std;
printList: Prints every Point in the list, one per line
void PointList::printListostream &out
if np
out "List empty" endl;
else
for unsigned int i ; i np; i
out listi endl; Using the Point class's overloaded operator
isFull: Returns true if the list array is filled to capacity
bool PointList::isFull
return np ;
isLine: Returns true if all Points form a straight line
bool PointList::isLine
if np
return true; Less than points always form a "line"
Calculate the slope between the first two points
double dx listgetX listgetX;
double dy listgetY listgetY;
double initialSlope;
Avoid division by zero
if dx
initialSlope std::numericlimits::infinity;
else
initialSlope dy dx;
Check slopes between consecutive points
for unsigned int i ; i np ; i
double dx listi getX listigetX;
double dy listi getY listigetY;
double currentSlope;
Avoid division by zero
if dx
currentSlope std::numericlimits::infinity;
else
currentSlope dy dx;
Check if slopes are approximately equal
if std::abscurrentSlope initialSlope
return false; Slopes differ, so not a line
return true; All slopes matched
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
