Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > BOOK: Beginning Visual C#
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Beginning Visual C#
This is the forum to discuss the Wrox book Beginning Visual C#, Revised Edition of Beginning C# for .NET v1.0 by Karli Watson, David Espinosa, Zach Greenvoss, Jacob Hammer Pedersen, Christian Nagel, Jon D. Reid, Matthew Reynolds, Morgan Skinner, Eric White; ISBN: 9780764543821
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning Visual C# section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old September 24th, 2004, 04:14 PM
Authorized User
 
Join Date: Jul 2004
Location: Saint Peters, Missouri, USA.
Posts: 59
Thanks: 0
Thanked 0 Times in 0 Posts
Default Beginning Visual C# Exercises - Chapter 09 Answers

1. Sealed classes can not serve as a base class (no inheritance)

2. The constructor is declared private preventing object instances of the class

3. The class acts as a container for static members allowing global access to each property,
            method and field without bounds of object encapsulation
        Once the namespace is set up, static members of the class may be called throughout the code
            as when invoking methods of the Console class from the System namespace

4. using System;

        namespace Vehicles
        {
            public interface iPassengerCarrier
            {
            }
            public interface iHeavyLoadCarrier
            {
            }
            public class VehicleType
            {
            }
            public class Train : VehicleType
            {
            }
            public class PassengerTrain : Train, iPassengerCarrier
            {
            }
            public class FreightTrain : Train, iHeavyLoadCarrier
            {
            }
            // Note: 424DoubleBogey is an illegal C# name
            public class DoubleBogey : Train
            {
            }
            public class Car : VehicleType
            {
            }
            public class Compact : Car, iPassengerCarrier
            {
            }
            public class Pickup : Car, iPassengerCarrier, iHeavyLoadCarrier
            {
            }
            public class SUV : Car, iPassengerCarrier
            {
            }
        }

5. using System;
        using Vehicles;

        namespace Traffic
        {
            class Class1
            {
                public static int census;

                private static void AddPassenger(object accomodation)
                {
                    try
                    {
                        // Cast a vehicle class into the passenger interface
                        iPassengerCarrier transport = checked((iPassengerCarrier)accomodation);
                        // Only vehicle objects derived from passenger interface arrive here
                        Console.WriteLine(transport.ToString() + " will carry " +
                            Convert.ToString(census) + " passengers.");
                    }
                    catch (System.InvalidCastException)
                    {
                        // The vehicles without passenger interface, so ignore
                    }
                    catch (Exception e)
                    {
                        // Report an unanticipated exception
                        Console.WriteLine(e.ToString());
                    }
                }

                [STAThread]
                public static void Main(string[] args)
                {
                    Compact carCompact = new Compact();
                    census = 5;
                    AddPassenger(carCompact);

                    FreightTrain boxCar = new FreightTrain();
                    census = 15;
                    AddPassenger(boxCar);

                    PassengerTrain railCar = new PassengerTrain();
                    census = 120;
                    AddPassenger(railCar);

                    SUV miniVan = new SUV();
                    census = 8;
                    AddPassenger(miniVan);

                    Pickup farmHaul = new Pickup();
                    census = 3;
                    AddPassenger(farmHaul);

                    DoubleBogey monsterCar = new DoubleBogey();
                    census = 1000;
                    AddPassenger(monsterCar);
                }
            }
        }
Reply With Quote
  #2 (permalink)  
Old June 19th, 2005, 09:03 AM
Registered User
 
Join Date: Jun 2005
Location: Shipston-on-stour, Warwickshire, United Kingdom.
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,

Your answer for chapter 9 exercise 5 does not do what was requested therefore it is technically wrong.

Bye,
Peter
Reply With Quote
  #3 (permalink)  
Old June 20th, 2005, 01:45 PM
Authorized User
 
Join Date: Jul 2004
Location: Saint Peters, Missouri, USA.
Posts: 59
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Create a console application project, Traffic, that references Vehicles.dll (created in Q4 above). Include a function, AddPassenger(), that accepts any object with the IPassengerCarrier interface. To prove that the code works, call this function using instances of each object that supports this interface, calling the ToString() method inherited from System.Object on each one and writing the result to the screen.

My answer does more than the exercise requested, but it certainly does what was requested.
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Beginning Visual C# Exercises - Chapter 09 seblake C# 0 July 27th, 2004 03:23 PM



All times are GMT -4. The time now is 10:35 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.