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 twoStudent
objects, adds grades to each object, calculates and outputs the average grades for each object. The following steps are performed:
- 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.- 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.- Define methods for adding grades, calculating the average grade, and getting the student’s full name.
- Create two
Student
objects,s1
ands2
, with the names “John Doe” and “Jane Doe” and student IDs “12345” and “67890”, respectively.- Add two grades to
s1
using theAddGrade()
method, one for “Math” with a grade of 85, and one for “English” with a grade of 90.- Add two grades to
s2
using theAddGrade()
method, one for “Math” with a grade of 95, and one for “English” with a grade of 85.- Get the full names and average grades for each
Student
object using theGetFullName()
andGetAverageGrade()
methods, and output them to the console usingConsole.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.