Skip to content

Instantly share code, notes, and snippets.

@WilliamGarrow
Last active October 27, 2015 14:46
Show Gist options
  • Save WilliamGarrow/b50fb6ecad5c9c00cd3b to your computer and use it in GitHub Desktop.
Save WilliamGarrow/b50fb6ecad5c9c00cd3b to your computer and use it in GitHub Desktop.
C# - Course grade book that computes statistics with high, low and average grades
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Grades
{
class Program
{
static void Main(string[] args)
{
GradeBook book = new GradeBook();
book.NameChanged += OnNameChanged;
book.Name = "Course Grade Book";
book.Name = "Student Grade Book";
book.AddGrade(91);
book.AddGrade(89.5f);
book.AddGrade(75);
GradeStatistics stats = book.ComputeStatistics();
Console.WriteLine(book.Name);
WriteResult("Average", stats.AverageGrade);
WriteResult("Highest", (int)stats.HighestGrade);
WriteResult("Lowest", stats.LowestGrade);
}
static void OnNameChanged(object sender, NameChangedEventArgs args)
{
Console.WriteLine($"Grade book changing name from {args.ExistingName} to {args.NewName}");
}
//method overloading in c#
static void WriteResult(string description, int result)
{
Console.WriteLine(description + ": " + result);
}
static void WriteResult(string description, float result)
{
Console.WriteLine($"{description}: {result:F2}", description, result);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment