C# (Basic)

#1 C#: Team Interface

Implement inheritance as described below.

Create a class Team that has the following:

  1. A member variable teamName [string]

  2. A member variable noOfPlayers [integer]

  3. A constructor function:

    1. It takes 2 parameters and assigns them to teamName and noOfPlayers respectively.

  4. A member function AddPlayer(count):

    1. It takes an integer count as a parameter and increases noOfPlayers by count.

  5. A member function RemovePlayer(count):

    1. It takes an integer count as a parameter and tries to decrease noOfPlayers by count.

    2. If decreasing makes noOfPlayers negative, then this function simply returns false.

    3. Else, decrease noOfPlayers by count and return true.

Create a class Subteam that inherits from the above class Team. It has the following:

  1. A constructor function:

    1. It takes 2 parameters, teamName and noOfPlayers, and calls the base class constructor with these parameters.

  2. A member function ChangeTeamName(name):

    1. It takes a string name as a parameter and changes teamName to name.

Note: Declare all the members as public so that they are accessible by the stubbed code.

public class Team
{
    public string teamName;
    public int noOfPlayers;

    public Team() { }

    public Team(string teamname, int noofplayers)
    {
        teamName = teamname;
        noOfPlayers = noofplayers;
    }

    public void AddPlayer(int count)
    {
        noOfPlayers += count;
    }
    public bool RemovePlayer(int count)
    {
        noOfPlayers -= count;
        if (noOfPlayers < 0)
            return false;
        return true;
    }
}

public class Subteam : Team
{
    public Subteam(string team_Name, int no_OfPlayers)
    {
        teamName = team_Name;
        noOfPlayers = no_OfPlayers;
    }
    public void ChangeTeamName(string name)
    {
        teamName = name;
    }
}

#2 C#: Notes Store

In this challenge, the task is to implement a class called NotesStore. The class will manage a collection of notes, with each note having a state and a name. Valid states for notes are 'completed', 'active', and 'others'. All other states are invalid.

The class must have the following methods:

  1. AddNote(state, name): adds a note with the given name (string) and state (string) to the collection. In addition to that:

    • If the passed name is empty, then it throws an Exception with the message 'Name cannot be empty'.

    • If the passed name is non-empty but the given state is not a valid state for a note, then it throws an Exception with the message 'Invalid state {state}'.

  2. GetNotes(state): returns a list of note names with the given state (string) added so far. The names are returned in the order the corresponding notes were added. In addition to that:

    • If the given state is not a valid note state, then it throws an Exception with the message 'Invalid state {state}'.

    • If no note is found in this state, it returns an empty list.

Note: The state names are case-sensitive.

public class NotesStore
{
    public NotesStore() { }
    public void AddNote(String state, String name)
    {
        var list = new List<string[]>();
        if (name != string.Empty)
            list.Add(name, state);
        else
            throw new ArgumentException("Name cannot be empty");
        if (state != "completed" || state != "active" || state != "others")
            throw new ArgumentException("Invalid State ", state);
    }
    public List<String> GetNotes(String state)
    {
        if (state != "completed" || state != "active" || state != "others")
            throw new ArgumentException("Invalid State ", state);

        return List<string>();
    }
}

Last updated

Was this helpful?