Java

OOP GUIDE / WORK

 

STUDENT CLASS 2 SOLUTIONS

 

 

TASK – PART 1 – SETUP

 

No solutions required.

 

 

TASK – PART 2 – SORTING BASED ON A COMPARE MODE

 

Below is my solution. Note that I opted to create three methods that I call from inside the compareTo method to keep things organized.  Also, to avoid having to remember which mode number is which one, I create three static methods that can be used to set the mode value.

 

 

    

public class Student implements Comparable

{

     public String lastName;

     public String firstName;

     public int grade;

     public String school;

     public static int compareMode;   //all students share one static var

    

     public Student()

     {

           String[] lastNames = {"Smith", "Do", "Bardot", "Lu", "Green", "Lowe", "Bacon", "Zoom", "Gravy", "TwoShoes", "Monroe", "Hope", "West", "Lopez", "Gray"};

           lastName = lastNames[(int)(Math.random() * lastNames.length)];

          

           String[] firstNames = {"Jimmy", "June", "Abbott", "Luna", "Harry", "Moon", "Lea", "Kayla", "Aryan", "Grace", "Drake", "Kiana", "Burt", "Amitoj", "Xavier", "Shaun", "Mary"};

           firstName = firstNames[(int)(Math.random() * firstNames.length)];

          

           grade = (int)(Math.random() * 4) + 9;

          

           String[] schools = {"Lockerby", "Confed", "Lasalle", "Lo-Ellen"};

           school = schools[(int)(Math.random() * schools.length)];

          

           compareMode = 1;

     }

    

     public String toString()

     {

           return grade + " " + firstName + " " + lastName + " (" + school + ")";

     }

 

     public static void compareByLastName()

     {

           compareMode = 1;

     }

    

     public static void compareBySchool()

     {

           compareMode = 2;

     }

    

     public static void compareByGrade()

     {

           compareMode = 3;

     }

    

     @Override

     public int compareTo(Object o)

     {

           if (compareMode == 1)

                return compareToByLastName(o);

           else if (compareMode == 2)

                return compareToBySchool(o);

           else

                return compareToByGrade(o);

     }

    

     public int compareToBySchool(Object o)

     {   

           Student other = (Student)o;

           return this.school.compareTo(other.school);

     }

    

     public int compareToByLastName(Object o)

     {

           Student other = (Student)o;

           return this.lastName.compareTo(other.lastName);

           //Note: String's compareTo compares two Strings alphanumerically.

           //It returns -1, 0 or 1 in the same way as compareTo does.  Very convenient!

          

           //Note: Could improve this to have first name serve as tie breaker.

     }

    

     public int compareToByGrade(Object o)

     {

           Student other = (Student)o;

           if (this.grade < other.grade)

                return -1;

           else if (this.grade == other.grade)

           {

                return 0;

           }

           else //this.grade > other.grade

           {

                return 1;

           }

     }

}

 

 

 

TASK – PART 3 – TESTING ALL SORTING CRITERIAS

 

Here is my solution:

 

import java.util.Arrays;

 

public class StudentTester

{

     public static void main(String[] args)

     {

           Student[] stews = new Student[20];

          

           //Create students

           for (int i=0; i<stews.length; i++)

           {

                stews[i] = new Student();

           }

          

           //Output students

           for (int i=0; i<stews.length; i++)

           {

                System.out.println(stews[i]);;

           }         

          

           Student.compareByGrade();

           Arrays.sort(stews);

           System.out.println("=====COMPARE BY GRADE=====");

          

           //Output students

           for (int i=0; i<stews.length; i++)

           {

                System.out.println(stews[i]);;

           }

          

           Student.compareByLastName();

           Arrays.sort(stews);

           System.out.println("=====COMPARE BY LAST NAME=====");

          

           //Output students

           for (int i=0; i<stews.length; i++)

           {

                System.out.println(stews[i]);;

           }

 

           Student.compareBySchool();

           Arrays.sort(stews);

           System.out.println("=====COMPARE BY SCHOOL=====");

          

           //Output students

           for (int i=0; i<stews.length; i++)

           {

                System.out.println(stews[i]);;

           }

          

     }

}