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

32.3 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 zyBooks--you do not need to submit code to Blackboard!
The PointList class consists of two data members:
list[] is an array of at most 50 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 0; 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 read-only. The Point files (Point.h, Point.cpp) contain the definition for the Point class that includes overloaded operators from Lecture 8.
You can download the files if you'd like to write your code outside of zyBooks.
Brief notes on each function:
void printList(ostream &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 points--if the slope between list[0] and list[1] matches the slope between list[1] and list[2], you can assume the slope between list[0] and list[2] 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 -0.000001 and 0.000001:
if (diff >=-0.000001 && diff <=0.000001)
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::printList(ostream &out){
if (np ==0){
out << "List empty" << endl;
} else {
for (unsigned int i =0; i < np; ++i){
out << list[i]<< endl; // Using the Point class's overloaded << operator
}
}
}
// isFull: Returns true if the list array is filled to capacity
bool PointList::isFull(){
return (np >=50);
}
// isLine: Returns true if all Points form a straight line
bool PointList::isLine(){
if (np <2){
return true; // Less than 2 points always form a "line"
}
// Calculate the slope between the first two points
double dx1= list[1].getX()- list[0].getX();
double dy1= list[1].getY()- list[0].getY();
double initialSlope;
// Avoid division by zero
if (dx1==0){
initialSlope = std::numeric_limits::infinity();
} else {
initialSlope = dy1/ dx1;
}
// Check slopes between consecutive points
for (unsigned int i =1; i < np -1; ++i){
double dx = list[i +1].getX()- list[i].getX();
double dy = list[i +1].getY()- list[i].getY();
double currentSlope;
// Avoid division by zero
if (dx ==0){
currentSlope = std::numeric_limits::infinity();
} else {
currentSlope = dy / dx;
}
// Check if slopes are approximately equal
if (std::abs(currentSlope - initialSlope)>0.000001){
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 blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!