Java OOP GUIDE / WORK STUDENT CLASS 2 Topics
TASK –
PART 1 – SETUP Copy
and paste the classes Student and Tester from the Student Class Solutions
into your IDE. TASK –
PART 2 – SORTING BASED ON A COMPARE MODE The
current version of compareTo compares Student objects
based on their grade level. Now,
we want to provide a way to let the programmer sort Student arrays based on
grade level, or on last names, or by school. The
solution is fairly simple. We can
create a variable called compareMode that keeps
track of the desired mode of comparing and then simply organize the compareTo method into multiple parts like below. public int compareTo(Object
o) if (compareMode == 1) else if (compareMode == 2) //add
code to return -1 0 or 1 based on the compare criteria 2 (last name) } else { //add
code to return -1, 0 or 1 based on compare criteria 3 (school) } } Note:
Because we only want one compareMode for the entire
program, instead of one for every student, we make compareMode
a static variable instead of an instance variable. That way, all Student objects will share
the one variable. Make
it happen! TASK –
PART 3 – TESTING ALL SORTING CRITERIAS In
the Tester class, sort the array using each criteria. After each sort, output the array to see
that it worked. |
|