Answer to Question #166907 in C# for Habib

Question #166907

Create a new project, and 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.


1
Expert's answer
2021-02-25T17:15:02-0500
class StudentAndTeacherTest
    {
        static void Main(string[] args) // Example of program usage
        {
            Person person = new Person("Joel");
            person.Greet();


            Student student = new Student("Jesse");
            student.SetAge(20);
            student.Greet();
            student.ShowAge();


            Teacher teacher = new Teacher("Walter");
            teacher.SetAge(30);
            teacher.Greet();
            teacher.Explain();


            Console.ReadKey();
        }
    }


    class Person
    {
        protected string name;
        protected int age;


        public Person(string name)
        {
            this.name = name;
        }


        public virtual void Greet() // Method for greeting
        {
            Console.WriteLine("Person " + name + " says hello.");
        }




        public void SetAge(int n) // Method for setting age
        {
            this.age = n;
        }
    }
    
    class Student : Person
    {
        public Student(string name) : base(name)
        {


        }


        public void ShowAge()
        {
            Console.WriteLine("My age is " + age + " years old.");
        }


        public void GoToClasses()
        {
            Console.WriteLine("I'm going to class.");
        }


        public override void Greet() // Overridden method for greeting in class Student
        {
            Console.WriteLine("Student " + name + " says hello.");
        }
    }


    class Teacher : Person
    {
        public Teacher(string name) : base(name)
        {


        }


        public void Explain()
        {
            Console.WriteLine("Explanation begins.");
        }
        public override void Greet() // Overridden method for greeting in class Teacher
        {
            Console.WriteLine("Teacher " + name + " says hello.");
        }
    }

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS