C Define a student class that supports the following fields
C#
Define a student class that supports the following fields.
id - int
name - string
grade - char
---
Properties:
ID - get
Name - get/set
Grade - get
Methods:
Constructor - Takes and int for id and string for name.
SetGrade(float score) - Sets the grade to a letter value based upon score, use a standard scale.
ToString() - override ToString to return Name + ID + Grade as a comma separated list
Main, create an object, set the grade and print it out using ToString()
Solution
using System;
public class Student
{
private int id;
private string name;
private char grade;
public Student(int ID,string n) //parameterized constructor
{
id = ID;
name = n;
}
public void setGrade(float score) //setGrade() method to find the Grade from score
{
if(score >= 90)
grade =\'A\';
else if(score >=80 && score <=89)
grade = \'B\';
else if(score >=70 && score <=79)
grade = \'C\';
else if(score >=60 && score <=69)
grade = \'D\';
else if(score >=50 && score <=59)
grade = \'E\';
else grade = \'F\';
}
public int ID //properties
{
get
{
return id;
}
set
{
id = value;
}
}
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
public char Grade
{
get
{
return grade;
}
set
{
grade = value;
}
}
public override string ToString() //ToString() method
{
return \"ID = \" + id +\", Name = \" + name + \", Grade = \" + grade;
}
public static void Main()
{
Student s= new Student(1009,\"John Williams\");
s.setGrade(78);
Console.WriteLine(s.ToString());
}
}
output:

