Consider an array of integers

Assignment Help JAVA Programming
Reference no: EM13165800

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[i]>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[i])) {...}. 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.

Reference no: EM13165800

Questions Cloud

Slugworth candies : Slugworth Candies, LLC, is a candy maker company that employs 450 people. The company is composed of six departments: Executive Staff (20), Human Relations (6), Finance and Accounting (15), Marketing and Sales (15), Factory (150), and Research & D..
We would like to implement the lexical order : We would like to implement the lexical order for lists. For simplicity, we only consider lists of numbers, where , >= have their usual meaning.
Write a program using for loops t : Write a program using for loops to produce the following output. Please read carefully the entire specification!
Gradesaverage, which prompts user : Write a program called GradesAverage, which prompts user for the number of students, reads it from the keyboard, and saves it in an int variable called numStudents. It then prompts user for the grades of each of the students and saves them in an i..
Consider an array of integers : 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
The interest rate per period : The interest rate per period. For example, if your loan's interest is 6.5% per year, and you are paying monthly, this would be 6.5%/12. If you are paying every two weeks, r would be 6.5%/26, because there are 26 two-week periods in a year.
. label the rows that make good power ciphers and explain : a) Make a power table for numbers mod 11. Indicate how the table shows Fermat's theorem, label the primitive roots mod 11. Explain how you can tell they are primitive roots. Label the rows that make good power ciphers and explain
Coinflip to prompt for and input a number : Write a program and name it CoinFlip to prompt for and input a number of times to flip a coin, then to output the number of "heads" and the number of "tails" that were flipped.
Write in c++ another overloaded operator : Write in C++ another overloaded operator to go in the program that has Treasury. Overload the forward slash /  so that in the main program, you can declare sale to be of type Treasury, and commission to be of type Treasury, and commispctage to be of ..

Reviews

Write a Review

JAVA Programming Questions & Answers

  Java application to declare and create a vector

Write a java application that perform the following task: declare and create a vector with initial size of 10, increment of 2

  Write a java program in a netbeans project

Write a Java program in a NetBeans project

  Write an array where the user can input 7 temperatures

Write an array where the user can input 7 temperatures; include a 'for' loop. Make sure the temperatures are larger than -100 and smaller than or equal to 120, if not, make sure the user enters a valid number. Make sure the temperature is between the..

  Client class to test implementation of the vector class

Write a client class to test your implementation of the Vector3D class thatyou implemented. Name the package in which this class is defined (projectname) vector3dapp.

  Java program to find a value at in index

Construct a main class named Array Program that generates an array of 50-integers, all ranging from one to one hundred.

  Method that receives an array and returns with no duplicate

Write a program that contrains a method that receives an array and returns a new array with no duplicates.

  Create web application uses visual web java server

Create the Web application which uses Visual Web Java Server Faces framework and either Apache Tomcat or Glassfish as the server.

  Write java application to show successive element of array

Write down java application named GoTooFar which declares the array of 5 integers and stored five values in array. write try block in which you loop to show each successive element of array.

  Javascript program that asks the use

Design a javascript program that asks the user for a username with at least eight characters, beginning with a letter and including at least one digit. Next, write a validation loop to ensure that these conditions have been met

  Make a fourth button called special

Make a fourth button called "special" that does something else notdescribed in the assignment, such as change the background color.

  Create a book class

Create a Book class. In addition to properties of author and isbn, with setters/getters, the Book should override Object's base method "boolean equals(Object)" and have a "boolean validate()" method.

  Elliptic curve encryption

write a program to implement Elliptic Curve encryption/decryption and program will read parameters, plaintext and ciphertextfrom a file named "input.txt" (under the same directory).

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd