Instantiate and display a ClassifiedAd Create an application named TestNewspaperAd that instantiates and displays a ClassifiedAd object. A ClassifiedAd has fields for number of words and a price. Include get and set properties for the number of words but only a get property for the price--the price should be calculated at nine cents per word. You can create either a console or GUI app.


namespace TestNewsPaperAd
{
    class Newspaper
    {
        static void Main(string[] args)
        {
            const double rate = 0.09;
            double total;
            ClassifiedAd Article = new ClassifiedAd();
            Article.Words = 583;

            total = Article.Words * rate;

            WriteLine("The price of the {0} word article is {1}\n", Article.Words, total);
        }
    }
}
class ClassifiedAd
{
    private int words;
    private double price;

    public int Words
    {
        get
        {
            return words;
        }
        set
        {
            words = value;
        }
    }
    public double Price
    {
        get
        {
            return price;
        }
    }
}
There is an error in how you implement the Price property: according to the description your get for the price should return the number of words times 0.09. As it is, the Price will always return 0 as there is never a value assigned to it.

Try adding Console.WriteLine(Article.Price); inside your Main method for demonstration.

You can add this inside your class:

private const decimal pricePerWord = 0.09M;


Notice the use of decimal (including the M suffix) instead of double, which is a more appropriate type for currency/money values.

Then your get becomes:

public decimal Price
{
    get
    {
        return Words * pricePerWord;
    }
}




I would also recommend to make a constructor for ClassifiedAd that takes the number of words, since a classified ad without words would be nonsensical.

public ClassifiedAd(int words)
{
    this.words = words;
}




Also as suggested by Roman Gräf, a ToString override method would be a nice addition:

override public string ToString()
{
    return string.Format("The price of a {0} word classified ad is {1}\n", Words, Price);
}




Then your Main method can do everything by only using what your ClassifiedAd class provides:

static void Main(string[] args)
{
    ClassifiedAd ad = new ClassifiedAd(583);
    Console.WriteLine(ad.ToString());
    // The price of a 583 word classified ad is 52.47

    ad.Words = 900;
    Console.WriteLine(ad.ToString());
    // The price of a 900 word classified ad is 81.00
}