Guest User

Untitled

a guest
Jun 18th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.34 KB | None | 0 0
  1. import java.io.*;
  2. import java.text.*;
  3. import java.util.*;
  4.  
  5. class Student
  6. {
  7. String name;
  8. int id;
  9. float gpa;
  10. } // Student
  11.  
  12. public class z
  13. {
  14. static void printStudents(Student[] student, int nStudents)
  15. {
  16. String b = " ";
  17. int i;
  18. for (i = 0; i < nStudents; i++)
  19. {
  20. System.out.print("Name = " + student[i].name);
  21. System.out.print(b.substring(student[i].name.length()));
  22. System.out.print(" ID = "
  23. + new DecimalFormat("0000000").format(student[i].id));
  24. System.out.println(" gpa = " + student[i].gpa);
  25. } // for
  26. } // printStudents
  27.  
  28. public static void main(String[] argv) throws Exception
  29. {
  30. // open a file for input
  31. BufferedReader fin;
  32. BufferedReader cin;
  33. cin = new BufferedReader(new InputStreamReader(System.in));
  34.  
  35.  
  36.  
  37. // create an empty list of capacity 100
  38. int nStudents = 0;
  39. Student[] student = new Student[100];
  40.  
  41.  
  42.  
  43. // read and save the records
  44.  
  45.  
  46. int aScore; // loop counter
  47. for (i = 0; i < student.length; i++)
  48. {
  49. System.out.print("Please enter score[" + i + "]: ");
  50. student[i] = new Double(cin.readLine()).intValue();
  51. } // for
  52.  
  53.  
  54. while (true)
  55. {
  56. // create a record and read it from file
  57. Student aStudent = new Student();
  58.  
  59.  
  60.  
  61. String name;
  62. System.out.print("What is your name? [quit to exit] ");
  63. name = cin.readLine();
  64. aStudent.name = name;
  65.  
  66.  
  67. int id;
  68. System.out.print("What is your id? ");
  69. id = new Double(cin.readLine()).intValue();
  70. aStudent.id = id;
  71.  
  72. float gpa;
  73. System.out.print("What is your grade point average? ");
  74. gpa = new Double(cin.readLine()).floatValue();
  75. aStudent.gpa = gpa;
  76.  
  77.  
  78. // add record to list, if it's not full
  79. if (nStudents < student.length)
  80. student[nStudents++] = aStudent;
  81. } // while
  82.  
  83.  
  84. // sort the students by name
  85. for (int i = 0; i < nStudents; i++)
  86. {
  87. for (int j = i + 1; j < nStudents; j++)
  88. {
  89. if (student[i].gpa < student[j].gpa) // then swap
  90. {
  91. Student temp = student[i];
  92. student[i] = student[j];
  93. student[j] = temp;
  94. }
  95. }
  96. }
  97.  
  98. printStudents(student, nStudents);
  99. } // main
  100. } // public class
Add Comment
Please, Sign In to add comment