Answer to Question #188610 in C# for CHANDRASENA REDDY

Question #188610

Sanjay has written the following code. The purpose is to create a Bird class and implement function overloading. The code has some errors. Debug the code.


private class Bird

{

public string Name;

public double Maxheight;

public Bird() //Default Constructor

{

this.Name = Mountain Eagle;

this.Maxheight = “500”; // TODO: Add constructor logic here

}

public Bird(string birdname, double max_ht) //Overloaded Constructor

{

this.Name = “Another Bird”;

this.Maxheight = null;

}

public void fly()

{

Console.WriteLine(“this.Name is flying at altitude this.Maxheight”);

}

public void fly(string AtHeight)

{

if(AtHeight <= this.Maxheight)

Console.WriteLine(this.Name + " flying at " + AtHeight.ToString());

elseif

Console.WriteLine(this.Name cannot fly at this height);

}

}

The code in the Main function is as follows:

Bird b = new Bird(“Eagle” , double.Parse(“200”));

b.fly();

b.fly(double.Parse(“300”))


1
Expert's answer
2021-05-04T05:41:09-0400
using System;

namespace Test
{
    class Bird
    {
        private string Name;
        private double Maxheight;

        public Bird() //Default Constructor
        {
            this.Name = "Mountain Eagle";
            this.Maxheight = 500;
        }

        public Bird(string birdname, double max_ht) //Overloaded Constructor
        {
            this.Name      = birdname;
            this.Maxheight = max_ht;
        }

        public void fly()
        {
            Console.WriteLine(this.Name + " is flying at altitude "+ this.Maxheight);
        }

        public void fly(double AtHeight)
        {
            if(AtHeight <= this.Maxheight)
                Console.WriteLine(this.Name + " flying at " + AtHeight.ToString());
            else
                Console.WriteLine(this.Name + " cannot fly at this height");
        }
    }

    class BirdTest
    {
        static void Main()
        {
            Bird b = new Bird("Eagle" , 200);

            b.fly();

            b.fly(300);
        }
    }
}

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