Question

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

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 + "\n";

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

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

}

Tester

public class Assignment7Tester {
        
        public static void main( String args[]){
                University ourUniversity;
                Student aStudent;
                MyDate dateOfEntryToCanada;
                FinalUserFrame aFrame;
                ourUniversity = new University(10);
                aStudent = new CanadianStudentUnder65("John");
                ourUniversity.insertStudent(aStudent);
                aStudent = new CanadianStudentUnder65("Mary", 3);
                ourUniversity.insertStudent(aStudent);
                aStudent = new SeniorStudent("Tom", 3, 1500.0);
                ourUniversity.insertStudent(aStudent);
                dateOfEntryToCanada = new MyDate("25/05/2009");
                aStudent = new ForeignStudent("Xiao", 3, "China", dateOfEntryToCanada);
                ourUniversity.insertStudent(aStudent);
                dateOfEntryToCanada = new MyDate("12/08/2013");
                aStudent = new ForeignStudent("Jagjit", 5, "India", dateOfEntryToCanada);
                ourUniversity.insertStudent(aStudent);
                dateOfEntryToCanada = new MyDate("05/04/2008");
                aStudent = new ForeignStudent("Liz", 3, "Ireland", dateOfEntryToCanada);
                ourUniversity.insertStudent(aStudent);
                dateOfEntryToCanada = new MyDate("13/11/2011");
                aStudent = new ForeignStudent("Hong", 4, "China", dateOfEntryToCanada);
                ourUniversity.insertStudent(aStudent);
                aStudent = new SeniorStudent("Pat", 2, 2000.00);
                ourUniversity.insertStudent(aStudent);
                dateOfEntryToCanada = new MyDate("21/09/2009");
                aStudent = new ForeignStudent("Ting", 5, "China", dateOfEntryToCanada);
                ourUniversity.insertStudent(aStudent);
                aFrame = new FinalUserFrame(ourUniversity);
                aFrame.setVisible(true);
        }

}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

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;
           }
       }
   }
}

Add a comment
Know the answer?
Add Answer to:
will provide any other class if needed. Just need these 3 for now. Thank you You...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Implement the classes in the following class diagram. The Book class implements the Comparable interface. Use impl...

    Implement the classes in the following class diagram. The Book class implements the Comparable interface. Use implements Comparable<Book> in the class definition. Now, all book objects are instances of the java.lang.Comparable interface. Write a test program that creates an array of ten books. 1. Use Arrays.sort( Book[]l books) from the java.util package to sort the array. The order of objects in the array is determined using compareTo...) method. 2. Write a method that returns the most expensive book in the...

  • PLEASE READ ALL Modify the MergeSort code provided in class (if you find a mistake with...

    PLEASE READ ALL Modify the MergeSort code provided in class (if you find a mistake with it extra credit) (https://repl.it/@ashokbasawapatna/MergeSortExample) to work with Students (No need for generic types, you will only write Main.java). Then use the compareTo method for strings in Java to sort the students in Alphabetical order via MergeSort. 1) Create an array of 10 students by hand. Make sure it's not in alphabetical order. ONLY USE LOWER CASES FOR NAMES (I'll only test with that). 2)...

  • MERGESORT ASSIGNMENT FOLLOW THE FORMAT Modify the MergeSort code provided in class (if you find a...

    MERGESORT ASSIGNMENT FOLLOW THE FORMAT Modify the MergeSort code provided in class (if you find a mistake with it extra credit) (https://repl.it/@ashokbasawapatna/MergeSortExample) to work with Students (No need for generic types, you will only write Main.java). Then use the compareTo method for strings in Java to sort the students in Alphabetical order via MergeSort. 1) Create an array of 10 students by hand. Make sure it's not in alphabetical order. ONLY USE LOWER CASES FOR NAMES (I'll only test with...

  • Write java class “BinarySearch” that uses recursion to find the index of a target value in...

    Write java class “BinarySearch” that uses recursion to find the index of a target value in an ascending sorted array. If not found, the result is -1. The array has to be unsorted before you sort it. Use “ BinarySearchDriver.java” to drive the “BinarySearch” class /************************************************************* * BinarySearchDriver.java * Student Name * *. *************************************************************/ public class BinarySearchDriver { public static void main(String[] args) {     int[] array = new int[] {55, 88, 33, 5, 8, 12, 16, 23, 45}; /unsorted...

  • If I have a class named classroom, a classroom class consists of five methods: 1. void...

    If I have a class named classroom, a classroom class consists of five methods: 1. void add(T newStudent) //add a student 2. void set(int pos, T student) //changes the student stored at position pos to be the student parameter 3. T get(int pos)//returns the student at position pos. 4. int size() //num of student in a classroom 5. String toString() ------------------------------------------------------------------------------------------------------------------------------- In another class recursionWS static <T> boolean studentInList (classroom<T> list1, classroom<T> list2){ //this method returns true if students in...

  • I need code in java The Student class: CODE IN JAVA: Student.java file: public class Student...

    I need code in java The Student class: CODE IN JAVA: Student.java file: public class Student {    private String name;    private double gpa;    private int idNumber;    public Student() {        this.name = "";        this.gpa = 0;        this.idNumber = 0;    }    public Student(String name, double gpa, int idNumber) {        this.name = name;        this.gpa = gpa;        this.idNumber = idNumber;    }    public Student(Student s)...

  • Write a method called removeDuplicates that accepts a PriorityQueue of integers as a parameter and modifies...

    Write a method called removeDuplicates that accepts a PriorityQueue of integers as a parameter and modifies the queue’s state so that any element that is equal to another element in the queue is removed. For example, if the queue stores [7, 7, 8, 8, 8, 10, 45, 45], your method should modify the queue to store [7, 8, 10, 45]. You may use one stack or queue as auxiliary storage. Please also create a Main Program to test the code....

  • In the USIntsArrayList Class, implement the USIntsArrayListInterface Interface (which is really just the UnSortedInts Class without...

    In the USIntsArrayList Class, implement the USIntsArrayListInterface Interface (which is really just the UnSortedInts Class without the logic. All you are doing in this class is providing the same functionality as UnSortedInts, but using an ArrayList to hold the data. Document appropriately Thank you, package arrayalgorithms; import java.util.ArrayList; /** * Title UnSorted Ints stored in an Array List * Description: Implement all the functionality of the UnSortedInts * class using an ArrayList * @author Khalil Tantouri */ public class USIntsArrayList...

  • FOR JAVA: Summary: Create a program that adds students to the class list (see below). The...

    FOR JAVA: Summary: Create a program that adds students to the class list (see below). The solution should be named Roster402_v2.java. Allow the user to control the number of students added to the roster. Ask if the user would like to see their new roster to confirm additions. If yes, then display contents of the file, if no, end the program. ------------------------------------------------------------------------------------- List of student names and IDs for class (this will be your separate text file): Jones, Jim,45 Hicks,...

  • Writing 3 Java Classes for Student registration /** * A class which maintains basic information about...

    Writing 3 Java Classes for Student registration /** * A class which maintains basic information about an academic course. */ public class Course {    /** * Attributes. */ private String code; private String title; private String dept; // name of department offering the course private int credits; /** * Constructor. */ public Course(String code, String title, int credits) { // TODO : initialize instance variables, use the static method defined in // Registrar to initialize the dept name variable...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT