Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please help with this assignment to modify the given C++ program below: #include #include #include using namespace std; // class Student prototype ----------------------- class Student

Please help with this assignment  to modify the given C++ program below: 
#include  #include  #include  
using namespace std; // class Student prototype ----------------------- class Student { private: string lastName; string firstName; int totalPoints; public: static const string DEFAULT_NAME; static const int DEFAULT_POINTS = 0; static const int MAX_POINTS = 1000; public: Student( string lst = DEFAULT_NAME, string fst = DEFAULT_NAME, long pts = DEFAULT_POINTS); // accessors and mutators string getLastName() { return lastName; } string getFirstName() { return firstName; } int getTotalPoints() { return totalPoints; } 
 bool setLastName(string last); bool setFirstName(string first); bool setPoints(int pts); 
 static int compareTwoStudents( Student firstStud, Student secondStud ); string toString(); 
private: static bool validString( string testStr ); static bool validPoints( int testPoints ); }; // end of class Student prototype -------------- 
// class StudentArrayUtilities prototype ----------------------- class StudentArrayUtilities { public: static void printArray(string title, Student data[], int arraySize); static void arraySort(Student array[], int arraySize); 
private: static bool floatLargestToTop(Student data[], int top); static void mySwap(Student &a, Student &b); }; // static initializations that can't be done in-line const string Student::DEFAULT_NAME = "zz-error"; 
int main() { Student myClass[] = { Student("smith","fred", 95), Student("bauer","jack",123), Student("jacobs","carrie", 195), Student("renquist","abe",148), Student("3ackson","trevor", 108), Student("perry","fred",225), Student("loceff","fred", 44), Student("stollings","pamela",452), Student("charters","rodney", 295), Student("cassar","john",321) }; 
 int arraySize = sizeof(myClass) / sizeof(myClass[0]); 
 StudentArrayUtilities::printArray("Before: ", myClass, arraySize); StudentArrayUtilities::arraySort(myClass, arraySize); StudentArrayUtilities::printArray("After: ", myClass, arraySize); } 
// constructor requires parameters - no default supplied Student::Student( string last, string first, long points) { if ( !setLastName(last) ) lastName = DEFAULT_NAME; if ( !setFirstName(first) ) firstName = DEFAULT_NAME; if ( !setPoints(points) ) totalPoints = DEFAULT_POINTS; } 
bool Student::setLastName(string last) { if ( !validString(last) ) return false; lastName = last; return true; } 
bool Student::setFirstName(string first) { if ( !validString(first) ) return false; firstName = first; return true; } 
bool Student::setPoints(int pts) { if ( !validPoints(pts) ) return false; totalPoints = pts; return true; } 
// could be an instance method and, if so, would take one parameter int Student::compareTwoStudents( Student firstStud, Student secondStud ) { int result; // this particular version based on last name only (case insensitive) result = firstStud.lastName.compare(secondStud.lastName); 
 return result; } 
string Student::toString() { string resultString; ostringstream cnvrtFirst, cnvrtLast, cnvrtPoints; 
 cnvrtFirst << firstName; cnvrtLast << lastName; cnvrtPoints << totalPoints; 
 resultString = " "+ cnvrtLast.str() + ", " + cnvrtFirst.str() + " points: " + cnvrtPoints.str() + " "; return resultString; } 
bool Student::validString( string testStr ) { if (testStr.length() > 0 && isalpha(testStr[0])) return true; return false; } 
bool Student::validPoints( int testPoints ) { if (testPoints >= 0 && testPoints <= MAX_POINTS) return true; return false; } // print the array with string as a title for the message box void StudentArrayUtilities::printArray(string title, Student data[], int arraySize) { string output = ""; 
 cout << title << endl; 
 // build the output string from the individual Students: for (int k = 0; k < arraySize; k++) output += " "+ data[k].toString(); cout << output << endl; } 
