Java Beginners

Moderators: zibadian
Number of threads: 1233
Number of posts: 2675

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Calculating average from a letter grade Posted by JonathanM85 on 7 Oct 2012 at 8:58 PM
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);
}

}
Attachment: AverageWhile.java (3970 Bytes | downloaded 55 times)



 

Recent Jobs