C# Example: Defining a class, creating objects from the class, and interacting with those objects - Biz Tech

C# Example: Defining a class, creating objects from the class, and interacting with those objects

Listen
 using System;

class Program
{
    static void Main(string[] args)
    {
        Student s1 = new Student("John", "Doe", "12345");
        Student s2 = new Student("Jane", "Doe", "67890");

        s1.AddGrade("Math", 85);
        s1.AddGrade("English", 90);

        s2.AddGrade("Math", 95);
        s2.AddGrade("English", 85);

        Console.WriteLine(s1.GetFullName() + ": " + s1.GetAverageGrade());
        Console.WriteLine(s2.GetFullName() + ": " + s2.GetAverageGrade());
    }
}

class Student
{
    private string firstName;
    private string lastName;
    private string studentId;
    private int numGrades;
    private double totalGrade;

    public Student(string firstName, string lastName, string studentId)
    {
        this.firstName = firstName;
        this.lastName = lastName;
        this.studentId = studentId;
        this.numGrades = 0;
        this.totalGrade = 0;
    }

    public void AddGrade(string course, double grade)
    {
        Console.WriteLine(this.firstName + " " + this.lastName + ": added grade " + grade + " for " + course + ".");
        this.numGrades++;
        this.totalGrade += grade;
    }

    public double GetAverageGrade()
    {
        return this.totalGrade / this.numGrades;
    }

    public string GetFullName()
    {
        return this.firstName + " " + this.lastName;
    }
}

This example defines a Student class with properties for the student’s first name, last name, and student ID, as well as variables for the number of grades and the total grade. It also defines methods for adding grades, calculating the average grade, and getting the student’s full name. The example then creates two Student objects, adds grades to each object, calculates and outputs the average grades for each object. The following steps are performed:

  1. Define a Student class with private fields for the student’s first name, last name, and student ID, as well as variables for the number of grades and the total grade.
  2. Define a constructor for the Student class that takes arguments for the first name, last name, and student ID, and initializes the fields and variables.
  3. Define methods for adding grades, calculating the average grade, and getting the student’s full name.
  4. Create two Student objects, s1 and s2, with the names “John Doe” and “Jane Doe” and student IDs “12345” and “67890”, respectively.
  5. Add two grades to s1 using the AddGrade() method, one for “Math” with a grade of 85, and one for “English” with a grade of 90.
  6. Add two grades to s2 using the AddGrade() method, one for “Math” with a grade of 95, and one for “English” with a grade of 85.
  7. Get the full names and average grades for each Student object using the GetFullName() and GetAverageGrade() methods, and output them to the console using Console.WriteLine().

Note that there are many other concepts and operations you can perform with classes and objects, such as inheritance, polymorphism, encapsulation, and static members.