Answers to Chapter 15 Review Questions

   


Chapter 15

1:

What is the full name of the class defined inside these two namespaces?

 namespace BliposSoft {     namespace RocketSimulation     {         class Rocket         {             ...         }     } } 
A:

BliposSoft.RocketSimulation.Rocket

2:

A programmer in an uncontrolled moment of vanity gave the following name to a class:

 ThisIsTheBestRocketClassInTheWholeWideWorldAndTheWholeWideBliposAsWell 

The claim in the name is true, so you need to use this class extensively in your program, but you don't want to write the long name. How can this be avoided?

A:

You can create a shorter alias, such as Rocket for ThisIsTheBestRocketClassInTheWholeWideWorldAndTheWholeWideBliposAsWell with the following line:

 using Rocket = ThisIsTheBestRocketClassInTheWholeWideWorldAndTheWhole WideBliposAsWell; 

You can now use the name Rocket in your code instead of the long cumbersome name.

3:

The following class is found in a compilation unit that will be part of a DLL assembly.

 class Bicycle {     double CalculateAirResistance()     {         ...     }     double WheelRotationsPerMinute()     {         ...     } } 

The access modifiers in front of the class and the two methods are missing. Which access modifiers should we put in front of the class and the methods to:

  1. Allow all of the class and both methods to be used in another assembly.

  2. Prohibit any part of the class to be used in another assembly, but allow the methods to be used within their own assembly.

  3. Allow the CalculateAirResistance method to be accessed from another assembly, but not the WheelRotationsPerMinute method.

  4. Allow all of the class to be used in another assembly along with CalculateAirResistance, while WheelRotationsPerMinute must only be accessed from within the class.

A:
  1. Declare both the class and the two methods for public.

  2. Declare both the class and the two methods for internal.

  3. Declare the class and CalculateAirResistance to be public, but WheelRotationsPerMinute to be internal.

  4. Declare the class and CalculateAirResistance to be public and WheelRotationsPerMinute to be private.

4:

You are working for a software company called BikeTech that is planning to write a class library containing the classes from the three main compartments in the company: Bicycle Design, Health and Fitness, and Computer Mapping. Use C# code to illustrate the overall namespace layout you would suggest for this class library.

A:
 namespace BikeTech {     namespace BicycleDesign     {         ...     }     namespace HealthAndFitness     {         ...     }     namespace ComputerMapping     {         ...     } } 
5:

Three different compilation units contain each of the following namespace definitions:

Compilation unit 1:

 namespace MyCompany {     public class Bicycle     {         ...     } } 

Compilation unit 2:

 namespace MyCompany.Design {     public class Drawer     {         ...     } } 

Compilation unit 3:

 namespace MyCompany.Design.Tools {     public class Cutter     {         ...     } } 

