Design a solution that requests and receives student names and an exam score for each. Use one-dimensional arrays to solve this.
When you are done, printout the Code and, also the corresponding output produced. Upload both the txt file and the console output showing that your program run correctly.
Sample Input:
John 69
Alex 89
Billy 72
Sam 59
Serena 96
Alldone
Sample Output:
The student with the highest score is Serena with a score of 96
Sorted list
Serena 96
Alex 89
Billy 72
John 69
Sam 59
Please Write In JAVA
Thank you
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
// Students.java
import java.io.File;
import java.util.Scanner;
public class Students {
// method to find the index of largest score in an array
static int findMax(int scores[], int count) {
int index = -1;
for (int i = 0; i < count; i++) {
// if index is still -1 or if current score is bigger than score at
// index, updating index
if (index == -1 || scores[i] > scores[index]) {
index = i;
}
}
return index;
}
// method to sort the names and scores array
static void sort(String names[], int scores[], int count) {
// sorting using bubble sort algorithm
for (int i = 0; i < count; i++) {
for (int j = 0; j < count - 1; j++) {
if (scores[j] < scores[j + 1]) {
// swapping elements at i and j in both names and scores
// arrays
int tmp1 = scores[j];
scores[j] = scores[j + 1];
scores[j + 1] = tmp1;
String tmp2 = names[j];
names[j] = names[j + 1];
names[j + 1] = tmp2;
}
}
}
}
public static void main(String[] args) {
// creating arrays to store names and scores. assuming there are no more
// than 50 students
String names[] = new String[50];
int scores[] = new int[50];
int numStudents = 0; // current number of students
// initializing scanner to read from console. if you are reading data
// from a file, then replace System.in with the File object, for
// example, if you are reading from data.txt, then replace below line
// with this => Scanner scanner = new Scanner(new File("data.txt"));
// make sure to throw IOException, and import java.io.File;
Scanner scanner = new Scanner(System.in);
String name = "";
int score;
// looping until alldone (case insensitive) is read for name
while (!name.equalsIgnoreCase("alldone")) {
//reading name
name = scanner.next();
if (!name.equalsIgnoreCase("alldone")) {
//reading score
score = scanner.nextInt();
//adding to arrays
names[numStudents] = name;
scores[numStudents] = score;
numStudents++;
}
}
//finding index of highest score
int indexOfHighest = findMax(scores, numStudents);
//displaying name and score of highest scorer
if (indexOfHighest != -1) {
System.out.println("The student with the highest score is "
+ names[indexOfHighest] + " with a score of "
+ scores[indexOfHighest]);
}
//sorting and displaying sorted list
sort(names, scores, numStudents);
System.out.println("Sorted list");
for (int i = 0; i < numStudents; i++) {
System.out.println(names[i] + "\t" + scores[i]);
}
}
}
/*OUTPUT (input is highlighted in bold text)*/
John 69
Alex 89
Billy 72
Sam 59
Serena 96
Alldone
The student with the highest score is Serena with a score of 96
Sorted list
Serena 96
Alex 89
Billy 72
John 69
Sam 59
Get Answers For Free
Most questions answered within 1 hours.