Classes Student + Teacher - C# Programming Exercise

In this exercise in C#, you will need to write a program that includes the Person class you just created. From this class, you will create two additional classes: Student and Teacher, both descendants of Person. The Student class will have a public method named "GoToClasses", which will print "I’m going to class" on the screen. The Teacher class will have a public method named "Explain", which will display "Explanation begins" on the screen. Also, the Teacher class will have a private attribute called "subject" of type string. The Person class must include a method called "SetAge(int n)" to set the age of the person. The student will have a public method called "ShowAge", which will print "My age is: 20 years old" on the screen. You will also need to create a test class called "StudentAndTeacherTest", which will contain the Main method and will: create a person and make them greet, create a student, set their age to 21, make them greet and display their age, and create a teacher aged 30, make them greet and then explain. This exercise will help you deepen your understanding of creating and inheriting classes, using public and private methods, and organizing tests within a main class in C#.

This exercise will help improve your skills in object-oriented programming (OOP) and allow you to better manage relationships between classes and instances in your C# projects.

 Category

OOP Object Oriented Programming

 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

// 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();
    }
}

 Share this C# exercise

 More C# Programming Exercises of OOP Object Oriented Programming

Explore our set of C# programming exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C#. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C#.

  •  Class Photo Album

    Write a C# class called "PhotoAlbum" with a private attribute "numberOfPages". The class should also have a public method named "GetNumberOfPages", whic...

  •  Class Shapes

    This exercise involves creating a project in C# that implements several classes based on a class diagram. The goal is to organize the code by splitting the ...

  •  Class Vehicles

    This exercise involves creating a project in C# and defining the corresponding classes according to a class diagram. Each class must include the attr...

  •  Class Square

    This exercise consists of completing the project called "Shapes" by adding a class named Square. In this class, we will store the starting X and Y coordinat...

  •  Class Orders

    In this exercise, you must create a project and the corresponding classes according to the class diagram. Each class must include the attributes and ...

  •  Class Colored Circle

    In this expanded exercise, you must modify the shapes and square project to also include a new class that stores data about colored circles. You need to...