ITEC 120
L08a: Searching Arrays

Objectives

To successfully complete this lab you will solve problems with arrays and pass arrays to and from methods. You will also gain more experience using the test-driven development (TDD) process.

Assignment

You will develop methods that operate on arrays of numbers.

Download the SearchDrv.java and TestSrv.java classes and familiarize yourself with the code. SearchDrv is a test driver. The test driver uses methods in the TestSrv class to validate test results.

You will a create a class named SearchSrv and create a stub method for each method (range, isFound, timesFound, and reverse) tested by the SearchDrv test driver. Compile all three classes and execute the test driver.

A stub method is a method that compiles but doesn't return the correct results. It might just do nothing, and return zero. You write a stub method just so you can get your test driver to run, so you can see what it's doing. Then, you can work on getting your methods to work correctly, running the test driver again and again to see if your test cases are passing.

Use this process as you develop these four methods:

timesFound

timesFound takes two parameters: an array of integers and an integer. It returns the number of times the given integer was found in the given array. (So it returns an int).

isFound

isFound takes two parameters: an array of integers and an integer. Notice the name of the method. This gives us a hint about the type of method this is - it answers a question. The question is: Is the given element found in the given array?

range

range takes one parameter: an array of integers, and returns the range between the biggeset and smallest element in the array.

reverse

takes an array of integers as a parameter, and returns an array of integers, which contains the same elements as in the given array, but in reverse order. It takes an array, reverses it, and returns it.

 

Copy your SearchSrv.java file four times to create the following classes: SearchWhile.java, SearchFor.java, SearchDo.java, and SearchForEach.java. You now have five files with stub methods. In the SearchSrv class, implement each method with whatever type of loop you think is most appropriate.

Loops

Rewrite each method with each type of loop (While, For, Do, and For Each). For example, develop all of the methods in the SearchWhile class with a While loop. Rewrite each method to gain additional practice solving these problems. All of your methods should pass all of your test cases (of course you will need to add code to your test driver to call the methods in each of the 5 files). You will learn a lot about loops by completing this exercise.

Submit Your Assignment

Submit all of your files: SearchDrv.java, TestSrv.java, SearchSrv.java, SearchWhile.java, SearchFor.java, SearchDo.java, and SearchForEach.java to the L08a dropbox on D2L.