Question

Hello, Someone help me with this data structure exercise. There is 2 part.

Hello,

Someone help me with this data structure exercise. There is 2 part.
media%2Fa00%2Fa00c7f9e-62b4-44f7-9470-74


media%2F9f7%2F9f7d7e51-759b-405a-aa4e-95
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Since the programming language is not used, C# programming has been used for solution.CRC Diagram CounterInterface UML Diagram Counter + void SetCounter +void IncreaseCounter positiveCounteroid SetCounter negati

//creating an interface with all abstract methods

interface CounterInterface
{
void SetCounter(); //method to set positive and negative counter
void IncreaseCounter(); //method to increase counter for non-negative numbers
void DecreaseCounter(); // method to increase counter for negative numbers
void ReturnCuttentCountAsString(); // method to display the current count
}

//Class Counter implementing the interface CounterInterface
class Counter : CounterInterface
{
//two fields are declared as static to check positive and negative count
public static int positiveCount;
public static int NegativeCount;

//setting counter values to 0
public void SetCounter()
{   
positiveCount = 0;
NegativeCount = 0;
}

//increasing the counter for positive numbers
public void IncreaseCounter()
{
positiveCount = positiveCount + 1;
}

//increasing the counter for negative numbers
public void DecreaseCounter()
{
NegativeCount = NegativeCount + 1;   
}

//returning the current counter as a string
public void ReturnCuttentCountAsString()
{
Console.WriteLine("Curent Postive Count is : " + positiveCount);
Console.WriteLine("Curent Negative Count is : " + NegativeCount);
}
}


class Program
{
static void Main(string[] args)
{
//declaring an array and assigning values
int[] numbers = { 1,2,6,5,6,98,0,-8,-7,8,9,-5,1,-2,0};

//creating object of Counter class
Counter obj = new Counter();

//call counter method to initialize values
obj.SetCounter();


//iterate over each element and increase counter of numbers based on the condition
foreach (var item in numbers)
{
if (item == 0)
{
//do nothing
}
else if (item > 0)
{
obj.IncreaseCounter(); //call method to increase positive count
}
else
{
obj.DecreaseCounter(); //call method to increase negative count
}
}

//call the method to display the result
obj.ReturnCuttentCountAsString();   
}
}


  

Add a comment
Know the answer?
Add Answer to:
Hello, Someone help me with this data structure exercise. There is 2 part.
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT