How JustAnswer Works:

  • Ask an Expert
    Experts are full of valuable knowledge and are ready to help with any question. Credentials confirmed by a Fortune 500 verification firm.
  • Get a Professional Answer
    Via email, text message, or notification as you wait on our site.
    Ask follow up questions if you need to.
  • 100% Satisfaction Guarantee
    Rate the answer you receive.

Ask ATLPROG Your Own Question

ATLPROG, Computer Software Engineer
Category: Programming
Satisfied Customers: 7677
Experience:  MS in IT.Several years of programming experience in Java C++ C C# Python VB Javascript HTML
Type Your Programming Question Here...
ATLPROG is online now
A new question is answered every 9 seconds

Consider an array of integers as below: int[] a = {5, 2, -4,

Customer Question

Consider an array of integers as below:
int[] a = {5, 2, -4, 3, 0, -5, 7, 11, 6, 13}
Complete the method named count(int[] a) in the class Count. The method should return the number of positive numbers in the array. Thus count(a) should return 7 (not counting the 2 negative numbers and 0).
On examining your code for count (your solution to part-a), you will see a test similar to this: if (a>0) {...}. If we were to now ask you to modify the count method so that it counted the number of negative numbers, it would be trivial: All that you would have to do is change the greater-than comparison operator (“>”)to the less-than operator (“<”). If we were to ask you to modify the count method to count the number of even numbers (whether positive or negative) then you would modify your code to have a test along the following lines: if (isEven(a)) {...}. Similarly, you could change your code for other types of counting (prime numbers, numbers greater than 10, etc). Note that in each case we can do any type of counting we like but we need to edit the code for the count method. This is a simple example of enhancing the functionality of existing code. For this part of the question, you will write Java code that allows you to enhance the counting functionality without modifying the code of existing methods. You will do this using the power of inheritance and polymorphism.
Complete the method countIF that is similar to your implementation of count (from the earlier part of this question) but takes as parameter an object that implements the appropriate test. The signature of countIF is as below:
int countIF( int[] a, Predicate p)
Complete the class Predicate which has just one method that returns true if the parameter is greater than 0:
boolean test(int x){ ... ? ... }
When countIF is invoked with a Predicate object, the behaviour will be as before: Your code will count the number of integers greater than 0 (i.e., the execution would return 7).
Now complete the two classes (IsNegative and IsEven) as subclasses of the Predicate class to help you count negative (IsNegative) and even numbers (IsEven).
class IsNegative extends Predicate {...}
class IsEven extends Predicate {...}
Note: IsNegative and IsEven classes should have a test method with the same signature as before: boolean test(int x). Once these classes have been written you will be able to pass in an instance of the appropriate class to countIF to do the testing we need:
countIF( a, new IsNegative() )
countIF( a, new IsEven() )
The first call above would return 2 for our array. The second call would return 4
Submitted: 1 year ago.
Category: Programming
Expert:  LogicPro replied 1 year ago.


please upload the formatted document to and post download link here