void StudentArrayUtilities::arraySort(Student array[], int arraySize) { for (int k = 0; k < arraySize; k++) // compare with method def to see where inner loop stops if (!floatLargestToTop(array, arraySize-1-k)) return; } 
// returns true if a modification was made to the array bool StudentArrayUtilities::floatLargestToTop(Student data[], int top) { bool changed = false; 
 // compare with client call to see where the loop stops for (int k =0; k < top; k++) if ( Student::compareTwoStudents(data[k], data[k+1]) > 0 ) { mySwap(data[k], data[k+1]); changed = true; } return changed; } 
void StudentArrayUtilities::mySwap(Student &a, Student &b) { Student temp("", "", 0); 
 temp = a; a = b; b = temp; } // end of StudentArrayUtilities method definitions. 

For this assignment, we're asked to modify the program per below:

1. We want the sort more flexible, by last name, first name or total points. Provide a static Student class method called setSortKey() which will establish a new sort criteria. A client will invoke it whenever it wants to switch to a new basis for comparison. The client will pass a parameter to setSortKey() telling it which one of the three Student fields it wants future sort algorithms to use.

2. No Output (SAU class): No displaying the array directly in the SAU class because we want to make the class "U.I. neutral." So we need to replace SAU 's printArray() method with a toString() method, and let the client choose how to use that. In our example main() we will be sending the string array to the console.

3. Median (SAU class): We'll add one more method to our SAU class: double getMedianDestructive(Student[] som eArray ) . This method will return the median of the totalPoints values in an array. Look up median. It is defined as the "middle-value" and is easily computed.

4. Client: Our client will declare three Student arrays to make sure our median works: one array that has an odd number of students (15), one that has an even number of students ( 16) and one that has a single student.

We'll test the sortKey and sort algorithm only on the even numbered array, and then we will test our median computation on all four arrays.

5. The Program Spec:

Additions to the Student Class: We will add the following members to the class in the modules.

Public static int consts:

SORT_BY_FIRST = 88

SORT_BY_LAST = 98

SORT_BY_POINTS = 108

These are the three sort keys that will be used by the client and the class, alike, to keep track of, or set, the sort key. If the client wants to establish a new sort key, it will pass one of these tokens (say Student::SORT_BY_FIRST) to the setter described next.

Private static int:

sortKey this will always have one of the three constants above as its value. Make sure it initially has SORT_BY_LAST in it, but after the client changes it, it could be any of the above constants.

You should supply the following simple public static methods:

boolean setSortKey( int key ) a mutator for the member sortKey .

int getSortKey() an accessor for sortKey .

Modification to the Student Class

compareTwoStudents( ... ) same signatures as in the modules, but now this method has to look at the sortKey and compare the two Students based on the currently activce sortKey . A switch statement with three different expressions is all you need, and each expression will be very similar to the one already in the modules (in fact one will be identical).

As you saw in the modules, it needs to return an int , which is positive, if the first student is greater than the second, negative if less than, and zero if they are the same, based on the current value of sortKey, of course.

Change to the StudentArrayUtilities Class

Replace printArray() with toString(). Generate the same kind of string , but instead of sending it to the screen, return it to the client.

Add a static method double getMedianDestructive(Student array[], intarraySize) This computes and returns the median of the total scores of all the students in the array:

Dispose of the case of a one-element array. A one-element array returns its one and only Student 's totalPoints.

Even-numbered arrays >= 2 elements: find the two middle elements and return their average of their total points.

Odd-numbered arrays >= 3 elements: return the total points of the exact middle element.

Special Note : This method has to do the following. It must sort the array according to totalScore in order to get the medians. Then it has to find the middle-student's score (e.g., if the array is size 21, the middle element is the score in array[10], after the sort). But, before doing the sort, it also has to change the sortKey of the Student class to SORT_BY_POINTS. One detail, that you may not have though of, is that, at the very start of the method, it needs to save the client's sort key. Then, before returning, restore the client's sort key. This method doesn't know what that sort key might be, but there is an accessor getSortKey() that will answer that question.

This method has the word " Destructive " in its name to remind the client that it may (and usually will) modify the order of the array, since it is going to sort the array by total points in the process of computing the median. However, it will not destroy or modify the clients sortKey when the method returns to client.

The main() driver

Our client will declare four Student arrays: using direct initialization, as in the modules: no user input. The array sizes should be 15, 16 and 1. The second array can be the same as the first with one extra Student tagged onto the end. Each array should be initialized in no particular order: unsorted in all fields. Using the largest, even numbered, array:

1. display the array immediately before calling a sort method.

2. sort the array using the default (initial) sort key and display.

3. change the sort key to first name, sort and display.

4. change the sort key to total score, sort and display.

5. setSortKey() to first name, call the getMedian() method and display the median score. Then call and, getSortKey() to make sure that the getMedian() method preserved the client's sortKey value of first name that was just set prior to the getMedian() call.

Using each of the two other arrays:

get the median of each array and display.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2022 Grenoble France September 19 23 2022 Proceedings Part 4 Lnai 13716

Authors: Massih-Reza Amini ,Stephane Canu ,Asja Fischer ,Tias Guns ,Petra Kralj Novak ,Grigorios Tsoumakas

1st Edition

3031264118, 978-3031264115

More Books

Students also viewed these Databases questions