Please Help Me Improve the Design of My Java Program The program functions to ca
ID: 3560872 • Letter: P
Question
Please Help Me Improve the Design of My Java Program
The program functions to calculate the lowest score among a group of students and report it to screen
It uses a user input approach using JOptionPane class
What Should I Do When There Are Two Students With The Same Lowest Grade, How Can I Display This?
======================================================================================================================================================================================
public static void main(String[] args)
{
String[] tempName = student();
int[] tempScore = scores(tempName);
lowestScore(tempName, tempScore);
}
public static String[] student()
{
String[] student = new String[Integer.parseInt(JOptionPane.showInputDialog("Enter Number of Students"))];
for(int i = 0; i < student.length; i++)
{
student[i] = JOptionPane.showInputDialog("Enter Name of Students");
}
return student;
}
public static int[] scores(String[] student)
{
int[] scores = new int[student.length];
for(int i = 0; i < student.length; i++)
{
scores[i] = Integer.parseInt(JOptionPane.showInputDialog("Enter The Midterm Score of Student"));
}
return scores;
}
public static void lowestScore(String[] student, int[] scores)
{
int minIndex = 0;
int lowest = 0;
String lowestStudent = " ";
for(int i = 1; i < scores.length; i++)
{
if(scores[i] < scores[minIndex])
{
lowest = scores[i];
lowestStudent = student[i];
}
JOptionPane.showMessageDialog(null, " " + "The Lowest Midterm Score Was: " + lowest + " " + "The Student Was: " + lowestStudent + " ");
}
}
======================================================================================================================================================================================
Explanation / Answer
public static void main(String[] args)
{
String[] tempName = student();
int[] tempScore = scores(tempName);
lowestScore(tempName, tempScore);
}
public static String[] student()
{
String[] student = new String[Integer.parseInt(JOptionPane.showInputDialog("Enter Number of Students"))];
for(int i = 0; i < student.length; i++)
{
student[i] = JOptionPane.showInputDialog("Enter Name of Students");
}
return student;
}
public static int[] scores(String[] student)
{
int[] scores = new int[student.length];
for(int i = 0; i < student.length; i++)
{
scores[i] = Integer.parseInt(JOptionPane.showInputDialog("Enter The Midterm Score of Student"));
}
return scores;
}
public static void lowestScore(String[] student, int[] scores)
{
int minIndex = 0;
int lowest = 0;
String lowestStudent = " ";
for(int i = 1; i < scores.length; i++)
{
if(scores[i] < scores[minIndex])
{
lowest = scores[i];
lowestStudent = student[i];
minIndex = i;
}
}
// check if any studnet with same lowest score and print that on to the screen.
for(int i=0; i<scores.length; i++)
if(scores[i] == lowest)
JOptionPane.showMessageDialog(null, " " + "The Lowest Midterm Score Was: " + lowest + " " + "The Student Was: " + student[i] + " ");
}