fork download
  1. import java.util.Scanner;
  2.  
  3. /**Write a program that will input 10 students names
  4. * in random order and their corresponding 3 project scores.
  5. * Data will be read into 2 separate arrays: an array of strings
  6. * for the names and a 2D array for the scores.
  7. * Output input data in table form
  8. *For each of the 3 projects, output the name of the student(s) who scored
  9. the highest mark.
  10. **/
  11.  
  12. class NameAndGrades {
  13. public static void main(String[] args) {
  14. //Scanner object to allow user input
  15. Scanner scnr = new Scanner(System.in);
  16.  
  17. //This array will store the names of the students
  18. String[] names = new String[10];
  19.  
  20. //This 2D array will store the 3 project scores
  21. int[][] scores = new int[10][3];
  22.  
  23.  
  24. //Ask user to input the student names
  25. System.out.println("Enter the 10 student names: ");
  26.  
  27. for (int index = 0; index < 10; index++) {
  28. System.out.print("Student " + (index + 1) + ": ");
  29. names[index] = scnr.nextLine();
  30. }
  31.  
  32.  
  33. selectionSort(names);
  34.  
  35.  
  36. //Will print the names alphabetically
  37. //THIS WILL BE DELETED LATER, IT WAS JUST TO CHECK IF IT DID ITS JOB
  38.  
  39. System.out.println("The students' names in alphabetical order: ");
  40.  
  41. for (int i = 0; i < names.length; i++) {
  42. System.out.println(names[i]);
  43. }
  44.  
  45.  
  46. //Ask user to enter the corresponding project scores
  47.  
  48. System.out.println("Enter the 3 project scores for each student: ");
  49. for(int i = 0; i < names.length; ++i) {
  50. System.out.print("Enter three project marks for " + names[i] + " : ");
  51. scores[i][0] = scnr.nextInt();
  52. scores[i][1] = scnr.nextInt();
  53. scores[i][2] = scnr.nextInt();
  54. }
  55.  
  56.  
  57. //PRINT NAMES AND SCORES SIDE BY SIDE
  58. //MAKE TABLE HEADING HERE
  59. System.out.println("\n\n\t\tMarks Table: \n=========================================");
  60. for(int i = 0; i < names.length; ++i) {
  61. System.out.print(names[i] + " marks: " + scores[i][0] + ", " + scores[i][1] + ", " + scores[i][2] + "\n");
  62. }
  63. }
  64.  
  65.  
  66.  
  67. /**Selection sort method made to sort the student names in
  68.   * alphabetical order.
  69.   @param names names of students
  70.   @return the names organized alphabetically
  71.   **/
  72.  
  73. public static String[] selectionSort(String[] names) {
  74. for (int index = 0; index < names.length - 1; ++index) {
  75. int minIndex = index;
  76. for (int j = index + 1; j < names.length; ++j) {
  77. if (names[j].compareTo(names[minIndex]) < 0) {
  78. minIndex = j;
  79. }
  80. }
  81. String temp = names[index];
  82. names[index] = names[minIndex];
  83. names[minIndex] = temp;
  84. }
  85. return (names);
  86. }
  87. }
Success #stdin #stdout 0.14s 321344KB
stdin
aaaa
aqaab
aabg
azer
actr
kkkk
bnbbb
rrrr
eeee
dddd

1 2 3
4 5 6
7 8 9
1 2 3
4 5 6
7 8 9
1 2 3
4 5 6
7 8 9
1 2 3
stdout
Enter the 10 student names: 
Student 1: Student 2: Student 3: Student 4: Student 5: Student 6: Student 7: Student 8: Student 9: Student 10: The students' names in alphabetical order: 
aaaa
aabg
actr
aqaab
azer
bnbbb
dddd
eeee
kkkk
rrrr
Enter the 3 project scores for each student: 
Enter three project marks for aaaa : Enter three project marks for aabg : Enter three project marks for actr : Enter three project marks for aqaab : Enter three project marks for azer : Enter three project marks for bnbbb : Enter three project marks for dddd : Enter three project marks for eeee : Enter three project marks for kkkk : Enter three project marks for rrrr : 

		Marks Table: 
=========================================
aaaa marks: 1, 2, 3
aabg marks: 4, 5, 6
actr marks: 7, 8, 9
aqaab marks: 1, 2, 3
azer marks: 4, 5, 6
bnbbb marks: 7, 8, 9
dddd marks: 1, 2, 3
eeee marks: 4, 5, 6
kkkk marks: 7, 8, 9
rrrr marks: 1, 2, 3