Write a Java program that reads the size of an array to store student score info
ID: 3833789 • Letter: W
Question
Write a Java program that reads the size of an array to store student score information, along with their names. You will need to create two arrays of type String and double i.e. name[] and score[] respectively.
First read the number of students that need to be analyzed, which will determine the size of both the arrays. After reading the size, create arrays of that size and then read each student’s name and their score.
For simplicity of this assignment, you will only consider score of one subject and therefore you will only create one double array.
Use JOptionPane messages for the program input and output.
The program must calculate and tabulate the score distribution and the number of students whose score falls within a certain range.
Also the program must calculate the difference (i.e. called range in statistics) between the smallest and highest score among all the students. That is, range = highest score – smallest score
The output must be as shown below:
Score group Number of Students Average Score
0 – 20 4 1 5.15
21 – 40 6 26.29
41 – 60 5 45.15
61 – 80 5 72.17
81 – 100 3 91.56
Average score of all students: 45.17
Score group that has most students: 21 – 40
Score range of all students: 40
After displaying the above output, pass the original score[] as input parameter to a method scoreVariance(). For each value in the score array, this method calculates (score – average score within corresponding score group) and saves the result value in the same array entry.
For eg. say, score[4] is 56 and the corresponding score group is 41 – 60.
Then from the above example output, the average score for 41 – 60 score group is 45.15. So the (score[4] – 45.15) = 10.85 and store this value in score[4]. Perform the above for all values of score[] and then return from the method.
After return from the method, in main() method, display all the values in score[] separated by comma using JOptionPane message box.
The program must have the functionality broken into appropriate methods.
Explanation / Answer
package com.ge.hc.lcs;
import java.util.Arrays;
import javax.swing.JOptionPane;
public class CheggSolution {
public static int noOfStudents;
public static String[] studentNames;
public static double[] studentScores;
public static void main(String[] args) {
noOfStudents = Integer.valueOf(JOptionPane.showInputDialog("Please enter the number of students: "));
studentNames = new String[noOfStudents];
studentScores = new double[noOfStudents];
for(int i = 0; i < noOfStudents; i++) {
studentNames[i] = JOptionPane.showInputDialog("Please enter the student "+(i+1)+"'s name");
studentScores[i] = Double.valueOf(JOptionPane.showInputDialog("Please enter the student "+(i+1)+"'s Score"));
}
displaySummary();
scoreVariance();
}
public static void displaySummary() {
StringBuffer buffer = new StringBuffer();
for(int i = 0; i < noOfStudents; i++) {
if(studentScores[i] >= Ranges.ZERO.getValue() && studentScores[i] <= Ranges.TWENTY.getValue()) {
Scores.FIRST.sum += studentScores[i];
Scores.FIRST.count += 1;
} else if(studentScores[i] > Ranges.TWENTY.getValue() && studentScores[i] <= Ranges.FORTY.getValue()) {
Scores.SECOND.sum += studentScores[i];
Scores.SECOND.count += 1;
} else if(studentScores[i] > Ranges.FORTY.getValue() && studentScores[i] <= Ranges.SIXTY.getValue()) {
Scores.THIRD.sum += studentScores[i];
Scores.THIRD.count += 1;
} else if(studentScores[i] > Ranges.SIXTY.getValue() && studentScores[i] <= Ranges.EIGHTY.getValue()) {
Scores.FOURTH.sum += studentScores[i];
Scores.FOURTH.count += 1;
} else if(studentScores[i] > Ranges.EIGHTY.getValue() && studentScores[i] <= Ranges.HUNDRED.getValue()) {
Scores.FIFTH.sum += studentScores[i];
Scores.FIFTH.count += 1;
}
}
buffer.append("Score Group No of students Average Score ");
buffer.append("0 - 20 "+Scores.FIRST.getCount()+" "+getAverage(Scores.FIRST) +" ");
buffer.append("21 - 40 "+Scores.SECOND.getCount()+" "+getAverage(Scores.SECOND)+" ");
buffer.append("41 - 60 "+Scores.THIRD.getCount()+" "+getAverage(Scores.THIRD) +" ");
buffer.append("61 - 80 "+Scores.FOURTH.getCount()+" "+getAverage(Scores.FOURTH) +" ");
buffer.append("81 - 100 "+Scores.FIFTH.getCount()+" "+getAverage(Scores.FIFTH)+" ");
buffer.append("Score Range : "+(Arrays.stream(studentScores).max().getAsDouble()
- Arrays.stream(studentScores).min().getAsDouble()));
JOptionPane.showMessageDialog(null, buffer.toString());
}
public static double getAverage(Scores score) {
if(score.getCount() == 0) {
return 0;
} else {
return score.getSum() / score.getCount();
}
}
public static void scoreVariance() {
for(int i = 0; i < noOfStudents; i++) {
if(studentScores[i] >= Ranges.ZERO.getValue() && studentScores[i] <= Ranges.TWENTY.getValue()) {
studentScores[i] = studentScores[i] - getAverage(Scores.FIRST);
} else if(studentScores[i] > Ranges.TWENTY.getValue() && studentScores[i] <= Ranges.FORTY.getValue()) {
studentScores[i] = studentScores[i] - getAverage(Scores.SECOND);
} else if(studentScores[i] > Ranges.FORTY.getValue() && studentScores[i] <= Ranges.SIXTY.getValue()) {
studentScores[i] = studentScores[i] - getAverage(Scores.THIRD);
} else if(studentScores[i] > Ranges.SIXTY.getValue() && studentScores[i] <= Ranges.EIGHTY.getValue()) {
studentScores[i] = studentScores[i] - getAverage(Scores.FOURTH);
} else if(studentScores[i] > Ranges.EIGHTY.getValue() && studentScores[i] <= Ranges.HUNDRED.getValue()) {
studentScores[i] = studentScores[i] - getAverage(Scores.FIFTH);
}
}
JOptionPane.showMessageDialog(null, Arrays.toString(studentScores));
}
enum Ranges {
ZERO(0, 0), TWENTY(1, 20), FORTY(2, 40), SIXTY(3, 60), EIGHTY(4, 80), HUNDRED(5, 100);
private int index;
private int value;
Ranges(int index, int value) {
this.index = index;
this.value = value;
}
public int getIndex() {
return index;
}
public int getValue() {
return value;
}
}
enum Scores {
FIRST, SECOND, THIRD, FOURTH, FIFTH;
private int sum;
private int count;
public int getSum() {
return sum;
}
public void setSum(int sum) {
this.sum = sum;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.