Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Examine the following code. Is there any problem according to design by contract

ID: 3726469 • Letter: E

Question

Examine the following code. Is there any problem according to design by contract? If yes, please list the problem(s).

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace DesignByContractDemo

{

    class Program

    {

        static void Main(string[] args)

        {

            Console.WriteLine("Please input numerator:");

            int numerator = int.Parse(Console.ReadLine());

            Console.WriteLine("Please input denominator:");

            int denominator = int.Parse(Console.ReadLine());

            Division d = new Division(numerator, denominator);

            if (denominator == 0)

                Console.WriteLine("Result = {0} ", double.PositiveInfinity);

            else

                Console.WriteLine("Result = {0} ", d.Divid());

            Console.ReadKey();

        }

    }

    class Division

    {

        double numerator;

        double denominator;

        public Division(double _numerator, double _denominator)

        {

            numerator = _numerator;

            denominator = _denominator;

        }

        public double Divid()

        {

            if (denominator != 0)

                return numerator / denominator;

            else

                return double.PositiveInfinity;

        }

    }

}

Explanation / Answer

Numerator and Denominator are being taken as Integer which violates the Contract


We cannot divide double/double

Or Double/Integer because Numnerator and denominator are being input as Integer which could be wrong.


Thanks, let me know if there is any concern.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote