Exercise
Classes Student + Teacher
Objective
Write a C# program that include in it the class Person that you just created.
Create a class "Student" and another class "Teacher", both descendants of "Person".
The class "Student" will have a public method "GoToClasses", which will write on screen "I’m going to class."
The class "Teacher" will have a public method "Explain", which will show on screen "Explanation begins". Also, it will have a private attribute "subject", a string.
The class Person must have a method "SetAge (int n)" which will indicate the value of their age (eg, 20 years old).
The student will have a public method "ShowAge" which will write on the screen "My age is: 20 years old" (or the corresponding number).
You must create another test class called "StudentAndTeacherTest" that will contain "Main" and:
Create a Person and make it say hello
Create a student, set his age to 21, tell him to Greet and display his age
Create a teacher, 30 years old, ask him to say hello and then explain.
Write Your C# Exercise
C# Exercise Example
// Import the System namespace for basic functionality
using System;
// Define the base class Person
public class Person
{
// Declare a public integer field for age
public int age;
// Define a public method to set the age of the person
public void SetAge(int n)
{
// Assign the value of n to the age field
age = n;
}
// Define a public method for greeting
public void Greet()
{
// Print a greeting message to the console
Console.WriteLine("Hello!");
}
}
// Define the derived class Student, inheriting from Person
public class Student : Person
{
// Define a public method that indicates the student is going to classes
public void GoToClasses()
{
// Print a message to indicate going to class
Console.WriteLine("I'm going to class.");
}
// Define a public method to show the student's age
public void ShowAge()
{
// Print the age message with the current age
Console.WriteLine("My age is: " + age + " years old.");
}
}
// Define the derived class Teacher, inheriting from Person
public class Teacher : Person
{
// Declare a private string field for the subject
private string subject;
// Define a public method that indicates the teacher is explaining
public void Explain()
{
// Print a message to indicate that an explanation is beginning
Console.WriteLine("Explanation begins.");
}
// Define a method to set the teacher's subject
public void SetSubject(string subjectName)
{
// Assign the value of subjectName to the subject field
subject = subjectName;
}
}
// Define a test class to execute the program
public class StudentAndTeacherTest
{
// Main method to run the program
public static void Main()
{
// Create a Person object and make it say hello
Person person = new Person();
person.Greet();
// Create a Student object, set its age, greet, and show its age
Student student = new Student();
student.SetAge(21);
student.Greet();
student.ShowAge();
student.GoToClasses();
// Create a Teacher object, set its age, greet, and explain
Teacher teacher = new Teacher();
teacher.SetAge(30);
teacher.Greet();
teacher.Explain();
}
}