Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Assignment 9 - Changing Sort Keys While it may appear long at first, this lab is easier than it looks, so stay calm. It is

Assignment 9 - Changing Sort Keys

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 from 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 compareTwoStudents() 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 double or a String, there was no pre-defined compareTo() available, so we created our own compareTwoStudents(), 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 compareTwoStudents() method and change its definition to work on first name or total points, but all that does is replace the inflexibility of last name with the inflexibility of some other criteria. So here's the plan: we will 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.

Console Output (SAU class)

Another change we'll make affects the output modality. Rather than using JOptionPane, we want to make the class "U.I. neutral." So we will 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, only.

Median (SAU class)

We'll add one more method to our SAU class: double getMedianDestructive( Student[] someArray ). 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, 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 four Student arrays to make sure our median works: one array that had an odd number of students (15), one that has an even number of students (16), one that has a single student and one that has no students(we will define the median of an array with no elements to be 0.0).

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.

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 members to the class in the modules.

Public static int constants (finals):

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).

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 active 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 greaterthan 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 public static method double getMedianDestructive(Student[] array) - This computes and returns the medianof 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 cases of an empty array (0 elements) and one-element array. Empty arrays return 0.0, always, and one-element array returns its one and only Student's totalPoints. (This second 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 totalPoints 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 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 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 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 client's sortKey when the method returns to client (see previous bullet).

The Foothill main()

Our client will declare four Student arrays: using direct initialization, as in the modules: no user input. The array sizes should be 15, 16, 1 and 0. 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,

setSortKey() to first name, call the getMedianDestructive() method and display the median score. and finally

call getSortKey() to make sure that the getMedianDestructive() method preserved the client's sortKey value of first name that was just set prior to the getMedianDestructive() call.

Using each of the other three 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.

image text in transcribed

i Chrome File Edit View History Bookmarks People Window Help 8 100% Wed 8:46 PM v LAB Assignment 9-Changinc x Chegg Study | Guided Solutio Secure https://foothillcollege.instructure.com/courses/5747assignments/120101#submit Here's a sample output, but you must not use my arrays. Make your own as per the spec above. FOOTHILL COLLEGE Before: smith, fred points: 95 bauer, jack points: 123 jacobs, carrie points: 195 renquist, obe points: 148 22-error, trevor points: 108 perry, fred points: 225 Loceff, fred points: 44 stollings, pamela points: 452 charters, rodney points: 295 cassar, john points: 321 Dashboard Sorting by default After: 28 bauer, jack points: 123 cassar, john points: 321 charters, rodney points: 295 jacobs, carrie points: 195 Loceff, fred points: 44 perry, fred points: 225 renquist, abe points: 148 smith, fred points: 95 stollings, pamela points: 452 zz-error, trevor points: 188 Calendar Inbox Sorting by first name After: Help renquist, obe points: 148 jacobs, carrie points: 195 Loceff, fred points: 44 perry, fred points: 225 smith, fred points: 95 bauer, jack points: 123 cassar, john points: 321 stollings, pomela points: 452 charters, rodney points: 295 zz-error, trevor points: 108 k

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

More Books

Students also viewed these Databases questions

Question

2. Do you agree that unions stifle creativity? Why or why not?

Answered: 1 week ago