In: Computer Science
Write a program that utilizes a Professor class. A professor has a first name, last name, affiliation, easiness grade, and a helpfulness grade. Grades range from 1 (lowest) to 5 (highest). The Professor class has two constructors. The first constructor initiates the object with just the first name, the last name, and the affiliation. The default value for easiness and helpfulness grade is 3. The second constructor initiates the object using the first name, the last name, the affiliation, and two grades.
The Professor class must not allow a first name or last name to be changed after instantiation.
The affiliation should be implemented as a property.
The grades should have both an accessor method and a mutator method. The Professor class should have a display method that prints all member variables in an appropriate format. For example, you can print out the first name, the last name, affiliation, and the two grades from the display method.
The Professor class should have a ToString() method. Make sure to override the ToString() method to present something meaningful.
Lastly, you should use the Main() method to demonstrate how the Professor class works. For example, create an instance of the Professor class with one of its two constructors. And then, invoke the display method on the instance.
Were using Visual Studios C# console.
This is the program requirements we are working on.
Can you write comments that explain the steps? Or how you get
the result for the steps you write.
Hi
Please find the code below
// Professor class
class Professor
{
// property for Professor
// make FirstName and LastName as readonly to restrict the changes
after initialisation
private readonly string FirstName;
private readonly string LastName;
// Affiliation is implemented as property
public string Affiliation { get; set; }
// default value for grades as per the requirement
private int EasinessGrade = 3;
private int HelpfulnessGrade = 3;
// first constructor which takes three parameters
public Professor(string firstName, string lastName, string
affiliation)
{
this.FirstName = firstName;
this.LastName = lastName;
this.Affiliation = affiliation;
}
// second constructor which takes five parameters
public Professor(string firstName, string lastName, string
affiliation, int easinessGrade, int helpfulnessGrade)
{
this.FirstName = firstName;
this.LastName = lastName;
this.Affiliation = affiliation;
// check for the valide easinesgrade as per the given
condition
if (easinessGrade < 1 || easinessGrade > 5)
Console.Write("Grade should be greater than or equal to 1 and less
than or equal to 5");
else
this.EasinessGrade = easinessGrade;
// check for the valide helpfullessgrade as per the given
condition
if (helpfulnessGrade < 1 || helpfulnessGrade > 5)
Console.Write("Grade should be greater than or equal to 1 and less
than or equal to 5");
else
this.HelpfulnessGrade = helpfulnessGrade;
}
//accessor method and a mutator method for Grades
public int GetEasinessGrade()
{
return this.EasinessGrade;
}
//mutator
public void GetEasinessGrade(int easinessGrade)
{
if(easinessGrade <1 || easinessGrade > 5)
{
Console.Write("Grade should be greater than or equal to 1 and less
than or equal to 5");
}
else
{
this.EasinessGrade = easinessGrade;
}
}
//accessor
public int GetHelpfulnessGrade()
{
return this.HelpfulnessGrade;
}
//mutator
public void GetHelpfulnessGrade(int helpfulnessGrade)
{
if (helpfulnessGrade < 1 || helpfulnessGrade > 5)
{
Console.Write("Grade should be greater than or equal to 1 and less
than or equal to 5");
}
else
{
this.HelpfulnessGrade = helpfulnessGrade;
}
}
// display method to show the content of professor
// this method doesn't return any value
public void Display()
{
//use string interpolation
// print all the details for professor
Console.WriteLine($"First Name:- {this.FirstName}");
Console.WriteLine($"Last Name:- {this.LastName}");
Console.WriteLine($"Affiliation:- {this.Affiliation}");
Console.WriteLine($"Helpfulness Grade:-
{this.HelpfulnessGrade}");
Console.WriteLine($"Easiness Grade:- {this.EasinessGrade}");
}
// override ToString method and return a string containing
information about professor
public override string ToString()
{
// again use string interpolation
return $"{this.FirstName} {this.LastName} is {this.Affiliation}
Professor having Easiness Grade {this.EasinessGrade} and
Helpfulness Grade {this.HelpfulnessGrade}";
}
}
// main function to demonstrate the working of professor class
namespace ConsoleApp1
{
class Program
{
// main function
static void Main(string[] args)
{
// create object of professor
Professor professor = new Professor("Mark", "Taylor", "Research",
2, 4);
// use display function to display the professor information
professor.Display();
// use ToString to get the string
var professorString = professor.ToString();
// Print the professorstring
Console.WriteLine(professorString);
}
}
OUTPUT:-
Thanks !
Hope it helps...