menu

Classes

Requirement Electronic Grade Book to read scores of an individual student and compute statiscts from the scores.

Input

The grades entered are floting points from 0 to 100.

Output

Highest, Lowest and Average Grade should be displayed.

Class

Classes are written to implement features of an software. Members of Class are of two types

  • State
  • Behaviour
  1. Add new item(class) which will provide the basic functions ```

List grades = new List(); //Constructor to initialize the list which will have default values

public void AddGrade(float grade) { grades.Add(grade); //Method to add items to a list }

2. Intialize object for the class and add grades

GradeBook objGrade = new GradeBook();

objGrade.Add(21.9f);


GradeBook class will instantiate new object with `deafult constructors`.  `f` is added so as to provide converstion to single precision
from double precision.   Here `default constructor` is invoked which is when no objects are passed to the instance of the class. 

##### Classes and Variables
Classes are reference type variables.

GradeBook book = new GradeBook(); book.AddGrade(num1);

book = new GradeBook(); book.AddGrade(num2);


The `book` instance which is used to add `num2` will be avaliable in memory since it is being instantiate second time the reference `num1`
will not be avaliable.  CLR scans through the code and finds the object that are not being used and flushes it out of memory so that other objects can use when needed.  Here book hold `pointer` which points to a place in memory where the values of variable are actually stored.

GradeBook book = new GradeBook(); book.AddGrade(num2);

GradeBook book2 = book; book2.AddGrade(num3);


Here both `book` and `book2` instances will be pointing out the same address where the variable is actually stored. And value which both
instance will have is `num2`&`num3`. 
![](https://user-images.githubusercontent.com/8538409/29997219-53179cb8-902b-11e7-969c-839671931e1b.PNG)

##### Access Modifiers

[Encapsulation](https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/) refers to hiding some parts of how software works. The default modifier is private and which should be specifiyed for better understanding. 

##### Static Keyword

`static` feild or method is one which can be accessed without instances of it which means it can be used without object/members of a class.     Members of a class can also be made as `static`.  

public static float minGrade=0; //GradeBook.cs console.WriteLine(GradeBook.minGrade); //Program.cs


Here `console` is a static member that allows us to access `WriteLine` without creating any instance of it which is very similar to `minGrade`.  

##### Statistics

So far we have satisfiyed a part of the requrirement which is to read grades and store it in a variable.  Next thing is to calculate Lowest, highest and average grade for the student.

In order to achieve that we can create new class `GradeStatistics` which has three variables of it own which are low, high and avg which will hold the respective values. 

class GradeStatistics { public GradeStatistics() { HighestGrade = 0; LowestGrade = float.MaxValue; }

    public float AverageGrade;
    public float HighestGrade;
    public float LowestGrade;
} ``` To calculate these values were are going to create new function `ComputeStatistics`.  ``` class GradeBook
{
    public GradeBook()
    {
        grades = new List<float>();
    }

    public GradeStatistics ComputeStatistics()
    {
        GradeStatistics stats =  new GradeStatistics();
        

        float sum = 0;
        foreach(float grade in grades)
        {
            stats.HighestGrade = Math.Max(grade, stats.HighestGrade);
            stats.LowestGrade = Math.Min(grade, stats.LowestGrade);
            sum += grade;
        }
        stats.AverageGrade = sum / grades.Count;
        return stats;
    }

    public void AddGrade(float grade)
    {
        grades.Add(grade);
    }

    private List<float> grades;
} ``` A better way to do this is to use inheritance to inhert the properties of other classes and use them in an intended way.

Features of C#