Calculating average from a letter grade - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Calculating average from a letter grade

Hello,
Recently I completed a home work assignemt that allows the user to enter 10 letter grades. The program computes and displays the average.
I am not satisfied with the code that I completed and was hoping to get some help with making improvements to the program.

Ideally this is how I would have liked the program to flow.
The user enters a series of 10 letter grades seperated by commas in the JOptionPane. String of letter grades transfered into an array of 10. Array of 10 letter grades then converted to its corresponding integer value and placed into a new array of 10 integers. Then determine the average of the integer array and displaying the class average via the JOptionPane.

Would someone on here be able to provide some advice on the attached/below code as to how I can improve it and include the user interacterbiliy I mentioned above.

Any help is greatly appreciated.
Many thanks,
Jonathan.


/*This program requires the user to enter 10 letter grades A,B,C and D
* the program then calculates the average of the grades and displays the results.
*/

import javax.swing.JOptionPane;

public class AverageWhile {

public static void main(String args[]) {
int counter, total;
float average;
float[] ArrayOfGrades = new float[10];
// initialization phase
total = 0;
counter = 0;

// processing phase
for (counter = 1; counter <= 10; counter++) {

String grade = JOptionPane
.showInputDialog("Enter Letter Grade for " + counter);
if (counter == 1) {

if (grade.equals("A"))
total = 4;
else if (grade.equals("B"))
total = 3;
else if (grade.equals("C"))
total = 2;
else if (grade.equals("D"))
total = 1;

ArrayOfGrades[0] = total;
// ASystem.out.println(ArrayOfGrades[0]);
}

if (counter == 2) {

if (grade.equals("A"))
total = 4;
else if (grade.equals("B"))
total = 3;
else if (grade.equals("C"))
total = 2;
else if (grade.equals("D"))
total = 1;

ArrayOfGrades[1] = total;
// System.out.println(ArrayOfGrades[1]);
}

if (counter == 3) {

if (grade.equals("A"))
total = 4;
else if (grade.equals("B"))
total = 3;
else if (grade.equals("C"))
total = 2;
else if (grade.equals("D"))
total = 1;

ArrayOfGrades[2] = total;
// System.out.println(ArrayOfGrades[2]);
}

if (counter == 4) {

if (grade.equals("A"))
total = 4;
else if (grade.equals("B"))
total = 3;
else if (grade.equals("C"))
total = 2;
else if (grade.equals("D"))
total = 1;

ArrayOfGrades[3] = total;
// System.out.println(ArrayOfGrades[3]);
}

if (counter == 5) {

if (grade.equals("A"))
total = 4;
else if (grade.equals("B"))
total = 3;
else if (grade.equals("C"))
total = 2;
else if (grade.equals("D"))
total = 1;

ArrayOfGrades[4] = total;
// System.out.println(ArrayOfGrades[4]);
}

if (counter == 6) {

if (grade.equals("A"))
total = 4;
else if (grade.equals("B"))
total = 3;
else if (grade.equals("C"))
total = 2;
else if (grade.equals("D"))
total = 1;

ArrayOfGrades[5] = total;
// System.out.println(ArrayOfGrades[5]);
}

if (counter == 7) {

if (grade.equals("A"))
total = 4;
else if (grade.equals("B"))
total = 3;
else if (grade.equals("C"))
total = 2;
else if (grade.equals("D"))
total = 1;

ArrayOfGrades[6] = total;
// System.out.println(ArrayOfGrades[6]);
}

if (counter == 8) {

if (grade.equals("A"))
total = 4;
else if (grade.equals("B"))
total = 3;
else if (grade.equals("C"))
total = 2;
else if (grade.equals("D"))
total = 1;

ArrayOfGrades[7] = total;
// System.out.println(ArrayOfGrades[7]);
}

if (counter == 9) {

if (grade.equals("A"))
total = 4;
else if (grade.equals("B"))
total = 3;
else if (grade.equals("C"))
total = 2;
else if (grade.equals("D"))
total = 1;

ArrayOfGrades[8] = total;
// System.out.println(ArrayOfGrades[8]);
}

if (counter == 10) {

if (grade.equals("A"))
total = 4;
else if (grade.equals("B"))
total = 3;
else if (grade.equals("C"))
total = 2;
else if (grade.equals("D"))
total = 1;

ArrayOfGrades[9] = total;
// System.out.println(ArrayOfGrades[9]);
}

}
average = ((ArrayOfGrades[0] + ArrayOfGrades[1] + ArrayOfGrades[2]
+ ArrayOfGrades[3] + ArrayOfGrades[4] + ArrayOfGrades[5]
+ ArrayOfGrades[6] + ArrayOfGrades[7] + ArrayOfGrades[8] + ArrayOfGrades[9]) /10);
JOptionPane.showMessageDialog(null, "The Class Average " + average);
}

}
Sign In or Register to comment.