Playing with Properties

Below is the complete code for the Playing with Properties Try It Out! problem.

The entire solution can be downloaded here:

PlayingWithProperties.zip

The Color Class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PlayingWithProperties
{
    /// <summary>
    /// Represents a color as red, green, blue, and alpha components.
    /// </summary>
    public class Color
    {
        /// <summary>
        /// Stores the red component of the color.
        /// </summary>
        private byte red;

        /// <summary>
        /// Get or set the red component of the color.
        /// This is an example of a normal, not auto-implemented property.
        /// the private instance variable called 'red' is this property's
        /// backing field.
        /// </summary>
        public byte Red
        {
            get
            {
                return red;
            }
            set
            {
                red = value;
            }
        }

        /// <summary>
        /// Get or set the green component of the color.
        /// </summary>
        public byte Green { get; set; }

        /// <summary>
        /// Get or set the blue component of the color.
        /// </summary>
        public byte Blue { get; set; }

        /// <summary>
        /// An auto-implemented property with different accessibility levels.
        /// You can do the same thing with non auto-implemented properties.
        /// </summary>
        public byte Alpha { get; private set; }

        /// <summary>
        /// Creates a new color with all of the components specified.
        /// </summary>
        public Color(byte red, byte green, byte blue, byte alpha)
        {
            // Here, I'm setting things through the properties. For the
            // explicitly implemented properties (not auto-implemented) I
            // could theoretically go directly to the backing field, but it
            // is usually safer to go through the property instead, even
            // inside of the class.

            Red = red;
            Green = green;
            Blue = blue;
            Alpha = alpha;
        }

        /// <summary>
        /// Creates a new color with only red, green, and blue components
        /// specified. In this case, alpha will be 255, or completely 
        /// opaque.
        /// </summary>
        public Color(byte red, byte green, byte blue)
        {
            Red = red;
            Green = green;
            Blue = blue;
            Alpha = 255;
        }
    }
}

The Ball Class

using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PlayingWithProperties
{
    /// <summary>
    /// Represents a ball, with a color and size. This also
    /// keeps track of the number of times the ball has been thrown.
    /// </summary>
    public class Ball
    {
        /// <summary>
        /// Gets or sets the color of the ball.
        /// </summary>
        public Color Color { get; set; }

        /// <summary>
        /// Stores the radius/size of the ball. The radius is in
        /// generic units.
        /// </summary>
        public float Radius { get; set; }

        /// <summary>
        /// Keeps track of the number of times this ball has been thrown.
        /// </summary>
        private int timesThrown;

        /// <summary>
        /// Creates a new ball with a given size and color.
        /// </summary>
        /// <param name="color"></param>
        /// <param name="radius"></param>
        public Ball(Color color, float radius)
        {
            Color = color;
            Radius = radius;
            this.timesThrown = 0;
        }

        /// <summary>
        /// Pops the ball, changing it's size to 0.
        /// </summary>
        public void Pop()
        {
            Radius = 0;
        }

        /// <summary>
        /// Throws the ball. This will increment the count
        /// of the number of times the ball has been thrown only
        /// if the ball has not been popped.
        /// </summary>
        public void Throw()
        {
            if (Radius > 0)
            {
                timesThrown++;
            }
        }

        /// <summary>
        /// Returns the number of times that the ball has been thrown
        /// so far. This will be updated whenever the Throw method is
        /// called.
        /// </summary>
        /// <returns></returns>
        public int GetTimesThrown()
        {
            return timesThrown;
        }
    }
}

The Program Class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DesigningAndBuildingClasses
{
    class Program
    {        
        /// <summary>
        /// Tests the Ball and Color classes.
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            // Create two different balls of different colors and sizes.
            Ball bigRed = new Ball(new Color(255, 0, 0), 5);
            Ball littlePurple = new Ball(new Color(255, 0, 255), 3);

            // Throw the big red ball around a few times.
            bigRed.Throw();
            bigRed.Throw();
            bigRed.Throw();
            Console.WriteLine(bigRed.GetTimesThrown());

            // Keep throwing it, and make sure that the number of
            // times thrown keeps going up.
            bigRed.Throw();
            bigRed.Throw();
            Console.WriteLine(bigRed.GetTimesThrown());

            // Throws the little purple ball around.
            littlePurple.Throw();
            littlePurple.Throw();
            Console.WriteLine(littlePurple.GetTimesThrown());

            // Pop the little purple ball and make sure that the throw
            // count doesn't keep going up if the ball has been popped.
            littlePurple.Pop();
            littlePurple.Throw();
            Console.WriteLine(littlePurple.GetTimesThrown());

            Console.ReadKey();

        }
    }
}