TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Miao Chen
NA
1
3.1k
Can someone tell me what's wrong with my code?
Sep 24 2010 9:01 PM
Hi All, I am new and just got an interview coding question. My code is below. The feedback was unsatisfied but I cannot see why. The code is fully tested.
Definitely need a second eye on it. Can someone tell me what's wrong with it? Thanks a bunch!
Here is the objective. And class Team is the missing class I wrote.
Implement the missing class without modifying the provided classes.
The correct output should be:
Local Boys = 6 (Home)
Tourists = 0 (Away)
Code is here:
using System;
namespace Invision.Test
{
public class Team
{
public enum FieldAdvantage { Home = 100, Away = 200 };
// declare a delegate to wire up score change event to the class
public delegate void ScoreChangeEventHandler(FieldAdvantage side, int newScore, ref bool overrule);
protected string _name;
protected FieldAdvantage _side;
protected int _score;
// declare the score change "event" which will be called whenever score value is changed
// this "event" will be set in the Referee class
public ScoreChangeEventHandler ScoreChange;
// this method will be called from the setter
// after execution, it sets the bool value passed from the caller
protected void OnScoreChanged(FieldAdvantage side, int newScore, ref bool overrule)
{
if (ScoreChange != null)
{
ScoreChange(side, newScore, ref overrule);
}
}
public virtual int NumPlayers
{
get { return 0; }
}
public FieldAdvantage Side
{
get { return _side; }
set { _side = value; }
}
public int Score
{
get { return _score; }
set
{
_score = value;
bool overrule = false;
OnScoreChanged(_side, _score, ref overrule);
if (overrule)
{
_score -= 100;
if (_score < 0)
_score = 0;
}
}
}
public Team(string name, FieldAdvantage side)
{
_name = name;
_side = side;
}
// override ToString method for displaying the name field
public override string ToString()
{
return _name;
}
}
public class FootballTeam : Team
{
public FootballTeam(string name, FieldAdvantage side)
: base(name, side)
{
}
public override int NumPlayers
{
get
{
return 11;
}
}
public void Touchdown()
{
// calls the setter method of property Score
Score += 6;
}
}
public class Game
{
public FootballTeam Home = new FootballTeam("Local Boys", Team.FieldAdvantage.Home);
public FootballTeam Away = new FootballTeam("Tourists", Team.FieldAdvantage.Away);
public static void Main()
{
Game game = new Game();
Referee referee = new Referee(game);
game.Home.Touchdown();
game.Away.Touchdown();
game.Home.Score += 100;
Console.WriteLine("{0} = {1} ({2})", game.Home, game.Home.Score, game.Home.Side.ToString());
Console.WriteLine("{0} = {1} ({2})", game.Away, game.Away.Score, game.Away.Side.ToString());
}
}
public class Referee
{
public Referee(Game game)
{
game.Home.ScoreChange += new Team.ScoreChangeEventHandler(OnScoreChange);
game.Away.ScoreChange += new Team.ScoreChangeEventHandler(OnScoreChange);
}
private void OnScoreChange(Team.FieldAdvantage side, int newScore, ref bool overrule)
{
if ((side == Team.FieldAdvantage.Away) || (newScore > 100))
overrule = true;
}
}
}
Reply
Answers (
1
)
Admin: Can't post replies using Google Chrome.
Giving Away Books