Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 29th, 2004, 04:30 PM
Authorized User
 
Join Date: Jul 2004
Posts: 59
Thanks: 0
Thanked 0 Times in 0 Posts
Default Beginning Visual C# Exercises - Chapter 10

1. public class MyClass
        {
            protected string myString;

            public string ContainedString
            {
                set
                {
                    myString = value;
                }
            }

            public virtual string GetString()
            {
                return myString;
            }

            public MyClass(string theMessage)
            {
                myString = theMessage;
            }
        }

        ... Instantiate and call as follows
        MyClass testThis = new MyClass("This is my base class!");
        Console.WriteLine(testThis.GetString());

2. public class MyDerivedClass : MyClass
        {
            public override string GetString()
            {
                return base.GetString() + " (output from derived class)";
            }

            public MyDerivedClass(string theMessage) : base(theMessage)
            {
            }
        }

        ... Instantiate and call as follows
        MyDerivedClass testThis = new MyDerivedClass("This is my derived answer!");
        Console.WriteLine(testThis.GetString());

3. public class MyCopyableClass
        {
            public int theNumber;

            public object GetCopy()
            {
                // Make a shallow copy
                return MemberwiseClone();
            }
        }

        ... Instantiate and compare field value
        MyCopyableClass theOriginal = new MyCopyableClass();
        theOriginal.theNumber = 33;
        MyCopyableClass theDuplicate = (MyCopyableClass)theOriginal.GetCopy();
        // field value in both objects should match
        if (theOriginal.theNumber == theDuplicate.theNumber)
            Console.WriteLine("Test 1: The two field values match each other");
        else
            Console.WriteLine("Test 1: Oh no! The two field values differ");

        theDuplicate.theNumber = 16;
        // Field value now should not match
        if (theOriginal.theNumber == theDuplicate.theNumber)
            Console.WriteLine("Test 2: The two field values match each other");
        else
            Console.WriteLine("Test 2: Oh no! The two field values differ");

4. ... At top of code
        using Ch10CardLib;

        ... Takes many attempts to obtain a flush!!
        int i, j;
        const int rawDeal = 5;
        string theHand, theSuit;
        bool isFlush;

        // Get the deck ready to deal
        Deck myDeck = new Deck();
        myDeck.Shuffle();

        // Deal maximum 50 cards @ 5 cards per hand (loop 10 times)
        for (i = 0; i < 10; i++)
        {
            // Initialize the hand before each deal
            theHand = "";
            // Peek at the suit of first card dealt
            theSuit = myDeck.GetCard((i * rawDeal) + 1).suit.ToString();
            // Presume this hand will be a flush
            isFlush = true;

            // Deal five cards and compare suit to first card
            for (j = 1; j <= rawDeal; j++)
            {
                // Look at card dealt
                Card tempCard = myDeck.GetCard((i * rawDeal) + j);
                // Stow card into your hand
                theHand = theHand + tempCard.ToString() + "\n";
                // Do you still have a flush?
                isFlush = isFlush && tempCard.suit.ToString() == theSuit;
            }

            // Celebrate if you have a flush!
            if (isFlush == true)
            {
                Console.WriteLine(theHand + "Flush!");
                continue;
            }

            // Tell the world you are an unlucky poker player
            if (i == 9 && isFlush == false)
                Console.WriteLine("No flush");
            }
        }
 
Old July 29th, 2004, 05:10 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Hey,

And do you have an issue with the code, and what specifically?

Brian





Similar Threads
Thread Thread Starter Forum Replies Last Post
Beginning Visual C# Exercises - Chapter 09 seblake C# 0 July 27th, 2004 03:23 PM
Beginning Visual C# Exercises - Chapter 07 seblake C# 0 July 20th, 2004 02:07 PM
Beginning Visual C# Exercises - Chapter 03 seblake C# 1 July 19th, 2004 09:16 AM
Beginning Visual C# Exercises - Chapter 06 seblake C# 1 July 19th, 2004 09:15 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.