Click here to Skip to main content
15,885,842 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I need help creating a method within a simple program.

I basically created a grade report sheet where I input 5 grades it returns the average score. I also have a combobox in which you have to select a major. I used an if statement to retrieve the letter grade and use a switch to determine the grade you need to get into a major.

The basic format I was shown was:
VB
public string DetermineLetterGrade
            (double courseAverage)

and then obviously a return of lettergrade

C#
private void btnCalculateGrade_Click_1(object sender, RoutedEventArgs e)
        {
            string studentName;
            int homework1;
            int homework2;
            int quiz1;
            int quiz2;
            int finalExam;
            string letterGrade = "Incomplete";
            string major = "unknown";
            double homeworkContribution;
            double quizContribution;
            double finalExamContribution;
            double courseAverage;

            const double homeworkWeight = 0.25;
            const double quizWeight = .35;
            const double finalExamWeight = .40;

           

            studentName = (tbStudentName.Text);
            homework1 = Convert.ToInt32(tbHomework1.Text);
            homework2 = Convert.ToInt32(tbHomework2.Text);
            quiz1 = Convert.ToInt32(tbQuiz1.Text);
            quiz2 = Convert.ToInt32(tbQuiz2.Text);
            finalExam = Convert.ToInt32(tbFinal.Text);
            major = Convert.ToString(cbDepartment.SelectionBoxItem);
            homeworkContribution = (homework1 + homework2) / 2 * homeworkWeight;
            quizContribution = (quiz1 + quiz2) / 2 * quizWeight;
            finalExamContribution = (finalExam) * finalExamWeight;
            courseAverage = homeworkContribution + quizContribution + finalExamContribution;


   


            if (courseAverage >= 90)
            {
                letterGrade = "A";
            }
            else if (courseAverage > 80)
            {
                letterGrade = "B";
            }
            else if (courseAverage > 70)
            {
                letterGrade = "C";
            }
            else if (courseAverage > 60)
            {
                letterGrade = "D";
            }
            else
            {
                letterGrade = "F";
            }




    string businessCollegeRequirement = "unknown";
            switch (major)
            {
                case "Accounting":
                case "MIS":
                    businessCollegeRequirement = "need at least an B";
                    break;
                case "Economics":
                case "Finance":
                case "Marketing":
                    businessCollegeRequirement = "Need at least a C";
                    break;
                case "Management":
                    businessCollegeRequirement = "Need at least a D";
                    break;

            }

            lblResult.Content = studentName + "'s" + "Course Average:" + courseAverage;
            lblResult.Content += "\nletterGrade:" + letterGrade;
            lblResult.Content += "\nRequirement: " + businessCollegeRequirement;
            lblResult.Content += "\nMajor: " + major;


        }
    }//end class
}//end namespace

C#
if (courseAverage >= 90)
           {
               letterGrade = "A";
           }
           else if (courseAverage > 80)
           {
               letterGrade = "B";
           }
           else if (courseAverage > 70)
           {
               letterGrade = "C";
           }
           else if (courseAverage > 60)
           {
               letterGrade = "D";
           }
           else
           {
               letterGrade = "F";
           }
Posted
Updated 16-Mar-11 19:26pm
v3
Comments
GauravKP 17-Mar-11 1:02am    
so where is your problem...
Sergey Alexandrovich Kryukov 17-Mar-11 2:32am    
OP commented:

I have no problem using the plain if structure to retrieve my grade but I have no Idea how to convert it to a method. It is probably something simple but I'm clueless.

For example if I just add public String GetGrade(double courseAverage) I get an extraordinary amount of errors.

It tells me I need a } after my variables and constants (in between them and the if structure)
Toli Cuturicu 17-Mar-11 8:33am    
No question. Just statements.

I don't like any of the Answers so far, not at all. Data should be separated from the code; code should not contain any immediate constants (hard-coded). It should be something like that:

C#
//data associated with symbols:
enum Grade { F = -1, D = 60, C = 70, B = 80, A = 90, }

//...

//code, no numeric literals:
static Grade GetGrade(double age) {
    for (Grade grade = Grade.A; grade > Grade.F; grade--)
        if (age > (int)grade)
            return grade;
    return Grade.F;
} //GetGrade


—SA
 
Share this answer
 
Comments
GauravKP 17-Mar-11 3:08am    
nice .. but it is only replacement of the elseif loop...
How can you say that enum Grade is not hardcoded ...
Sergey Alexandrovich Kryukov 17-Mar-11 3:10am    
What do your think? Data separated from code.
You think it should be voted 1?
--SA
GauravKP 17-Mar-11 3:14am    
Enum vs string, refer to comment, how is it related to performance. I like if you explain the concept...can be usefull for further work.
Sergey Alexandrovich Kryukov 17-Mar-11 3:18am    
You did not understand. Performance is your mistake. Should say "static"
--SA
Sergey Alexandrovich Kryukov 17-Mar-11 3:18am    
I did not here you. Did you vote 1?
--SA
Then what is your problem , you can get the grade easily, make your problem clear..

C#
private String GetGrade(int courseAverage)
        {
            string letterGrade = "";
            if (courseAverage >= 90)
                letterGrade = "A";
            else if (courseAverage > 80)
                letterGrade = "B";
            else if (courseAverage > 70)
                letterGrade = "C";
            else if (courseAverage > 60)
                letterGrade = "D";
            else
                letterGrade = "F";
            return letterGrade;
        }
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 17-Mar-11 2:31am    
Well, numeric literals (immediate constants, hard-coded) in the code, long "if" (could be longer), no abstraction. How to support it?

Using string type is very bad. "" should not be used, use string.Empty instead.
The function does not use "this", so should be static (the code will compile, but will not pass standard FxCop validation with error class "performance" -- performance mistake).

Will work but not passing.

Please see my Answer.
--SA
C#
const double homeworkWeight = 0.25;
        const double quizWeight = .35;
        const double finalExamWeight = .40;

        private String CalculateGrade(string studentName, int homework1, int homework2, int quiz1, int quiz2, int finalExam)
        {
            string letterGrade = "Incomplete";

            double homeworkContribution = 0;
            double quizContribution = 0;
            double finalExamContribution = 0;

            homeworkContribution = (homework1 + homework2) / 2 * homeworkWeight;
            quizContribution = (quiz1 + quiz2) / 2 * quizWeight;
            finalExamContribution = (finalExam) * finalExamWeight;
            double courseAverage = homeworkContribution + quizContribution + finalExamContribution;

            if (courseAverage >= 90)
                letterGrade = "A";
            else if (courseAverage > 80)
                letterGrade = "B";
            else if (courseAverage > 70)
                letterGrade = "C";
            else if (courseAverage > 60)
                letterGrade = "D";
            else
                letterGrade = "F";

            return studentName + "'s" + "\nCourse Average : " + courseAverage + "\nletterGrade : " + letterGrade;

        }
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 17-Mar-11 2:32am    
What's the difference? more code...
--SA
GauravKP 17-Mar-11 3:03am    
How can you achieve this in less code lines ?
Sergey Alexandrovich Kryukov 17-Mar-11 3:25am    
:-)
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900