Answer to Question #270770 in C# for jade

Question #270770

In an Application console Project, Give instructions that:

- Create a class "Person"

- Create two classes “Student” and “Teacher”, both inherit from the class "Person".

- The class "Student" will have a public method "GoToClasses", which will display on the screen "I’m going to class. ".

- The class "Person" must have a method "SetAge (int n)" which will indicate the value of their age (for example, 20 years old).

- The class "Student" will have a public method "DisplayAge" which will write on the screen "My age is: XX years old".

- You must create another test class called "Test" which will contain "static void Main()" .

- Create a Person object and make it say "Hello"

- Create a Student object, set its age to 20, make it say "Hello", “I'm going to class. And display his age. - Create a Teacher object, 50, ask him to say "Hello" and then begin the course.


1
Expert's answer
2021-11-25T02:32:47-0500
using System;
using System.Collections.Generic;


namespace App
{


    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 DisplayAge()
        {
            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 override void Greet() // Overridden method for greeting in class Teacher
        {
            Console.WriteLine("Teacher " + name + " says Hello.");
        }
    }


    class Program
    {


        static void Main(string[] args)
        {
            Person person = new Person("Peter");
            person.Greet();




            Student student = new Student("Mary");
            student.SetAge(20);
            student.Greet();
            student.DisplayAge();




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






            Console.ReadLine();
        }
    }
}



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
APPROVED BY CLIENTS