C#: Create a delegate called UpdateGpaDelegate. Next, create a class called Stud
ID: 3752149 • Letter: C
Question
C#:
Create a delegate called UpdateGpaDelegate. Next, create a class called Student with id, name, and gpa fields. The class also includes an event that is of the UpdateGpaDelegate type. When a student's gpa is increased, it will be displayed in red font. When a student's gpa is decreased, it will display the font in blue. Demonstrate the class by creating three student objects with data you make up. Display the student data on the console screen. Then, update each student's gpa. Make sure that one student's gpa is increased, one decreased, and one not changed. Finally, display the student data again. You should see the font change to red for the first student, blue for the second, and the normal color for the last student. (Hint: use two events, one to handle gpa increases, the other to handle gpa decreases).
Explanation / Answer
ANSWER :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//Please not I have used only 1 Event Handler
public class Student
{
public delegate void UpdateGpaDelegate(double val);
public event UpdateGpaDelegate OnChangeGPA;
public int id { get; set; }
public String name { get; set; }
double _gpa;
string color;
public double gpa
{
get
{
return this._gpa;
}
set
{
if (OnChangeGPA != null)
OnChangeGPA(value);
_gpa = value;
Console.ResetColor();
}
}
public Student(int id, string name, double gpa)
{
this.id = id;
this.name = name;
this.gpa = gpa;
}
public void DisplayStudent()
{
if (color != null)
{
if (color == "Blue")
Console.ForegroundColor = ConsoleColor.Blue;
else
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"Id- {this.id} Name- {this.name} gpa- {this.gpa} ");
Console.ResetColor();
}
else
Console.WriteLine($"Id- {this.id} Name- {this.name} gpa- {this.gpa} ");
}
public void student_OnChangeGPA(double value)
{
if (value < gpa)
{
color = "Blue";
}
if (value > gpa)
{
color = "Red";
}
if (value == gpa)
{
color = null;
}
}
public void SetGPA(double value)
{
OnChangeGPA(value);
gpa = value;
Console.ResetColor();
}
}
class Program
{
static void Main(string[] args)
{
Student student1 = new Student(1, "Student 1", 7.8);
Student student2 = new Student(2, "Student 2", 7.8);
Student student3 = new Student(3, "Student 3", 7.8);
student1.OnChangeGPA += new Student.UpdateGpaDelegate(student1.student_OnChangeGPA);
student2.OnChangeGPA += new Student.UpdateGpaDelegate(student2.student_OnChangeGPA);
student3.OnChangeGPA += new Student.UpdateGpaDelegate(student3.student_OnChangeGPA);
student1.DisplayStudent();
student2.DisplayStudent();
student3.DisplayStudent();
Console.WriteLine("Now Changing GPA for students");
student1.gpa++;
student2.gpa--;
student3.gpa = 7.8;
student1.DisplayStudent();
student2.DisplayStudent();
student3.DisplayStudent();
Console.ReadLine();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.