I am trying to make a grade book that takes the inputs of five assignments for X amount of students and comes up with a final grade for each student. The program successfully loops according to the number of students entered, and compiles correctly. However, I need to come up with the averages of each assignment based on the whole class. I'm trying to accomplish this with arrays, but I'm desperately stuck.
int numStudents = Integer.parseInt(JOptionPane.showInputDialog(null,"Enter number of students: "));
String[] examOneGrade = new String[numStudents];
String[] examTwoGrade = new String[numStudents];
String[] examFinalGrade = new String[numStudents];
String[] projectGrade = new String[numStudents];
String[] homeworkGrade = new String[numStudents];
// loops depending on number of students in the class
for (int i = 0; i < numStudents; i++) {
String name = JOptionPane.showInputDialog(null, "Enter Student Name: ");
JOptionPane.showMessageDialog(null, "Enter Grades for " + name,
" ", JOptionPane.PLAIN_MESSAGE);
examOneGrade[i] = JOptionPane.showInputDialog(null, "Enter Exam 1 Grade: ");
examTwoGrade[i] = JOptionPane.showInputDialog(null, "Enter Exam 2 Grade: ");
examFinalGrade[i] = JOptionPane.showInputDialog(null, "Enter Final Exam Grade: ");
projectGrade[i] = JOptionPane.showInputDialog(null, "Enter Project Grade: ");
homeworkGrade[i] = JOptionPane.showInputDialog(null, "Enter Homework Grade: ");
// converts strings to floats
float exam1 = Float.parseFloat(examOneGrade[i]);
float exam2 = Float.parseFloat(examTwoGrade[i]);
float finalExam = Float.parseFloat(examFinalGrade[i]);
float project = Float.parseFloat(projectGrade[i]);
float homework = Float.parseFloat(homeworkGrade[i]);
// weights
float number1 = exam1 * .10f;
float number2 = exam2 * .10f;
float number3 = finalExam * .30f;
float number4 = project * .30f;
float number5 = homework * .20f;
// calculates student final grade
float grade = number1 + number2 + number3 + number4 + number5;
JOptionPane.showMessageDialog(null, "Final Grade: " + grade,
" " + name, JOptionPane.PLAIN_MESSAGE);
}
I'm not asking to be given the answer, I'm just desperate to know if I'm on the right path and where I should be looking. This exact topic seems to be nonexistent.
My attempt at a low grade method:
public int getMinimum(List<Student> studentList) {
float lowGrade = getExamOneGrade[0];
for(Student student : studentList) {
if(student.getExamOneGrade() < lowGrade) {
lowGrade = getExamOneGrade;
}
}
return lowGrade;
}
Well, basically there's nothing wrong about your approach. But you should go one step further and put all the information and computations concerning a single student in an object. E.g.
Please note, that this is just a brief example. You have to complete it on your own.
public class Student {
private String name;
private float examOneGrade;
private float examTwoGrade;
private float examFinalGrade;
private float projectGrade;
private float homeworkGrade;
// getters and setters
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
public float getExamOneGrade(){
return examOneGrade;
}
public void setExamOneGrade(float examOneGrade) {
this.examOneGrade = examOneGrade;
}
// and so on ...
// weight computation goes here
public float getExamOneWeight() {
return examOneGrade * .10f;
}
public float getExamTwoWeight() {
return examOneGrade * .10f;
}
// ...
public float getFinalGrade {
return getExamOneWeight() +
getExamTwoWeight() +
// ...
getHomeworkWeight();
}
}
Then in your code
int numStudents = Integer.parseInt(JOptionPane.showInputDialog(null,
"Enter number of students: "));
List<Student> studentList = new ArrayList<Student>();
// loops depending on number of students in the class
for (int i = 0; i < numStudents; i++) {
Student student = new Student();
student.setName(JOptionPane.showInputDialog(null, "Enter Student Name: "));
JOptionPane.showMessageDialog(null, "Enter Grades for " + student.getName(),
" ", JOptionPane.PLAIN_MESSAGE);
student.setExamOneGrade(Float.parseFloat(JOptionPane.showInputDialog(null, "Enter Exam 1 Grade: ")));
student.setExamTwoGrade(Float.parseFloat(JOptionPane.showInputDialog(null, "Enter Exam 2 Grade: ")));
// and so on ..
// now add the student to the ArrayList
studentList.add(student);
JOptionPane.showMessageDialog(null, "Final Grade: " + student.getFinalGrade(),
" " + student.getName(), JOptionPane.PLAIN_MESSAGE);
}
EDIT II:
// show the averages here
JOptionPane.showMessageDialog(null, "Avg Grade Of Exam One: " + getAverageGradeOfExamOne(studentList), JOptionPane.PLAIN_MESSAGE);
JOptionPane.showMessageDialog(null, "Avg Grade Of Exam Two: " + getAverageGradeOfExamTwo(studentList), JOptionPane.PLAIN_MESSAGE);
// ...
Using this list of students you can now compute whatever you want and it is all well structured and easy to use.
e.g.
private float getAverageGradeOfExamOne(List<Student> studentList) {
float sum;
for(Student student : studentList){
sum += student.getExamOneGrade();
}
return sum/studentList.size();
}
EDIT:
Using this method above in your "original class" in the main-method we are talking about you could print the result this way:
JOptionPane.showMessageDialog(null, "Avg Grade Of Exam One: " + getAverageGradeOfExamOne(studentList), JOptionPane.PLAIN_MESSAGE);
If this does not work I suppose your "main-method" is a static one. Then change getAverageGradeOfExamOne() to static too, like this
private static float getAverageGradeOfExamOne(List<Student> studentList) {
float sum;
for(Student student : studentList){
sum += student.getExamOneGrade();
}
return sum/studentList.size();
}
One of these method declarations should tie up loose ends for you.