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

will provide any other class if needed. Just need these 3 for now. Thank you You

ID: 3855386 • Letter: W

Question

will provide any other class if needed. Just need these 3 for now.

Thank you

You will make one change to the Sort class. You will modify the sortAnything method such that it now accepts a boolean isDescending. If isDescending is false, sortAnything will behave normally (sorting the student in ascending order). If isDescending is true, however, sortAnything will sort in descending order instead.

Class University:

Modify sortStudents such that it takes a boolean, isDescending, and uses that when calling Sort’s method sortAnything.

Class FinalUserFrame:

FinalUserFrame extends JFrame and has a label, a text field, a text area, and three buttons (see the layout below). It should use a flow layout manager and its dimensions should initially be 700 x 400. The label should say “Enter name of Country”. The input field (which is the text field) should allow 20 characters. The text field (called countryInputField) should allow input such that when the user inputs the name of a country, the text area (called outputField) should show how many students in the university are from that country. For instance, if Canada is entered into the input field, the output field should show “Number of Students from Canada is 4”. The text area should be 15 x 50. As mentioned, there are three buttons. The first is a “Show Students” button. When this button is clicked, the entire (unsorted) list of students is shown in the output field. This list should always remain unsorted, the students should be shown in the order in which they were created.

Sort

public class Sort {
public static void sortAnything(Sortable listObjects[], int numObjects){
Sortable temp;
int indexSmallest, index1, index2;
for (index1 = 0; index1 < numObjects - 1; index1++){
indexSmallest = index1;
for (index2 = index1 + 1; index2 < numObjects; index2++)
if (listObjects[index2].lessThan(listObjects[indexSmallest]))
indexSmallest = index2;
temp = listObjects[index1];
listObjects[index1] = listObjects[indexSmallest];
listObjects[indexSmallest] = temp;
}
}
}

University

public class University {
private Student [] listOfStudents;
private static int howManyStudents = 0;

public University(int maximumNumberOfStudents){
listOfStudents = new Student [maximumNumberOfStudents];
}

public boolean insertStudent(Student aStudent){
boolean result = false;
int i=0;
while(listOfStudents[i] != null){
i++;
}
listOfStudents[i] = aStudent;
if(i==10){
result = false;
}else{
result = true;
}
howManyStudents++;
return result;
}

public int numberOfStudents(String nameOfCountry){
int canadianStudent = 0;
int foreignStudent = 0;
int result = 0;

if(nameOfCountry.equals("Canada")){
for(Student list: listOfStudents){
if(list instanceof Student){
if (list.findCountry().equals("Canada")){
canadianStudent++;
}
}
result = canadianStudent;
}
}
if(nameOfCountry.equals("China")){
for(Student list2: listOfStudents){
if(list2 instanceof Student){
if (list2.findCountry().equals("China")){
foreignStudent++;
}
}
result = foreignStudent;
}
}
return result;
}

public String toString(){
String Student = "Number of students in University = " + howManyStudents + " ";

for(Student s: listOfStudents){
if(s != null){
Student += s.toString() + " ";
}
}
return Student;
}

public void sortStudents(){
Sort.sortAnything(listOfStudents, howManyStudents);
}

}

Tester

Explanation / Answer

Note: Complete code not given so not able test the code, required modification done in the given method.

Solution:

class Sort
{
   public static void sortAnything(Sortable listObjects[], int numObjects, boolean isDescending)
   {
       if(isDescending==false)
       {
           Sortable temp;
           int indexSmallest, index1, index2;
           for (index1 = 0; index1 < numObjects - 1; index1++)
           {
               indexSmallest = index1;
               for (index2 = index1 + 1; index2 < numObjects; index2++)
               if (listObjects[index2].lessThan(listObjects[indexSmallest]))
               indexSmallest = index2;
               temp = listObjects[index1];
               listObjects[index1] = listObjects[indexSmallest];
               listObjects[indexSmallest] = temp;
           }
       }
       if(isDescending==true)
       {
           Sortable temp;
           int indexLargest, index1, index2;
           for (index1 = 0; index1 < numObjects - 1; index1++)
           {
               indexLargest = index1;
               for (index2 = index1 + 1; index2 < numObjects; index2++)
               if (listObjects[index2].greaterThan(listObjects[indexLargest]))
               indexLargest = index2;
               temp = listObjects[index1];
               listObjects[index1] = listObjects[indexLargest];
               listObjects[indexLargest] = temp;
           }
       }
   }
}