Grade book using an array to store test grades. - CSharp Custom Type

CSharp examples for Custom Type:class

Description

Grade book using an array to store test grades.

Demo Code

using System;/*w  w  w. j a v  a 2s  .  c om*/
class MyCourse
{
   private int[] grades; // array of student grades
   public string CourseName { get; }
   public MyCourse(string name, int[] gradesArray)
   {
      CourseName = name; // set CourseName to name
      grades = gradesArray; // initialize grades array
   }
   public void DisplayMessage()
   {
      Console.WriteLine($"Welcome to the grade book for\n{CourseName}!\n");
   }
   public void ProcessGrades()
   {
      OutputGrades();
      Console.WriteLine($"\nClass average is {GetAverage():F}");
      Console.WriteLine($"Lowest grade is {GetMinimum()}");
      Console.WriteLine($"Highest grade is {GetMaximum()}\n");
      OutputBarChart();
   }
   public int GetMinimum()
   {
      var lowGrade = grades[0]; // assume grades[0] is smallest
      foreach (var grade in grades)
      {
         if (grade < lowGrade)
         {
            lowGrade = grade; // new lowest grade
         }
      }
      return lowGrade; // return lowest grade
   }
   public int GetMaximum()
   {
      var highGrade = grades[0]; // assume grades[0] is largest
      foreach (var grade in grades)
      {
         if (grade > highGrade)
         {
            highGrade = grade; // new highest grade
         }
      }
      return highGrade; // return highest grade
   }
   public double GetAverage()
   {
      var total = 0.0; // initialize total as a double
      foreach (var grade in grades)
      {
         total += grade;
      }
      return total / grades.Length;
   }
   public void OutputBarChart()
   {
      var frequency = new int[11];
      foreach (var grade in grades)
      {
         ++frequency[grade / 10];
      }
      for (var count = 0; count < frequency.Length; ++count)
      {
         if (count == 10)
         {
            Console.Write("  100: ");
         }
         else
         {
            Console.Write($"{count * 10:D2}-{count * 10 + 9:D2}: ");
         }
         for (var stars = 0; stars < frequency[count]; ++stars)
         {
            Console.Write("*");
         }
         Console.WriteLine(); // start a new line of output
      }
   }
   public void OutputGrades()
   {
      for (var student = 0; student < grades.Length; ++student)
      {
         Console.WriteLine($"Student {student + 1, 2}: {grades[student],3}");
      }
   }
}
class MainClass
{
   static void Main()
   {
      int[] gradesArray = { 89, 87, 68, 94, 76, 83,};
      var myMyCourse = new MyCourse("Programming", gradesArray);
      myMyCourse.DisplayMessage();
      myMyCourse.ProcessGrades();
   }
}

Result


Related Tutorials