Java programming using methods Design a solution that requests and receives stud
ID: 3821868 • Letter: J
Question
Java programming using methods
Design a solution that requests and receives student names and an examscore for each.
The program should continue to accept names and scores until the user inputs a student whose name is “alldone”.
After the inputs are complete determine which student has the highest score and display that student’s name and score.
Below what output should look like.
C: Learn Java CS1301 src java Lab8 Please enter student's name or "alldone" if finished alldone Nobody had the highest grade with a score of e.e C: Learnjava CS1301 src java Lab8 Please enter student's name or "alldone" if finished jack Please enter student's score 70 Please enter student's name or "alldone" if finished sam Please enter student's score 80 Please enter student's name or "alldone" if finished R2D2 Please enter student's score 100 Please enter student's name or "alldone" if finished alldone R2D2 had the highest grade with a score of 100.0Explanation / Answer
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/**
* Java programming using methods
Design a solution that requests and receives student names and an examscore for each.
The program should continue to accept names and scores until the user inputs a student whose name is “alldone”.
After the inputs are complete determine which student has the highest score and display that student’s name and score.
Below what output should look like.
*
*/
public class Lab8 {
public static int getIndexOfMaxMarks(List<Integer> marksList)
{
int maxIndex = 0;
int maxMarks = marksList.get(maxIndex);
for(int i = 1; i < marksList.size(); i++)
{
if (marksList.get(i) > maxMarks)
{
maxMarks = marksList.get(i);
maxIndex = i;
}
}
return maxIndex;
}
public static void main(String[] args)
{
List<String> nameList = new ArrayList<>();
List<Integer> marksList = new ArrayList<>();
Scanner sc = new Scanner(System.in);
while(true)
{
System.out.print("Please enter the student's name, or "alldone" if finished > ");
String name = sc.next();
if (name.equalsIgnoreCase("alldone"))
{
break;
}
nameList.add(name);
System.out.print("Please enter student's score > ");
int marks = sc.nextInt();
marksList.add(marks);
}
System.out.println();
if (marksList.size() == 0)
{
System.out.println("Nobody had the highest grade, with a score of 0.0");
}
else
{
int maxMarksIndex = getIndexOfMaxMarks(marksList);
System.out.println(nameList.get(maxMarksIndex) + " had the highest grade, with a score of " + marksList.get(maxMarksIndex));
}
System.out.println();
sc.close();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.