
Never send a human to do a machine's job
Hello gentlemen.
As a newbie to C # and .NET, I asked yesterday at stackoverflow ( http://stackoverflow.com/questions/2192808/simple-but-interesting-task-for-c-newbie ) for newbie test suggestions. As a result, I received a number of answers, interesting and not very. But among them, one stood out favorably, with a proposal to solve the programmer quiz.
The goal of the puzzle is to use the available classes (code 1) to display the phrase that I put in the topic of this publication. For those who want to read the post in the original, I gave a link to the question itself above, the search answer there is marked as accepted. For everyone else, the task itself is:
The author of this answer proposes to implement the code itself inside
PS: I haven’t decided yet (but there are certain thoughts, of course), therefore, more experienced comrades, do not leave spoilers in the comments :-)
Thank you all and good luck with the solution :-)
UPD 1: fixed according to the first comment, however, I am not entirely sure that the conditions of the task prohibit the creation of new classes + inheritance. and I’m not sure that the problem can be solved without this. naturally I could be wrong.
As a newbie to C # and .NET, I asked yesterday at stackoverflow ( http://stackoverflow.com/questions/2192808/simple-but-interesting-task-for-c-newbie ) for newbie test suggestions. As a result, I received a number of answers, interesting and not very. But among them, one stood out favorably, with a proposal to solve the programmer quiz.
The goal of the puzzle is to use the available classes (code 1) to display the phrase that I put in the topic of this publication. For those who want to read the post in the original, I gave a link to the question itself above, the search answer there is marked as accepted. For everyone else, the task itself is:
- using System;
-
- public interface IWord {
- void Print();
- }
-
- public interface IWord2 : IWord {
- new void Print();
- }
-
- public abstract class Base {
- protected static string msg = "send ";
-
- public Base() {
- Console.Write(this.GetString());
- }
-
- static Base() {
- Console.Write("Never ");
- }
-
- public virtual void Print() {
- Console.Write("to ");
- }
-
- protected virtual string GetString() {
- return "llama ";
- }
- }
-
- public class Derived : Base, IWord {
-
- static Derived() {
- Console.Write(Derived.msg);
- }
-
- public new virtual void Print() {
- Console.Write("do ");
- }
-
- protected override string GetString() {
- return "a ";
- }
- }
-
- public sealed class MoreDerived : Derived, IWord {
- public override void Print() {
- Console.Write("mach");
- }
-
- void IWord.Print() {
- Console.Write("a ");
- }
-
- protected override string GetString() {
- return "do ";
- }
- }
-
- public sealed class MoreDerived2 : Derived, IWord2 {
-
- static MoreDerived2() {
- Console.Write("ine");
- }
-
- public new void Print() {
- Console.Write("job. ");
- }
-
- void IWord2.Print() {
- Console.Write("job.");
- }
-
- protected override string GetString() {
- return "'s ";
- }
- }
-
- public abstract class Unfinished : Base {
- protected new void Print() {
- Console.Write("camel ");
- }
-
- protected override string GetString() {
- return "human ";
- }
- }
-
- public class Finished: Unfinished {
- }
* This source code was highlighted with Source Code Highlighter.
The author of this answer proposes to implement the code itself inside
- using System;
-
- public class AgentSmith {
- //Never send a human to do a machine's job.
- public static void Main()
- {
- //put your code here
-
- Console.ReadLine();
- }
- }
* This source code was highlighted with Source Code Highlighter.
PS: I haven’t decided yet (but there are certain thoughts, of course), therefore, more experienced comrades, do not leave spoilers in the comments :-)
Thank you all and good luck with the solution :-)
UPD 1: fixed according to the first comment, however, I am not entirely sure that the conditions of the task prohibit the creation of new classes + inheritance. and I’m not sure that the problem can be solved without this. naturally I could be wrong.