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

  Requests 2 numbers from the user and then passes

Write a program in java that requests 2 numbers from the user and then passes these 2 numbers to a method findMax that displays the largest number. Make sure your method is called fron main(). Test the method by passing various data to it

  The program should ask the user to enter the student'' answer

Your program should store these correct answers in an array. (Store each question's correct answer in an element of a string array.) the program should ask the user to enter the student's answers for each of the 20 questions, which should be stored i..

  Implementation of encryption and steganography in java

This is a project report showing the essential details and coding structures related with the implementation of encryption techniques and steganography in JAVA.

  Implementation the two classes

Program Specifications: In the new implementation, the two classes, Student and Grades, are defined with the following operations/methods.

  Java program for real estate agent

Write down java program for real estate agent. Program must perform the following tasks: ask users for average house price for the each of past 5 years for single family residence of 1500 square feet.

  Design a java program that has two parallel arrays

Design a java program that has two parallel arrays: a string array named people that is initialized with the names of seven of your friends, and a string array named phoneNumbers that is initialized with your friends phone numbers.

  Create file lab7.java with a main method.

Create file Lab7.java with a main method. Inside your main method, write a segment of code which instantiates an object of the Student class (This class has been created for you in the  Student.java  file that is provided as part of this lab).

  Create a file that contains your favorite movie quote

Create a file that contains your favorite movie quote. Use a text editor such as Notepad, and save the file as quote.txt. Copy the file contents,

  Write a java program called salesinfominer

Write a Java program called SalesInfoMiner that reads in a file of products, stored in text format. The program must then read in information about past sales transactions and output all the products to a new file, sorted by product description,..

  Input pairs of natural numbers

Java program to input pairs of natural numbers.

  Write a version of sumpairs

Write a version of sumPairs  that sums each component of the pairs separately, returning a pair consisting of the sum of the first components and the sum of the second components. So basically [(3,1)(10,3)] would return (13,4).

  Write a program to register students for a college

Students have names, addresses and courses. Implement the interface class RegisterStudent. RegisterStudent has one method, public boolean register, which returns the boolean value of true or false if the student is successfully registered for the ..

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