//Contain all computations within the public function. public GradeStatistics ComputeStatistics() { GradeStatistics stats = new GradeStatistics(); float sum = 0; foreach (float grade in grades) //specify the variable to hold the value from the list "grades" as the list is looped through. { stats.HighestGrade = Math.Max(grade, stats.HighestGrade); //Math.Max will return the highest value given two inputs. stats.LowestGrade = Math.Min(grade, stats.LowestGrade); //Math.Min will return the lowest value given two inputs. sum += grade; //+= will add the value on the right to the value on the left and store in the variable on the left. } stats.AverageGrade = sum / grades.Count; return(stats); }
static void Main(string[] args) { GradeBook book = new GradeBook(); //Variable that can be used to access the class GradeBook (variable of type GradeBook. book.AddGrade(91); book.AddGrade(89.5f); //f tells the compiler that the value is a float and not double. //This would create a new object, gradebook, in memory and assign book2 the memory location. GradeBook book2 = new GradeBook(); book2.AddGrade(75); GradeBook book3 = book; //This creates the book3 variable which will have the memory location of book assigned to it. //Now any additions to book3 also impact book because they are referencing the same object at the specified memory location (Very Important). book3.AddGrade(15); GradeStatistics stats = book.ComputeStatistics(); //Runs ComputerStatistics and places the values at the memory locations for the fields in the object stats. Console.WriteLine("Avg Grade: " + stats.AverageGrade); //Displays value of 65.16666 Console.WriteLine("Max Grade: " + stats.HighestGrade); //Displays value of 91 Console.WriteLine("Min Grade: " + stats.LowestGrade); //Displays value of 15 }