Answer to Question #133499 in C# for aliza

Question #133499
Make a class named Fruit with a data member to calculate the number of fruits in a
basket. Create two other class named Apples and Mangoes to calculate the number
of apples and mangoes in the basket. Print the number of fruits of each type and
the total number of fruits in the basket.
1
Expert's answer
2020-09-17T06:32:06-0400
namespace FruitBasket
{
    using System;


    /// <summary>
    /// Abstract Fruit class (using as basket)
    /// </summary>
    public abstract class Fruit
    {
        /// <summary>
        /// Number of fruits static property
        /// </summary>
        public static int NumberOfFruits { get; protected set; }
    }


    /// <summary>
    /// Apple class derived from Fruit
    /// <inheritdoc cref="Fruit"/>
    /// </summary>
    public class Apple : Fruit
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="Apple"/> class.
        /// </summary>
        public Apple()
        {
            NumberOfApples++; // increase number of apples
            NumberOfFruits++; // increase number of fruits in basket
        }


        /// <summary>
        /// Number of apples static property
        /// </summary>
        public static int NumberOfApples { get; protected set; }
    }


    /// <summary>
    /// Mango class derived from Fruit
    /// <inheritdoc cref="Fruit"/>
    /// </summary>
    public class Mango : Fruit
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="Mango"/> class.
        /// </summary>
        public Mango()
        {
            NumberOfMangoes++; // increase number of mangoes
            NumberOfFruits++; // increase number of fruits in basket
        }


        /// <summary>
        /// Number of mangoes static property
        /// </summary>
        public static int NumberOfMangoes { get; protected set; }
    }


    public class Program
    {
        public static void Main(string[] args)
        {
            var apple = new Apple();
            var apple2 = new Apple();
            var apple3 = new Apple();


            var mango = new Mango();
            var mango2 = new Mango();
            var mango3 = new Mango();
            var mango4 = new Mango();
            var mango5 = new Mango();


            Console.WriteLine($"Total number of apples in the basket - {Apple.NumberOfApples}");
            Console.WriteLine($"Total number of mangoes in the basket - {Mango.NumberOfMangoes}");
            Console.WriteLine($"Total number of fruits in the basket - {Fruit.NumberOfFruits}");
        }
    }
}


Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be first!

Leave a comment

Ask Your question

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS