Programming Exercises



Assume that you are writing the source code from which three different program versions will be compiled a Standard Version, a Professional Version, and an Enterprise Version. In this case, the program simply contains three different WriteLine statements that write the following three lines:

A:  "This is the standard version" B:  "This is the professional version" C:   "This is the enterprise version" 

The Standard Version only prints line A, the Professional Version only prints line B, and the Enterprise Version only prints line C. Use preprocessor directives so that you can easily control (without deleting and adding WriteLine statements, but instead by defining and undefining identifiers) which of the three WriteLine statements is being included in the program.


Use document comments and the <summary> and <remarks> tags to document the following program. Make sure you provide descriptions for all classes and methods. Use the compiler to extract the corresponding XML document.

using System; class Calculator {         public static int Sum(int x, int y)         {                 return x + y;         }         public static int Product(int x, int y)         {                 return x * y;         } } class RocketScientist {             public static void Main()             { Console.WriteLine("Estimated time to go to Mars: { 0} ",          graphics/ccc.gifEstimatedTimeToMars(23));             }             public static uint EstimatedTimeToMars(int x)             {                     return (uint) (Calculator.Sum(x, 24) + Calculator.Product(x, 21));             } } 

Consider the source code in Listing 21.4.

Listing 21.4 Train.cs
using System; class Train {     private uint distance = 0;     public void MoveBySteam(uint addDistance)     {         Console.WriteLine("Moving { 0}  kilometers by steam", addDistance);         distance += addDistance;     }     public void MoveByElectricity(uint addDistance)     {         Console.WriteLine("Moving { 0}  kilometer by electricy", addDistance);         distance += addDistance;     }     public void TestingDistance()     {         Console.WriteLine("Testing. distance instance variable: { 0} ", distance);     } } class Tester {     public static void Main()     {         Train orientExpress = new Train();         orientExpress.MoveBySteam(100);         orientExpress.MoveByElectricity(200);         orientExpress.TestingDistance();     } } 
  1. The Train class's MoveBySteam method in Listing 21.4 has become obsolete and you want the compiler to report a warning whenever it encounters a call to this method during compilation. The warning must announce that the programmer should instead use the MoveByElectricity method.

  2. The TestingDistance method should only be called during the testing of the program. Use an attribute to enable you to switch on and off any calls to this method in an easy fashion.

  3. You want to test what happens to the program if no calls are made to MoveBySteam method, while still maintaining the warning introduced in exercise a. You want to easily be able to switch this test on and of and do this independently of the calls to TestingDistance. In other words, you want the possibility of switching off the calls to MoveBySteam while the TestingDistance method is still being called, and vice versa.

  4. Because of the information you gain from the experiment you make in exercise c, you now want to make sure that no one is calling the MoveBySteam method. Make the compiler report an error instead of just a warning when it encounters a method call to this method in the source code.


C# Primer Plus
C Primer Plus (5th Edition)
ISBN: 0672326965
EAN: 2147483647
Year: 2000
Pages: 286
Authors: Stephen Prata

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: