Advertisement
Guest User

Untitled

a guest
Nov 11th, 2012
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.90 KB | None | 0 0
  1. package chapt11;
  2.  
  3. import java.io.FileReader;
  4. import java.util.Arrays;
  5. import java.util.Scanner;
  6. import java.lang.Comparable;
  7.  
  8. public class CH11AS8App {
  9.  
  10. /**
  11. * @param args
  12. */
  13. public static void main(String[] args) throws Exception {
  14.  
  15. Student [] studentArray;
  16.  
  17. System.out.println("Welcome to the Student Scores Application.");
  18. System.out.println();
  19.  
  20.  
  21. String lastName;
  22. String firstName;
  23. int examScore;
  24.  
  25.  
  26. Scanner aScanner = new Scanner(new FileReader(
  27. "src//chapt11//ch11AS8data.txt"));
  28.  
  29. int nStudent = 1;
  30.  
  31. studentArray = new Student[nStudent];
  32.  
  33.  
  34. int counter=0;
  35. while (aScanner.hasNext()) {
  36. lastName = aScanner.next();
  37. firstName = aScanner.next();
  38. examScore = aScanner.nextInt();
  39.  
  40. System.out.println("Student " + nStudent++ + " " + firstName
  41. + " " + lastName + " " + +examScore);
  42.  
  43. studentArray[counter] = new Student(lastName, firstName, examScore);
  44. counter++;
  45. }
  46.  
  47. Arrays.sort(studentArray,0,nStudent);
  48. System.out.print(studentArray);
  49. }
  50.  
  51. static class Student implements Comparable<Student> {
  52.  
  53. private String firstName;
  54. private String lastName;
  55. private int examScore;
  56.  
  57. public Student(String firstName, String lastName, int examScore) {
  58. this.firstName = firstName;
  59. this.examScore = examScore;
  60. this.lastName = lastName;
  61. }
  62.  
  63. // Get & Set Methods
  64. public int getExamScore() {
  65. return examScore;
  66. }
  67.  
  68. public String getFirstName() {
  69. return firstName;
  70. }
  71.  
  72. public String getLastName() {
  73. return lastName;
  74. }
  75.  
  76. @Override
  77. public int compareTo(Student s) {
  78. if (s.lastName.equals(lastName)) {
  79. return firstName.compareToIgnoreCase(s.firstName);
  80. }
  81. return lastName.compareToIgnoreCase(s.lastName);
  82. }
  83. }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement