[Solved] Assignment 9 Changing Sort Keys

$25

File Name: Assignment_9__Changing_Sort_Keys.zip
File Size: 301.44 KB

SKU: [Solved] Assignment 9 – Changing Sort Keys Category: Tag:
5/5 - (1 vote)

In week 8, we saw an example of a Student class that provided a staticcompare_strings_ignore_case()method. We needed to define such a method because our sort algorithm(which was in the StudentArrayUtilities (SAU) class) had to have a basis for comparing twoStudentobjects, the foundation of SAUs sorting algorithm. Since a Student is a compound data type, not afloat or a string, there was no pre-defined less-than, <, operation available, so we created our owncompare_strings_ignore_case(), which was based on the spelling of the last name. You can review those notes to see its definition.

We want to make the sort more flexible in this assignment. Sometimes we want to sort on last name, as we did in the lectures, but other times we may choose to sort on the first name (useful for informal communication) or total points (useful for analyzing statistics). We could go into thecompare_strings_ignore_case() method and change its definition to work on first name. Or, we could rename it to compare_two_numbers() and base it on total points (or for that possibility just changeSAUsfloat_largest_to_top() so that compare the points itself < or <=, no method call needed. But, such solutions only replace the inflexibility of last name with the inflexibility of some other criteria.

So heres the plan: we will provide class Student with a new class method set_sort_key() 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 an argument to set_sort_key() telling it which one of the threeStudentattributes it wants future sort algorithms to use. The Student class will keep itscompare_strings_ignore_case() static method, but add a new class methodcalledcompare_two_students()(which can use the older method as a helper). This name doesnt commit tostring vs. number, plus it gives the SAU class a unified method to invoke,Student.compare_two_students(), inside itsfloat_largest_to_top(). The current sort key will informcompare_two_students() how to make the comparison.

No Output (SAU class)

Another change well make affects the output modality. Rather than displaying the array directly in the SAU class, we want to make the class U.I. neutral. So we will replace SAUs print_array() method with ato_string() method and let the client choose how to use that. In our example main program we will be sending the string array to the console.

Median (SAU class)

Well add one more class method to SAU: double get_median_destructive( cls, array, array_size). This method will return the median of the total_toints values in an array. Look up median. It is defined as the middle-value and is easily computed, but you first have to sort the array in order to find it. Fortunately, you already have the sort method.

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.

Well test the sort_key and sort algorithm only on the even numbered array, and then we will test our median computation on all three arrays.

The Program Spec

These changes should not be complicated as long as you read carefully and follow directions. New and modified members/methods are all very short, so stay focused and apply what you learned back in week 8.

Additions to the Student Class

We will add the following static members to the class in the modules.

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 (sayStudent.SORT_BY_FIRST) to the setter described next. I have intentionally made the literal values non-contiguous so you would not rely on their particular values in your logic. You should be able to change the values without breaking your program (but you dont have to change them; use the three values above).

static int:

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

You should supply the following simple class methods:

def set_sort_key(cls, key): a mutator for the member sortKey.

def get_sort_key(cls): an accessor for sortKey.

A new class comparison method:

def compare_two_students(cls, first_stud, second_stud): This method has to look at the sort_key and compare the two Students based on the currently activesort_key. It will return a number as follows:

If sort_key is SORT_BY_FIRST it will use the two students first members to make the comparison. It will do this by passing those two values to compare_strings_ignore_case(), and passing up the returned value directly to the client.

If sort_key is SORT_BY_LAST it will use the two students last members to make the comparison. It will do this by passing those two values to compare_strings_ignore_case(), and passing up the returned value directly to the client.

If sort_key is SORT_BY_POINTS it will use the two students total_points members to make the comparison. It will do this by subtracting the second students points from the first students points and returning that number.

Notice that whatever value the current Student.sort_key has, Student.compare_two_students() returns a value value thats negative if first_stud comes before second_stud, positive if first_stud comesaftersecond_stud and 0 if they are the same interpreted through the current value of sort_key. (It doesnt matter what the the exact value returned besides its being +, or 0.)

Change to the StudentArrayUtilities Class

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

Adjust float_largest_to_top() to call compare_two_students() rather thancompare_strings_ignore_case(). Make adjustments required by this change.

Add a class method get_median_destructive(cls, array, array_size): This computes and returns the median of the total scores of all the students in the array The details are simple, but you have to take them each carefully:

Dispose of the case of a one-element array. A one-element array returns its one and onlyStudentstotal_points. (This case can actually be skipped if you handle the next cases correctly, but it doesnt hurt to do it separately, here.)

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 total_pointsin order to get the medians, and thats easy since we already have the sort method. Then it has to find the middle-students score (e.g., if the array is size 21, the middle element is the score inarray[10], after the sort). But, before doing the sort, it also has to change the sort_key of the Studentclass to SORT_BY_POINTS. One detail, that you may not have thought of, is that, at the very start of the method, it needs to save the clients sort key. Then, before returning, restore the clients sort key. This method doesnt know what that sort key might be, but there is an accessor get_sort_key()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 sort_key when the method returns to client (see previous bullet).

If the user passes in a bad arraySize (< 1) return a 0.

The Main Program

Our client will declare three 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 extraStudenttagged onto the end. Each array should be initialized in no particular order: unsorted in all fields.

Using the largest, even numbered, array:

display the array immediately before calling a sort method,

sort the array using the default (initial) sort key and display,

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

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

set_sort_key() to first name, call the get_median_destructive() method and display the median score. and finally

call get_sort_key() to make sure that the get_median_destructive() method preserved the clients sort_key value of first name that was just set prior to the get_median_destructive() call.

Using each of the two other arrays:

get the median of each array and display. No other testing needed in this part.

Heres a sample output, but you must not use my arrays. Make your own as per the spec above.

Before default sort (even): ----------     name: smith, fred    total points: 95. ----------     name: bauer, jack    total points: 123. ----------     name: jacobs, carrie    total points: 195. ----------  ... etcAfter default sort (even):  ----------     name: bauer, jack    total points: 123. ----------     name: cassar, john    total points: 321. ----------     name: charters, rodney    total points: 295. ----------     name: jacobs, carrie    total points: 195. ... etcAfter sort BY FIRST:  ----------     name: renquist, abe    total points: 148. ----------     name: jacobs, carrie    total points: 195. ----------     name: loceff, fred    total points: 44. ----------     name: perry, fred    total points: 225.  ... etcAfter sort BY POINTS:  ----------     name: loceff, fred    total points: 44. ----------     name: smith, fred    total points: 95. ----------     name: zz-error, trevor    total points: 108. ----------     name: bauer, jack    total points: 123. ----------  ... etcMedian of even class =   ...Successfully preserved sort key.Median of odd class = ... Median of small class = ...

Reviews

There are no reviews yet.

Only logged in customers who have purchased this product may leave a review.

Shopping Cart
[Solved] Assignment 9 Changing Sort Keys
$25