Create a class named Game that contains a string with the name of the Game and an integer that holds the maximum number of players. Include properties with get and set accessors for each field. Also, include a ToString () Game method that overrides the Object class’s ToString () method and returns a string that contains the name of the class (using GetType()), the name of the Game, and the number of players. Create a child class named GameWithTimeLimit that includes an integer time limit in minutes and a property that contains get and set accessors for the field. Write a program that instantiates an object of each class and demonstrates all the methods.

using System;
public class GameDemo
{
    public static void Main()
    {
        Game baseball = new Game();
        GameWithTimeLimit football = new GameWithTimeLimit();
        baseball.Name = "baseball";
        baseball.MaxNumPlayers = 9;
        football.Name = "football";
        football.MaxNumPlayers = 11;
        football.Minutes = 60;
        Console.WriteLine(baseball.ToString());
        Console.WriteLine(football.ToString() + "Time to play = " + football.Minutes + " minutes");

    }

}
public class Game
{
    public string Name { get; set; }
    public int MaxNumPlayers { get; set; }
    public new string ToString()
    {
        return (GetType() + "" + Name + "maximum number of players = " + MaxNumPlayers);
    }
}
public class GameWithTimeLimit : Game
{
    public int Minutes { get; set; }
}

Getting a few errors:
1. 'Game.Name get and set' must declare a body because it is not marked abstract or extern.
2. 'Game.MaxNumPlayers get and set' must declare a body because it is not marked abstract or extern.
3. 'GameWithTimeLimit get and set' must declare a body because it is not marked abstract or extern.

Stress and don't know what to do. Please Help.

Recommended Answers

All 6 Replies

This gives no problems when I try it.
Only some minor spacing problems in your ToString methods.
See line 14 and 25.

I tried running this in a new console application and it still gives the same errors.

This is the code I tried and ran without problems.
Using VS 2010 Pro.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Game baseball = new Game();
            GameWithTimeLimit football = new GameWithTimeLimit();
            baseball.Name = "baseball";
            baseball.MaxNumPlayers = 9;
            football.Name = "football";
            football.MaxNumPlayers = 11;
            football.Minutes = 60;
            Console.WriteLine(baseball.ToString());
            Console.WriteLine(football.ToString() + " Time to play = " + football.Minutes + " minutes");
           
            Console.ReadKey();
        }
    }
   
    public class Game
    {
        public string Name { get; set; }
        public int MaxNumPlayers { get; set; }
        public new string ToString()
        {
            return (GetType() + " " + Name + " maximum number of players = " + MaxNumPlayers);
        }
    }

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

Your sln file is corrupt or you are trying to compile this code targeting a framework before they added auto-properties.

Please refer some good book like complete reference in Java.You will learn easily.In that book each topic is explained with example.

I'd suggest - Head First C# and C# 4.0 in a Nutshell, are very good books.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.