Answer to Question #338304 in C# for app

Question #338304

Class Counter

-define the data members as per the given specifications.

-define the constructor with public visibility.

-the array data will contain 0 or 1 always.

-Implement the below methods for this class:

-String getCount():

· Write a code that gives the output on the below conditions -

1. If the count of zeros is even and the count of one is also even then return "Great".

2. If the count of zeros is odd and the count of one is also odd then return "Great".

3. If the count of zeros is even and the count of odd then throw ExceptionOne with a message "One comes odd times".



1
Expert's answer
2022-05-08T14:32:58-0400


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace Console
{
    class ExceptionOne:Exception
    {
        public ExceptionOne(string msg):
            base(msg) { }
        public override string ToString()
        {
            return base.ToString();
        }
    }
    class Counter
    {
        private int[] data;
        public Counter(int size=15)
        {
            data = new int[size];
        }
        public Counter(int[] da)
        {
            data=da;
        }
        public string getCount()
        {
            int cnOne = 0;
            int cnZero = 0;
            for (int i = 0; i < data.Length; i++)
            {
                if(data[i]==1)
                    cnOne++;
                else
                    cnZero++;
            }
            if (cnOne % 2 == cnZero % 2)
            {
                return "Great";
            }
            else if (cnZero % 2 == 0 && cnOne % 2 == 1)
                throw new ExceptionOne("One comes odd times");
            return "";
        }
    }
    internal class Program
    {
        static void Main(string[] args)
        {
            int[] d=new int[10];
            Random random = new Random();
            for (int i = 0; i < d.Length; i++)
            {
                d[i]=random.Next(2);
                System.Console.Write($"{d[i],5}");
                
            }
            System.Console.WriteLine(); 
            Counter counter = new Counter(d);   
            System.Console.WriteLine(counter.getCount());
            System.Console.ReadKey();
        }
    }
}

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