Illustrate the power of java inheritance and polymorphism

Assignment Help JAVA Programming
Reference no: EM13781560

The learning objective of this question is to illustrate the power of Java inheritance and polymorphism. Your response to this question is in multiple parts. You will submit your answers as specified at the end of this question.

To help you get started and to guide your development efforts for this assignment we have provided scaffolding code. Download and unzip the BlueJ project count.zip. Study the code along with the description below. Edit the code and submit the completed BlueJ project. We have also provided a unit test file to help you check your code.

Now, let us look at the 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[i]>0) {...}. If we were to now ask you to modify thecount 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 thecount 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.

After completing and testing your solution, zip the entire BlueJ folder and submit it. Since your instructor will need all files in the project folder, make sure you zip the entire folder. For this question the names of the BlueJ project, Java class files, and the zip file you submit are given below:

BlueJ project: count

Java classes and Interfaces:

Count.java (this will have two methods: count, and countIF)

Predicate.java (a Java class with one method test)

IsNegative.java (extends Predicate)

IsEven.java (extends Predicate)

CountTest.java (the unit test file - This file is fully complete. Do not modify this file.)

Submit: count.zip

Reference no: EM13781560

Questions Cloud

How some of these ethical failures could have been avoided : Relate any weaknesses in ethical approaches of those involved to your theory bases of consequentialism and nonconsequentialism. Develop recommendations about how some of these ethical failures could have been avoided
Difference between random variation and assignable variation : What is process data and what is the difference between random variation and assignable variation
Exploring your culture : Part of successfully exploring other cultures is having a solid foundation in the tenets of one's culture. This assignment will have you evaluate key components of your culture.
Specific social-political or educational issue : What did the data reveal about your opinion? In other words, was it supported or disproved by your findings?Has your opinion changed after researching it? Explain your answer. What information do you think is needed to formulate an educated opinion..
Illustrate the power of java inheritance and polymorphism : Illustrate the power of Java inheritance and polymorphism. Your response to this question is in multiple parts. You will submit your answers as specified at the end of this question.
Management planning presentation : Imagine you are an executive for BP, and you are preparing a presentation for the board of directors about the organization's direction.
External environmental analysis issues : How you intend to ensure the organization's vision, mission, and people strategies and values statements are aligned with the proposed strategic plan
Write a research paper on obesity : Write a Research Paper on Obesity. Before you submit your Final Research Paper, make sure that you have Incorporated a minimum of five scholarly sources.
Outline and preliminary list of references : Complete the outline after you have done library / Internet research for evidence that bears on your hypothesis. Provide information about all of the following components of the final paper:

Reviews

Write a Review

JAVA Programming Questions & Answers

  1 comparing portions of strings write an application that

1. comparing portions of strings write an application that uses string method region-matches to compare two strings

  Statements to print a label

Add the statements to print a label in the following format (the numbers in the example output are correct for input of $4.25 per pound and 41 ounces). Use the formatting object money to print the unit price and total price and the formatting object ..

  Enterprise java beans (ejb) in software development

Enterprise Java Beans (EJB) in software development, EJB technology, EJB application, Stateless Session Beans (SLSB), Stateful Session Beans (SFSB), Message Driven Bean (MDB), Entity Bean

  Earn a fixed salary

Write a JavaTM application using NetBeansTM Integrated Development Environment (IDE) that calculates the total annual compensation of a salesperson. Consider the following factors:A salesperson will earn a fixed salary of .

  Modify the java application using java netbeans ide

Modify the Java application using Java NetBeans IDE to meet these additional and changed business requirements

  In this game we have a number of players interacting in a

in this game we have a number of players interacting in a simple way. the game takes place on a virtual

  Prepare an address book to save the complete information

Prepare an address book to save the complete information of the person - Define the class Address that can store a street address, city, state, and zip code. Use the appropriate methods to print and store the address.

  Functioning java application.

Your final project must be a complete and functioning Java application.

  Write a program to compare berlin, tehran and seoul

Write a program to compare Berlin, Tehran and Seoul information with Louisville - Your program should ask the User to select one of the cities with entering their first letters as input, "B" for Berlin, "T" for Tehran and "S" for Seoul.

  Development of a simple program involving multiple classes

Development of a Simple Program Involving Multiple Classes and development of a basic Class, development of the Country and World classes

  Define a class called counter an object of this class is

define a class called counter. an object of this class is used to count things so it records a count that is a

  Prepare a polynomial-time algorithm

Write a polynomial-time algorithm that, given an initial configuration (P_1, P_2, ..., P_k), decides if it is a winning configuration - Prepare a polynomial-time algorithm that decides if it is a winning configuration.

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