Letter Grade Calculations in C#

The following program will take the users input (in this case grades on a 1 - 100 scale) and provide the average grade noted as both a numeric value and letter grade.
 
One of the neat work-arounds shown in this example is the use of the switch statement.  Visual Basic has a similar statement to C#'s switch, however it is a little more flexible.  In each case condition in Visual Basic, you can name a range.  However, the switch statement in C# you can only test one value at a time. 
 
For example if we wanted to assign the letter grade "A" based on the range of 90 to 100, we could our case line Visual Basic would read;
 
Select Case grade
Case Is 90 to 100
lblLetterGrade.Text = "A"
End Select
 
While in C# we would need multiple case lines to accomplish the same thing (see example below).
 
swtich(grade)
{
case: 100
case: 99
case: 98
case: 97
case: 96
case: 95
case: 94
case: 93
case: 92
case: 91
case: 90
lblLetterGrade.Text = "A";
break;
}
Other uses that this program incorporates is the use of ArrayList.  In order to use ArrayList, you must bring in the namespace System.Collections.
 
ArrayList is flexible since you don't have to declare a type with the array.  You can assign it any type of values you need to and convert or cast them into the format that you need at a later time.  In addition, in this program we write our own methods to validate the grade entered and assign a letter grade to the average grade computed.
 
Below is the code of the program.
 

using System;

//must include System.Collections if using ArrayList

using System.Collections;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

 

namespace Letter_Grade

{

    public partial class frmLetterGrade : Form

    {

        //array to hold our grades,

        //a double to accept the grade as type double

        ArrayList grades = new ArrayList();

        double grade = 0;

 

        public frmLetterGrade()

        {

            InitializeComponent();

        }

 

        private void frmLetterGrade_Load(object sender, EventArgs e)

        {

            //I decided to make the buttons inactive to

            //prevent misfiring of methods

            btnEnterGrade.Enabled = false;

            btnCalcAverage.Enabled = false;

        }

 

        private void txtNumericGrade_Leave(object sender, EventArgs e)

        {

            //no code necessary

        }

 

        private void txtNumericGrade_TextChanged(object sender, EventArgs e)

        {

            //if we don't have a blank on the text box, let's

            //test the number for validity

            if (txtNumericGrade.Text != "")

            {

                grade = numberText(txtNumericGrade.Text);

 

                if (grade < 0)

                {

                    MessageBox.Show("You need to enter a positive number");

                }

                    //this if statement won't allow the user to enter

                    //any value under 1

                    if (grade >= 1)

                    {

 

                        btnCalcAverage.Enabled = true;

                        btnEnterGrade.Enabled = true;

                    }

                    else

                    {

                        txtNumericGrade.Text = "";

                    }// end if/else statement

            }

        }// end outer if

 

        //this method was written to cast the entry

        //as a number and catch alphabetic characters

        private double numberText(string entry)

        {

                try

                {

                    return Convert.ToDouble(entry);

                }

                catch

                {

                    MessageBox.Show("You did not enter a valid number.");

                    txtNumericGrade.Text = "0";

                    return 0.09;

                }

        }

 

        private void btnEnterGrade_Click(object sender, EventArgs e)

        {

            grades.Add(grade);

            txtNumericGrade.Text = "";

            txtNumericGrade.Focus();

        }

 

        private void btnCalcAverage_Click(object sender, EventArgs e)

        {

            double totalPoints = 0;

            double averageGrade = 0;

           

            for(int i = 0; i < grades.Count; i++)

            {

                totalPoints += Convert.ToDouble(grades[i]);

            }//end for loop

 

            averageGrade = totalPoints / grades.Count;

 

            lblAverageGrade.Text += averageGrade.ToString();

            lblLetterGrade.Text += " " + LetterGrade(averageGrade).ToString();

        }

 

        private string LetterGrade(double avgGrade)

        {

            string letterGrade;

            if (avgGrade > 100)

            {

                letterGrade = "A+";

            }

            else

            {

                switch ((int)Math.Floor(avgGrade / 10))

                {

                    case 10:

                    case 9:

                        letterGrade = "A";

                        break;

                    case 8:

                        letterGrade = "B";

                        break;

                    case 7:

                        letterGrade = "C";

                        break;

                    case 6:

                        letterGrade = "D";

                        break;

                    default:

                        letterGrade = "F";

                        break;

 

                }//end switch

               

            }//end if else statement

 

            return letterGrade;

        }

    }

}

AttachmentSize
Letter Grade.zip42.97 KB

Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <p> <span> <div> <h1> <h2> <h3> <h4> <h5> <h6> <img> <map> <area> <hr> <br> <br /> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <em> <b> <u> <i> <strong> <font> <del> <ins> <sub> <sup> <quote> <blockquote> <pre> <address> <code> <cite> <embed> <object> <strike> <caption>

More information about formatting options