C# Cognizant Handson Solutions

GameInheritance C# Handson Solution Cognizant

Reena  has to teach  a lesson  on various sports and games that are played around the world , to her grade 4 students..

To make the class interactive, she  decided to call each student and ask them  the name of a game  they knew ,number of players needed to play the game and about games that are played with  a time limit.

Help her by writing a C# Program that prompts the user for the said details and display them on the console.

Create the classes , along with the specified members  as mentioned below.

1. class  Game  //  parent class  that describes the games properties

Include Auto-implemented properties for the game’s name and maximum number of players. 

Property NameDescription
public string Name  property to store the name of the game.
public int MaxNumPlayers Maximum number of players   included for the game
Method NameDescription
ToString()  Should overrides the Object class’s ToString() method and returns a string that contains the nameof the Game, and the number of players as  given in the sample output. 

2. class  GameWithTimeLimit // child class that should inherit class Game 

Generate an auto-implemented integer property for  Minutes to store the game’s time limit in minutes.

Property NameDescription
Public int TimeLimit  store the game’s time limit in minutes.
Method NameDescription
ToString()  Should overrides the Object class’s ToString() method and returns a string .Should call the base parent class ToString and print the name and number of players . In addition this method should print the time limit for the game.(Refer the sample output.) 

3. class Program 

Create a class named Program  with Main method to  instantiate  objects for the above mentioned  classes and Display the output as given in the sample.

Note: 

Do not  create any new namespace.

Create classes with public access specifier.

The Main method should be defined in public class Program.

GameInheritance.cs

using System;

public class Game
{
    public string Name { get; set; }
    public int MaxNumPlayers { get; set; }

    public override string ToString()
    {
        return ("Maximum number of players for " + Name + " is " + MaxNumPlayers);
    }
}

public class GameWithTimeLimit : Game
{
    public int TimeLimit { get; set; }

    public override string ToString()
    {
        Console.WriteLine(base.ToString());
        return ("Time Limit for " + Name + " is " + TimeLimit + " minutes");
    }
}

public class Program
{
    public static void Main(string[] args)
    {
        Game g = new Game();
        GameWithTimeLimit gt = new GameWithTimeLimit();

        Console.WriteLine("Enter a game");
        g.Name = Console.ReadLine();

        Console.WriteLine("Enter the maximum number of players");
        g.MaxNumPlayers = int.Parse(Console.ReadLine());

        Console.WriteLine("Enter a game that has time limit");
        gt.Name = Console.ReadLine();

        Console.WriteLine("Enter the maximum number of players");
        gt.MaxNumPlayers = int.Parse(Console.ReadLine());

        Console.WriteLine("Enter the time limit in minutes");
        gt.TimeLimit = int.Parse(Console.ReadLine());

        Console.WriteLine(g.ToString());
        Console.WriteLine(gt.ToString());
    }
}

 

Similar Posts