CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: GPA Program

Threaded View

  1. #1
    Join Date
    Feb 2013
    Posts
    4

    GPA Program

    i wrote the following code for a dialog box to enter grades and calculate gpa. is there any way to make it
    shorter? like can i use object arrays and initialize it in a for loop? thank you
    Code:
    import javax.swing.*;
    public class GPA
    {
        public static void main(String args[])
        {
            int Counter=0;
            while(Counter!=1)
            {
                String Name=JOptionPane.showInputDialog("Enter Student Name");
                JTextField[] Subj=new JTextField[6];
                
                for(int i=0;i<Subj.length;i++)
                {
                    Subj[i]=new JTextField();
                }
                        
                double[] Credits={1.0, 1.0, 1.0, 1.0, 1.25, 0.75}; 
                double[] Points=new double[6]; //Letter Grade into Points
                double TotalCredits=0.0; 
                double GPA=0.0;
                Object[] Message={
                "Enter English Grade: ",Subj[0],
                "Enter Math Grade",Subj[1],
                "Enter Social Studies Grade",Subj[2],
                "Enter Art Grade",Subj[3],
                "Enter Science Grade",Subj[4],
                "Enter P.E Grade",Subj[5]
                };
                
                int Value = JOptionPane.showConfirmDialog(null, Message, 
                        "Enter Letter Grade", JOptionPane.OK_CANCEL_OPTION); 
                
                String[] Letter=new String[6];
                for(int i=0;i<Subj.length;i++)
                {
                    Letter[i]=Subj[i].getText();
                }
                
                for(int i=0;i<Letter.length;i++)
                {
                    switch(Letter[i])
                    {  
                        case "A":
                             Points[i]=4.00;
                            break;
                        case "A-":
                            Points[i]=3.67;
                            break;
                        case "B+":
                            Points[i]=3.33;
                            break;
                        case "B":
                            Points[i]=3.00;
                             break;
                        case "B-":
                         Points[i]=2.67;
                            break;
                        case "C+":
                            Points[i]=2.33;
                            break;
                        case "C":
                            Points[i]=2.00;
                            break;
                        case "C-":
                            Points[i]=1.67;
                            break;
                        case "D+":
                            Points[i]=1.33;
                            break;
                        case "D":
                            Points[i]=1.00;
                            break;
                        case "D-":
                            Points[i]=0.67;
                            break;
                        case "F":
                            Points[i]=0.00;
                            break;
                        default:
                            JOptionPane.showMessageDialog(null,"One of the"
                                    + " Inputted Grades is Not Valid");
                            System.exit(0);
                            break;
                    }
                }
             
                //calculate and display GPA
                for(int j=0;j<Letter.length;j++)
                {
                    TotalCredits+=Points[j]*Credits[j];
                    GPA=TotalCredits/6;
                }
                JOptionPane.showMessageDialog(null,String.format("Student Name: %s"
                        + "\nGPA: %.2f",Name,GPA));
                
                int value=JOptionPane.showConfirmDialog(null,"Would You Like To "
                        + "Calculate Another GPA?","",JOptionPane.YES_NO_OPTION);
                if(value==JOptionPane.NO_OPTION)
                {
                    JOptionPane.showMessageDialog(null,"Thank You For Using This "
                            + "GPA Calculator");
                    Counter++;
                }
            }
        }
    }
    Last edited by DataMiser; February 5th, 2013 at 09:37 PM. Reason: added code tags

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured