You must Sign In to post a response.
  • Category: Visual Studio

    C# classes referencing to get letter grade

    I have two program one name is program.cs and other is grade.cs ( this one has my methods in it) I'm trying to have program.cs reference grade.cs to show the grade letter. I was able to have my code show the grade percent but I'm also trying to have it show the actual letter grade next to the grade percent. Below is my code for the grade percent and the letter grade I'm unsure about referencing the letter grade because it is a double and I want to use percent in my program.cs to tally up the total for quizzes, tests, & homework

    Program.CS

    Console.WriteLine("Exam = " + myExam.GetGradePercent() + "%");


    Grade.CS
    public double GetGradePercent()
    {
    double totalEarnedPoints = 0; //Start with zero
    //Add all elements of the array to total
    for (int i = 0; i < scoresEntered; i++)
    {
    totalEarnedPoints += earnedScores[i];
    }
    //Return grade percentage by dividing earned points by total points
    return totalEarnedPoints / pointTotal;
    }



    public string GetLetterGrade(double percentage)
    {
    //Incoming parameter will be in decimal format. Example 90% will be 0.90
    percentage *= 100; //Multiply incoming percentage by 100 to move decimal point scaling number from 0 to 100

    if (percentage >= 97.0)
    {
    return "A+";
    }
  • #753022
    Hi,
    Please find the codesnippet.
    using System;
    namespace TestConsoleApplication
    {
    /// <summary>
    /// Main Program
    /// </summary>
    class Program
    {
    static void Main(string[] args)
    {
    int[] marks = new int[5]{10,20,100,55,10};
    Grade myExam = new Grade(marks);
    Console.WriteLine("Exam = " + myExam.GetGradePercent()*100 + "%"); //Converting the decimal to Numeric value
    Console.WriteLine("Grade Letter = " + myExam.GetLetterGrade(myExam.GetGradePercent()));
    Console.ReadLine();
    }
    }
    /// <summary>
    /// Custom Class implements the Grade Functionality
    /// </summary>
    public class Grade
    {
    #region Global Variable and Constructor
    int scoresEntered;
    int[] earnedScores;
    public Grade(int[] marks)
    {
    earnedScores = marks;
    }
    #endregion
    /// <summary>
    /// Method to calculate Grade Percent
    /// </summary>
    /// <returns></returns>
    public double GetGradePercent()
    {
    double totalEarnedPoints = 0; //Start with zero
    double pointTotal = 500; //Max Marks

    //Assign variables
    scoresEntered = earnedScores.Length;
    //Add all elements of the array to total
    for (int i = 0; i < scoresEntered; i++)
    {
    totalEarnedPoints += earnedScores[i];
    }
    //Return grade percentage by dividing earned points by total points
    return totalEarnedPoints / pointTotal;
    }
    /// <summary>
    /// Method to Convert Percentage to Letter Grade
    /// </summary>
    /// <param name="percentage"></param>
    /// <returns></returns>
    public string GetLetterGrade(double percentage)
    {
    /****
    * >97 = A+
    * >=85 && <97 = A
    * >=60 && <85 = B
    * >=40 && <60 = C
    * >=0 && <40 = D
    *******************/

    string letterGrade = String.Empty;
    percentage *= 100;

    if (percentage >= 97.0)
    {
    letterGrade = "A+";
    }
    else if (percentage >= 85 && percentage<97)
    {
    letterGrade = "A";
    }
    else if (percentage >= 60 && percentage < 85)
    {
    letterGrade = "B";
    }
    else if (percentage >= 40 && percentage < 60)
    {
    letterGrade = "C";
    }
    else if (percentage >= 0 && percentage < 40)
    {
    letterGrade = "D";
    }
    return letterGrade;
    }
    }
    }

    Regards,Mahe
    Happy Coding


  • Sign In to post your comments