-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStudentSorter.java
More file actions
76 lines (63 loc) · 1.82 KB
/
StudentSorter.java
File metadata and controls
76 lines (63 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/**
* Student Sorter - reads students in at command line or from redirected file
* sorts the students and outputs the result
* the sort, initially has a couple of bugs in it.
* @author Kohdy Nicholson
* @author Michael Roscoe
*/
import java.util.Scanner;
public class StudentSorter
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
scan.useDelimiter(",");
System.out.println("Enter the total number of students>");
int sCount = scan.nextInt();
scan.nextLine();
Student[] studentList = new Student[sCount];
for (int i = 0; i < sCount; i++)
{
System.out.println("Enter student name, id, major, year, days since registration>");
String name = scan.next();
String id = scan.next();
String major = scan.next();
int year = scan.nextInt();
int daysSinceReg = scan.nextInt();
scan.nextLine();
if (major.equalsIgnoreCase("CS"))
studentList[i] = new CSStudent(name, id, year,daysSinceReg);
else
studentList[i] = new OtherStudent(name, id, year,daysSinceReg);
}
brokenSort(studentList);
System.out.println("\n\n------Sorted Students------");
for (int i = 0; i < sCount; i++)
System.out.println(studentList[i]);
}
public static void brokenSort(Student[] list)
{
int min;
Student temp;
//visit each spot in the array
for (int i = 0; i < list.length-1; i++)
{
min = i;
//compare each spot in the array with each item in the unsorted list
for (int j = i + 1; j < list.length; j++)
{
//is the current that is found the new smallest number
if (list[j].compareTo(list[i]) > 0)
{
if (list[j].compareTo(list[min]) > 0){
min = j;
}
}
}
// Swap the values
temp = list[i];
list[i] = list[min];
list[min] = temp;
}
}
}