The compilation units are to be compiled into the same assembly. Instead of having three compilation units, write one compilation unit with namespace definitions matching those contained in the three displayed compilation units. Use the nested C# namespace definition format that looks like the following:

 namespace <outer_namespace> {     namespace <inner_namespace>     {         etc. 
A:
 namespace MyCompany {     public class Bicycle     {         ...     }     namespace Design     {         public class Drawer         {             ...         }         namespace Tools         {             public class Cutter             {                 ...             }         }     } } 
6:

You have written two source files called Bicycle.cs and Person.cs. You want to compile them and create a DLL assembly called healthlib.dll. You must also reference the following two assemblies to the compiler for this compilation: mathlib.dll and anatomylib.dll. Write the compiler command you would give to the compiler.

A:
 csc /out:healthlib.dll /t: library /r:mathlib.dll;anatomylib.dll Bicycle.cs Person.cs 
7:

What can you use Ildasm for?

A:

Ildasm let's you inspect the contents of any assembly by displaying its ingredients in a user-friendly GUI.

Answers to Chapter 15 Programming Exercises

1:

Recall your first object-oriented source code SimpleElevatorSimulation.cs in Listing 5.1 of Chapter 5, "Your First Object-Oriented C# Program." It contained three classes in the same compilation unit. In hindsight, it didn't follow the standard convention of one class per compilation unit. Rectify this problem and rewrite the program so that it more correctly contains three compilation units, each with its own class (Elevator, Person, and Building) and all classes properly structured inside a neat namespace hierarchy. Turn these three compilation units into a DLL assembly. (Hint: You should rename the Main method from the Building class, because this new DLL is for reuse only). Write another source file that performs an Elevator simulation similar to that performed by our original program, but this time it uses the namespaces and their classes located in the DLL assembly you created before from the three classes.

A:

Exercise 1: Create the following three compilation units. (The contents of the individual classes have been omitted for space reasons. You can simply insert the code from Listing 5.1 here.)

Compilation unit one with the following overall content is called Elevator.cs:

 using System; namespace ElevatorSimulation {     public class Elevator     {         ...     } } 

Compilation unit two with the following content is called Person.cs:

 using System; namespace ElevatorSimulation {     public class Person     {         ...     } } 

Compilation unit three with the following content is called Building.cs. (The Building class contains the RunSimulation method instead of the Main method.)

 using System; namespace ElevatorSimulation {     public class Building     {         ...         public void RunSimulation()         {             ...         }     } } 

Compile the three compilation units into a DLL called ElevatorSimulation.dll with the following compiler command:

 csc /out: ElevatorSimulation.dll /t:library Elevator.cs Person.cs Building.cs 

You can now write a separate small program (called, for example, Simulator.cs) that calls the RunSimulation method of the Building class. Its main ingredients are as follows:

 using System; using ElevatorSimulation; class Simulator {     public static void Main()     {         Building aBuilding = new Building();         aBuilding.RunSimulation();     } } 

When you compile Simulator.cs, you must reference the ElevatorSimulation.dll as in the following command that generates an .exe file called Simulator.exe.

 csc /r:ElevatorSimulation.dll Simulator.cs 

To run the program, you can now simply type

 Simulator 

which should result in the familiar output from Listing 5.1.

Observe that you could alternatively compile just the Elevator.cs and Person.cs compilation units into a .dll and then create your own simulation similar to that contained in the Building class. This latter class would have to be compiled with the .dll containing the code of Elevator.cs and Person.cs. This approach is used in the next answer.

2:

Perform the same type of exercise as exercise one, but this time use the BankSimulation.cs source file in Listing 10.14 of Chapter 10, "Arrays Part I: Array Essentials," as the basis. Write one compilation unit for each of the two classes Account and Bank and put them in a suitable namespace hierarchy. Compile this part into a DLL assembly. Write the source code for a bank simulation program that uses the namespaces and classes of the created DLL assembly. The functionality of this program should be identical to that of the program we originally created from Listing 10.14.

A:

Exercise 2: First create the following two compilation units. (The contents of the individual classes have been omitted for space reasons. You can simply insert the code from BankSimulation.cs.)

Compilation unit one with the following general content is called Account.cs:

 using System; namespace BankSimulation {     public class Account     {         ...     } } 

Compilation unit two with the following content is called Bank.cs:

 using System; namespace BankSimulation {     public class Bank     {         ...     } } 

Let the compiler generate a .dll called Banklib.dll with the following command:

 csc /out: Banklib.dll /t:library Bank.cs Account.cs 

Write a compilation unit with the contents of the BankSimulation class from Listing 10.14 and call it BankSimulation.cs with the following content:

 using System; using BankSimulation; {     public class BankSimulator     {         public static void Main         {             ...         }     } } 

Compile this compilation unit into an .exe assembly called BankSimulation.exe with the following compiler command:

 csc /r:Banklib.dll BankSimulation.cs 

You can now run the bank simulation with the following command:

 BankSimulation 

This should generate the same output as that of Listing 10.14.


   


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

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net