Answer to Question #293627 in C# for samy

Question #293627

Write a class representing any Vegetable described by a color passed in the constructor. Write a class Carrot derived from Vegetable, which in the constructor assumes the price per kilogram. Write Buy method (which takes the number of kilograms as the parameter) in such a way that it is possible to write the code: Vegetable r = new Carrot (15, "red"); // 15 PLN / kg, color: red double toPay = r.Buy (0.5); // Returns the price of 0.5 kg of the carrot


1
Expert's answer
2022-02-03T08:59:39-0500


using System;
using System.Collections.Generic;
using System.Globalization;


namespace App
{


    abstract class Vegetable
    {
        private string color;






        public Vegetable() { }
        public Vegetable(string color)
        {
            this.color = color;
        }


        public string Color
        {
            get { return color; }
            set { color = value; }
        }
        public abstract double Buy(double numberKilograms);
    }


    class Carrot: Vegetable
    {
        private double pricePerKilogram;
        public Carrot() { }
        public Carrot(double pricePerKilogram, string color)
            : base(color)
        {
            this.pricePerKilogram = pricePerKilogram;
        }
        public override double  Buy(double numberKilograms)
        {
            return numberKilograms * pricePerKilogram;
        }
    }
    class Program
    {
        static void Main(string[] args){
            Vegetable r = new Carrot(15, "red"); // 15 PLN / kg, color: red 
            double toPay = r.Buy(0.5); // Returns the price of 0.5 kg of the carrot
            Console.Write("The price of 0.5 kg of the carrot: {0}",toPay);


            Console.ReadLine();
        }
    }
}

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 the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS