Class Design

. Below is a set of birds and movements they can perform.
Birds:
Penguin:
• hopping: moves 2 ft
• flying: can't fly
Hawk:
• hopping: can't hop
• flying: moves 100 ft; won't fly if World.windSpeed > 40
Robin:
• hopping: moves 1 ft; won't hop if World.temperature < 0
• flying: moves 20 ft; won't fly if World.windSpeed > 20
Crow:
• hopping: moves 1 ft; won't hop if World.temperature < 0
• flying: moves 30 ft; won't fly if World.windSpeed > 25
Requirements:
• Create a class or set of classes in C# to represent the birds.
• Create an instance of each type of bird and add them all into a single collection. Have each bird hop and then fly in turn, each time after the World changes. After all birds have moved, print out the total distance that each one has moved. (See the comments in Main below marked // TODO).
• Fill in main() to generate the desired output (which is shown under main() below).
• Use best design practices. The priority is to create maintainable code.


using System;
using System.Collections.Generic;

namespace PCD
{

public class World
{
public static int temperature = 0; // celcius
public static int windSpeed = 0; // miles per hour
}

public class MainClass
{
static void Main(string[] args)
{
// TODO: create the birds here

World. = 20;
World. temperature windSpeed = 12;

// TODO: have each bird fly & hop

// a storm arrives!
World.temperature = -10;
World.windSpeed = 30;

// TODO: have each bird fly & hop

// TODO: print total distances

}

}
}


Desired output:
The penguin moved 4 ft.
The hawk moved 200 ft.
The robin moved 21 ft.
The crow moved 31 ft.

Questions by musclebai   answers by musclebai

Showing Answers 1 - 3 of 3 Answers

using System;
using System.Collections.Generic;

namespace PCD
{
    public class World
    {
        public static int temperature = 0; // celcius
        public static int windSpeed = 0; // miles per hour
    }

    public abstract class Bird
    {
        private string _TypeName = "Bird";

        private int _MovedDistance = 0; // feet  
   
        private int _HOP_DISTANCE = 0;
        private int _FLY_DISTANCE = 0;
        private int _MIN_TEMP = 0;
        private int _MAX_WIND_SPEED = 0;

        protected int HOP_DISTANCE
        {
            get { return _HOP_DISTANCE; }
            set { _HOP_DISTANCE = value; }
        }
        protected int FLY_DISTANCE
        {
            get { return _FLY_DISTANCE; }
            set { _FLY_DISTANCE = value; }
        }
        protected int MIN_TEMP
        {
            get { return _MIN_TEMP; }
            set { _MIN_TEMP = value; }
        }
        protected int MAX_WIND_SPEED
        {
            get { return _MAX_WIND_SPEED; }
            set { _MAX_WIND_SPEED = value; }
        }

        public string TypeName
        {
            get { return _TypeName; }
            set { _TypeName = value; }
        }

        public int MovedDistance
        {
            get
            {
                return _MovedDistance;
            }
        }

        virtual public void Hop()
        {
            if (World.temperature >= _MIN_TEMP)
                _MovedDistance += HOP_DISTANCE;
        }
        virtual public void Fly()
        {
            if (World.windSpeed <= _MAX_WIND_SPEED)
                _MovedDistance += FLY_DISTANCE;
        }
    }

    public class Penguin : Bird
    {
        public Penguin()
        {
            TypeName = "Penguin";
            HOP_DISTANCE = 2;
            FLY_DISTANCE = 0;
            MIN_TEMP = -60;
        }

        public override void Fly()
        {
            return;
        }
    }

    public class Hawk : Bird
    {
        public Hawk()
        {
            TypeName = "Hawk";
            HOP_DISTANCE = 0;
            FLY_DISTANCE = 100;

            MAX_WIND_SPEED = 40;
        }
    }

    public class Robin : Bird
    {
        public Robin()
        {
            TypeName = "Robin";

            HOP_DISTANCE = 1;
            FLY_DISTANCE = 20;

            MIN_TEMP = 0;
            MAX_WIND_SPEED = 20;
        }
    }

    public class Crow : Bird
    {
        public Crow()
        {
            TypeName = "Crow";

            HOP_DISTANCE = 1;
            FLY_DISTANCE = 30;

            MIN_TEMP = 0;
            MAX_WIND_SPEED = 25;
        }
    }

    public class MainClass
    {
        static void Main(string[] args)
        {
        // TODO: create the birds here

            Penguin peng = new Penguin();
            Hawk hawk = new Hawk();
            Robin robin = new Robin();
            Crow crow = new Crow();

            List<Bird> birds = new List<Bird>();

            birds.Add(peng);
            birds.Add(hawk);
            birds.Add(robin);
            birds.Add(crow);

            World.temperature = 20;
            World.windSpeed = 12;

            // TODO: have each bird fly & hop
            foreach (Bird bird in birds)
            {
                bird.Hop();
                bird.Fly();
            }

            // a storm arrives!
            World.temperature = -10;
            World.windSpeed = 30;

            // TODO: have each bird fly & hop
            foreach (Bird bird in birds)
            {
                bird.Hop();
                bird.Fly();
            }

            // TODO: print total distances
            foreach (Bird bird in birds)
            {
                Console.WriteLine("The {0} moved {1} ft.", bird.TypeName, bird.MovedDistance);
            }
        }
    }
}

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions