Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Assignment 9 - Changing Sort Keys While it may appear long at first, this lab is

ID: 3727450 • Letter: A

Question

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: 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 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 compare TwoStudents() 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(0) 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)

Explanation / Answer

import java.lang.Math;

import java.util.Arrays;

public class StudentAlgorithmUtility

{

public static final int SORT_BY_FIRST = 88;

public static final int SORT_BY_LAST = 98;

public static final int SORT_BY_POINTS = 108;

//This "main" method is for example

public static void main(String[] args)

{   

Student s1 = new Student("Michael","Jackson",128);

Student s2 = new Student("Michael","Jackson",150);

  

Student[] arrayStudent = {s1,s2};

s1.setSortKey(SORT_BY_POINTS);   

System.out.print(s1.compareTwoStudents(s2));

StudentAlgorithmUtility studentAlgorithmUtility = new StudentAlgorithmUtility();

studentAlgorithmUtility.getMedianDestructive(arrayStudent);

}

  

public double getMedianDestructive(Student[] studentArray)

{   

myStudentArray = studentArray;

//Call existing sort array method to sort the student array

if(studentArray.length == 0)

{

return 0.0;

}

boolean isOdd = IsArrayLengthOdd(studentArray.length);

int median = 0;

if(isOdd)

{

median = (int)Math.ceil(studentArray.length/2);  

return studentArray[median-1].totalPoints;

}

else

{

median = studentArray.length/2;

double indexPointValue1 = studentArray[median-1].totalPoints;

double indexPointValue2 = studentArray[median].totalPoints;

return (indexPointValue1 + indexPointValue2)/2;

}

  

  

}

  

private boolean IsArrayLengthOdd(int studentArrayLength)

{   

if((studentArrayLength % 2) == 0)

{

return false;

}

else

{

return true;

}

}

  

public String toString()

{

return Arrays.toString(myStudentArray);

}

  

private Student[] myStudentArray;

}

// you can add other public classes to this editor in any order

public class Student

{

private static int sortKey;

private String firstName;

private String lastName;

public double totalPoints;

  

public Student(String firstName,String lastName,double totalPoints)

{

this.firstName = firstName;

this.lastName = lastName;

this.totalPoints = totalPoints;

}

public int compareTwoStudents(Student object)

{

int result = 0;

switch(sortKey)

{

case 88:

result = this.firstName.compareTo(object.firstName);

break;

  

case 108:

Double value1 = new Double(this.totalPoints);

Double value2 = new Double(object.totalPoints);

result = value1.compareTo(value2);

break;

  

default:

result = this.lastName.compareTo(object.lastName);

break;   

}

return result;

}

  

public static boolean setSortKey(int key)

{

sortKey = key;

return true;

}   

  

public static int getSortKey()

{

return sortKey;

}

}