Java Reference
In-Depth Information
default
:
System.out.println("Invalid Course Grade");
}
}
return
sum / getHoursEnrolled();
}
The method
sortCourses
sorts the array
coursesEnrolled
by course number. To
sort the array, we use a selection sort algorithm. Because we will compare the course
numbers, which are the strings and
private
data members of the
class
Course
,we
first retrieve and store the course numbers in the local variables. Moreover, this
method also rearranges the course grades because the course grades are stored in a
separate array. The definition of this method is:
private void
sortCourses()
{
int
minIndex;
Course temp =
new
Course();
//variable to swap data
String course1;
String course2;
char
tempGrade;
for
(
int
i = 0; i < numberOfCourses - 1; i++)
{
minIndex = i;
for
(
int
j = i + 1; j < numberOfCourses; j++)
{
1
0
//get course numbers
course1 =
coursesEnrolled[minIndex].getCourseNumber();
course2 = coursesEnrolled[j].getCourseNumber();
if
(course1.compareTo(course2) > 0)
minIndex = j;
}
//end for
temp.copyCourseInfo(coursesEnrolled[minIndex]);
coursesEnrolled[minIndex].copyCourseInfo(coursesEnrolled[i]);
coursesEnrolled[i].copyCourseInfo(temp);
tempGrade = courseGrades[minIndex];
courseGrades[minIndex] = courseGrades[i];
courseGrades[i] = tempGrade;
}
//end for
}
//end sortCourses
Search WWH ::
Custom Search