Answer to Question #316937 in C# for DOHC 44

Question #316937

1.Make a Console App with a class that encapsulates the following information about a bird: English name, Latin name, and Date last seen. Allows the user to fill in the details about a bird. The data is saved in a Bird object, which is then displayed to the user. 2.In the program, create a superclass called Animal and update class Bird to inherit from it. Create a new class named Butterfly that allows the user to collect butterfly data. 3.Display the percentage of birds and butterflies in the app when displaying the birds and butterflies. When there are no entries in the app yet, use exception handling to handle the divide by zero. 4.Create an interface called ISighting with a single method called displaySighting. Implement the interface in Animal but leave the displaySighting method abstract. Implement the displaySighting method in both the Bird & Butterfly classes to display all the information known about the animal to the console.Update app so that the user can choose to add a bird or a butterfly.


1
Expert's answer
2022-03-23T18:30:28-0400
interface ISighting
    {
        void displaySighting();
    }


    abstract class Animal : ISighting
    {
        public string EnglishName { set; get; }
        public string LatinName { set; get; }
        public DateTime DateLastSeen { set; get; }


        abstract public void displaySighting();
    }
    class Bird : Animal
    {
        public override void displaySighting()
        {
            Console.WriteLine("Bird Information \n" +
                $"English Name: {EnglishName} \n" +
                $"Latin Name: {LatinName} \n" +
                $"Date Last Seen: {DateLastSeen} \n");
        }
    }
    class Butterfly : Animal
    {
        public override void displaySighting()
        {
            Console.WriteLine("Butterfly Information \n" +
               $"English Name: {EnglishName} \n" +
               $"Latin Name: {LatinName} \n" +
               $"Date Last Seen: {DateLastSeen} \n");
        }
    }
    internal class Program
    {
        static void Main(string[] args)
        {
            List<Bird> birds = new List<Bird>();
            List<Butterfly> butterflies = new List<Butterfly>();


            bool exit = new bool();
            while (!exit)
            {
                Console.WriteLine("1.Bird Information \n" +
                    "2.Butterfly Information \n" +
                    "3.Add Bird \n" +
                    "4.Add Bitterfly \n" +
                    "5.Display the percentage of birds and butterflies \n" +
                    "6.Exit");
                switch (Console.ReadLine())
                {
                    case "1":
                        {
                            if (birds.Count > 0)
                                foreach (var bird in birds)
                                    bird.displaySighting();
                            else
                                Console.WriteLine("No entries");
                            break;
                        }
                    case "2":
                        {
                            if (butterflies.Count > 0)
                                foreach (var butterfly in butterflies)
                                    butterfly.displaySighting();
                            else
                                Console.WriteLine("No entries");
                            break;
                        }
                    case "3":
                        {
                            Bird bird = new Bird();
                            Console.WriteLine("Bird record");
                            Console.Write("English Name: ");
                            bird.EnglishName = Console.ReadLine();
                            Console.Write("Latin Name: ");
                            bird.LatinName = Console.ReadLine();
                            Console.Write("Date Last Seen: ");
                            bird.DateLastSeen = DateTime.Parse(Console.ReadLine());
                            birds.Add(bird);
                            break;
                        }
                    case "4":
                        {
                            Butterfly butterfly = new Butterfly();
                            Console.WriteLine("Butterfly record");
                            Console.Write("English Name: ");
                            butterfly.EnglishName = Console.ReadLine();
                            Console.Write("Latin Name: ");
                            butterfly.LatinName = Console.ReadLine();
                            Console.Write("Date Last Seen: ");
                            butterfly.DateLastSeen = DateTime.Parse(Console.ReadLine());
                            butterflies.Add(butterfly);
                            break;
                        }
                    case "5":
                        {
                            if (birds.Count == 0 && butterflies.Count == 0)
                            {
                                Console.WriteLine("No entries");
                                break;
                            }
                            Console.WriteLine($"Percentage butterflies: { butterflies.Count / (birds.Count + butterflies.Count) * 100}%");
                            Console.WriteLine($"Percentage birds: { birds.Count / (birds.Count + butterflies.Count) * 100}%");
                            break;
                        }
                    default:
                        {
                            exit = true;
                            break;
                        }
                }
            }
        }
    }

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