I'm working on a Grade Point Average Calculator.

The user just have to Select the number of courses offered, then select the grade obtain in the different courses by choosing the appropriate grade from the Choice Box.

The problem with my application is that, if the user mistakenly selects "A" instead of "B", then he corrects his mistake by selecting "B", the program sums up all the selections, including the wrong selection. So the calculation ends up being wrong.

Another proble with my application is that, u can't select an item twice. i.e if a user, selects "A" in the first Choice box, he can't select "A" again in the same Choice box, in case he wants to do another calculation.

This is the source code for the gradeChoice class.....

import java.awt.*;

import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;

import javax.swing.*;
/*
 *
 *@author Anokam Kingsley
 */

    GradeChoice(){
        super();

            final String[] Grades = {"Grades","A","A-","B+","B","B-","C+","C","C-","D","F"};

            for(String grades : Grades){
                add(grades);
                }

            addItemListener(new ItemListener(){

                             public void itemStateChanged(ItemEvent e){

                                    String gradeString = getSelectedItem();
                                     switch(gradeString){
                                     case "A" :
                                     gradeMark = 4.00;
                                     break;
                                     case "A-":
                                     gradeMark = 3.67;
                                     break;
                                     case "B+":
                                     gradeMark = 3.34;
                                     break;
                                     case "B":
                                     gradeMark = 3.00;
                                     break;
                                     case "B-":
                                     gradeMark = 2.67;
                                     break;
                                     case "C+":
                                      gradeMark = 2.34;
                                     break;
                                     case "C":
                                      gradeMark = 2.00;
                                     break;
                                     case "C-": 
                                      gradeMark = 1.67;
                                     break;
                                     case "D":
                                      gradeMark = 1.00;
                                     break;
                                     case "F":
                                      gradeMark = 0.00;
                                     break;
                                     default:
                                     System.out.println("Error!!");                         
                                     } 

                                    Scores += gradeMark;

                                }}); //End of addItemListener

                         }//End of GradeChoice constructor

                                 static double gradeMark,Scores;;

    }// End of class.

i'd be very grateful if someone helps me

It seems to me that the problem isn't in the code, it's in the design of the GUI. In general you can't neatly make repeated selections or delete errors just by making selections in a list of choices. At the very least you could have "Add" and "Delete" buttons which would allow the user to select a grade then add that or delete it from the total. A field showing the current list of grades as they are enterd would be good feedback for the user, (but not essential).

But if you really want to stick with one list of choices then (a) clear the selection after each user selection is processed - this will allow the user to select the same grade twice, and (b) have a choice "oops! - please delete the last selection"

Edited 3 Years Ago by JamesCherrill

This article has been dead for over six months. Start a new discussion instead.