Doogie
Doogie

Reputation: 41

How to change some of my code to a Class?

I wrote some code and it works, but now trying to take some of my code and change it to a Class. I already change part of the code to first a method and then turn it into a Class. But this last part of code having trouble figuring out how to change to a method so then I can make a Class.

else if (input == "3")
{
    Console.WriteLine("Here are your Students: ");
    Array.Sort(names);
    Console.WriteLine(String.Join(", ", names));                    
    Console.WriteLine("");

    double average = gradesList.Average();

    if (average >= 90)
    {
        Console.WriteLine($"There average is a {average.ToString("n2")} which is an A.");

    }
    else if (average >= 80)
    {
        Console.WriteLine($"There average is a {average.ToString("n2")} which is an B.");
    }
    else if (average >= 70)
    {
        Console.WriteLine($"There average is a {average.ToString("n2")} which is an C.");
    }
    else if (average >= 60)
    {
        Console.WriteLine($"There average is a {average.ToString("n2")} which is an D.");
    }
    else
    {
        Console.WriteLine($"There average is a {average.ToString("n2")} which is an E.");
    }

}

The first part I was thinking of leaving that and just change the Grade part into a Class. I tried public static string GetLetterGrade(int average) return ($"There average is a {average.ToString("n2")} which is an A.") but get all kinds or errors that I don't understand.

Upvotes: 3

Views: 104

Answers (2)

Olivier Jacot-Descombes
Olivier Jacot-Descombes

Reputation: 112352

I would make the grade a non-mutable struct. It represents a value and should be a value type. A class is a reference type.

public readonly struct Grade
{
    public Grade(double percentage)
    {
        Percentage = percentage;
    }

    public double Percentage { get; }

    public char Letter
    {
        get {
            if (Percentage >= 90) return 'A';
            if (Percentage >= 80) return 'B';
            if (Percentage >= 70) return 'C';
            if (Percentage >= 60) return 'D';
            return 'E';
        }
    }

    public override string ToString() => $"{Percentage:n2} ({Letter})";
}

Since the if-statements return, it is not necessary to have an else-part.

Then you can easily print the message with

double average = gradesList.Average();
var grade = new Grade(average);
Console.WriteLine(
    $"There average is a {grade.Percentage:n2} which is an {grade.Letter}.");

Note that because ToString is overridden, you can print the grade directly.

Console.WriteLine($"The average grade is {grade}");

It will print something like The average grade is 74.25 (C).

Upvotes: 1

Greg
Greg

Reputation: 346

this should work

public static char GetLetterGrade(double average)
{
    if (average >= 90)
    {
        return 'A';
    }
    else if (average >= 80)
    {
        return 'B';
    }
    else if (average >= 70)
    {
        return 'C';
    }
    else if (average >= 60)
    {
        return 'D';
    }
    return 'E';
}

but if you wish to use GetLetterGrade(int average) overload, use it like this

double average = gradesList.Average();
var grade = GetLetterGrade((int)average);

Upvotes: 0

Related Questions