Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please do it in Python 3. Assignment 9 - Changing Sort Keys Make sure you have read and understood both m odules A and B

Please do it in Python 3.image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

Assignment 9 - Changing Sort Keys

Make sure you have read and understood

both modules A and B this week, and

module 2R - Lab Homework Requirements

before submitting this assignment. Hand in only one program, please.

While it may appear long at first, this lab is easier than it looks, so stay calm. It is easier because:

it uses material and code that you learned, compiled and ran over a week ago when you studied Module 8, and

below, I give you very explicit directions on what to do for each of the very short methods you need to write.

You will start with my existing Student and StudentArrayUtilities classes that you have already compiled and run when you read the modules. Then you will modify each of these classes as described below and provide a new test client.

Understand the Application

Sort Flexibility (Student class)

In week 8, we saw an example of a Student class that provided a static compare_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 two Student objects, the foundation of SAUs sorting algorithm. Since a Student is a compound data type, not a float or a string, there was no pre-defined less-than, operation available, so we created our own compare_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 the compare_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 change SAU's float_largest_to_top() so that compare the points itself last name with the inflexibility of some other criteria.

So here's 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 three Studentattributes it wants future sort algorithms to use. The Student class will keep its compare_strings_ignore_case() static method, but add a new class methodcalled compare_two_students() (which can use the older method as a helper). This name doesn't commit to string vs. number, plus it gives the SAU class a unified method to invoke, Student.compare_two_students(), inside its float_largest_to_top(). The current sort key will inform compare_two_students() how to make the comparison.

No Output (SAU class)

Another change we'll 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 SAU's print_array() method with a to_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)

We'll add one more class method to SAU: double get_median_destructive( cls, array, array_size). This method will return the median of the total_tointsvalues 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.

We'll 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 (say Student.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 don't 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 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 static 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 student's points from the first student's points and returning that number.

Notice that whatever value the current Student.sort_key has, Student.compare_two_students() returns a value value that's negative if first_stud "comes before" second_stud, positive if first_stud "comes after" second_stud and 0 if they are the same -- interpreted through the current value of sort_key. (It doesn't 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 than compare_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 only Student's total_points. (This case can actually be skipped if you handle the next cases correctly, but it doesn't 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_points in order to get the medians, and that's easy since we already have the sort method. Then it has to find the middle-student's 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 Student class 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 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 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 client's sort_key when the method returns to client (see previous bullet).

If the user passes in a bad arraySize (

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 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:

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 client's 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.

Here's 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. ---------- ... etc After 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. ... etc After 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. ... etc After 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. ---------- ... etc Median of even class = ... Successfully preserved sort key. Median of odd class = ... Median of small class = ...
Assignment 9 - Changing Sort Keys Make sure you have read and understood both modules A and B this week, and module 2R - Lab Homework Requirements before submitting this assignment. Hand in only one program, please While it may appear long at first, this lab is easier than it looks, so stay calm. It is easier because 1. it uses material and code that you learned, compiled and ran over a week ago when you studied Module 8, and 2. below, I give you very explicit directions on what to do for each of the very short methods you need to write. You will start with my existing Student and StudentArrayUtilities classes that you have already compiled and run when you read the modules. Then you will modify each of these classes as described below and provide a new test client Understand the Application Sort Flexibility (Student class) In week 8, we saw an example of a Student class that provided a static compare strings_ignore case0 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 two Student objects, the foundation of SAUs sorting algorithm. Since a Student is a compound data type, not a float or a string, there was no pre-defined less-than,

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

Students also viewed these Databases questions

Question

the standard error and confidence intervals

Answered: 1 week ago

Question

5. Understand how cultural values influence conflict behavior.

Answered: 1 week ago