|
-
February 6th, 2013, 04:51 AM
#2
Re: GPA Program
First thing I'd do is get rid of the big switch statement - firstly, switch(String) only works in Java 1.7 and secondly, it's only providing a map of grade (A, B+ etc) to a score (4, 3.67 etc). You can accomplish this just as easily with a map and a look up function. e.g. ...
Code:
public class GPA
{
private HashMap<String, Double> gradeMap = new HashMap<String, Double>();
GPA() {
gradeMap.put("A", 4.00);
gradeMap.put("A-", 3.67);
gradeMap.put("B+", 3.33);
gradeMap.put("B", 3.00);
gradeMap.put("B-", 2.67);
gradeMap.put("C+", 2.33);
gradeMap.put("C", 2.00);
gradeMap.put("C-", 1.67);
gradeMap.put("D+", 1.33);
gradeMap.put("D", 1.00);
gradeMap.put("D-", 0.67);
gradeMap.put("F", 0.00);
}
public double gradeToScore(String grade) {
double score = -1;
if (gradeMap.containsKey(grade.toUpperCase())) {
score = gradeMap.get(grade.toUpperCase());
}
return score;
}
public static void main(String args[])
{
GPA gpa = new GPA();
....
Don't worry about my mixing double (return value of gradeToScore) and Double (values held in the hash) - the compiler takes care of it. (Google autoboxing if interested).
The first line of main creates an instance of the GPA class. This invokes the constructor, which populates the map with the grade to score mapping. Then when doing the processing, you can replace the switch statement with
Code:
double grade = gpa.gradeToScore(Letter[i]);
if (grade >= 0) {
Points[i] = grade;
}
else {
JOptionPane.showMessageDialog(null,"One of the"
+ " Inputted Grades is Not Valid");
System.exit(0);
}
You test the returned score is greater than or equal to zero and use it if this is the case. Otherwise (it is -1) meaning that the grade is not found in the map - in other words one of the grades keyed is not valid.
Letters is largely irrelevant because you populate it from subj. You might as well directly access the subj array when ascertaining the scores... i.e.
Code:
for(int i=0;i<Subj.length;i++) {
double grade = gpa.gradeToScore(Subj[i].getText());
if (grade >= 0) {
...
and then use the subj array again to calculate the GPA
Code:
for(int j=0;j<Subj.length;j++) {
TotalCredits+=Points[j]*Credits[j];
GPA=TotalCredits/6;
You use the "magic number" 6 everywhere - to initialize array bounds and to calculate the GPA. It would make more sense to use a constant with a sensible name like SUBJECT_COUNT. This makes maintaining the program easier in the future because if you want to expand it to 8 subjects, you change the constant rather than the number 6 at several places in the code. You could even use it in all the for loops because you'd know that all the arrays they are iterating over are the right size. [The fly in the ointment here are the Message and Credits arrays which you must maintain by hand.]
Finally, when you choose to exit the program you do it by incrementing counter. It has no other function that this, so you might as well get rid of it. Make the main loop while(true) and replace the line where you increment the counter with a break statement.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|