CSc 103 Lab

CSc 102 Review: Generic methods, Exception handling

 


References:


Goal


Implement a generic linear search method.
Learn to use BlueJ's unit testing tools.

Design

Here is the design for a class GenericUtilities containing two generic static methods search and print.

- search: This is a generic method. Given an array of objects and a target (an object) of some class, search returns true if the target is in the array and false otherwise. The signature of this method is the following:

public static <SomeType> boolean search (SomeType [] theArray, SomeType target)

Note: this code will be applicable for objects of ANY class without restriction.
Recall that the class coding standard forbids the use of "break" or "return" within a loop or decision.  (If you are unable to invent a solution without break,  go ahead and use it;  the instructor will present the correct solution during the next class meeting).

- print: This is a generic method. Given an array of objects of some class, print outputs elements of the array on the screen (the format is up to you). The signature of this method is the following:

public static <SomeType> void print (SomeType [] theArray)

Note: this code will be applicable for objects of ANY class without restriction.
Implement this method using a for-each loop.

Procedure

Launch BlueJ.   From the menu bar, select: Tools -> Preferences -> Interface 
Check "Show unit testing tools" and click "OK."

Create a new project and a new class called GenericUtilities.

Edit the class and create a new method that has the signature for the search method above.
Create a method body that simply says "return false;".  Compile the class
(it should compile without error).

Right-click on the GenericUtilities class and select "Create Test Class" from the context menu.

Open the GenericUtilitiesTest class in the editor and replace the auto-generated code
with this instructor provided code.

Compile the test class (it should compile without error).

In the BlueJ main window, click the "Run Tests" button.  A dialog should appear showing the test failed.

Study the instructor provided test code and add a single line comment above
each "assert" statement explaining what the test does.

Implement (write the code for) the search and print methods.  Be sure your code conforms to the class coding standard.  

In the BlueJ main window, click the "Run Tests" button.  A dialog should appear showing a successful test run.

In a manner similar to the instructor provided code, create two new test methods,
one that will test your search with floating point values, and one that will test your
search with strings.

Create a method that will invoke your print method.

Execute your tests and make any corrections necessary until they pass.

Submit your completed code and tests to Web-CAT. Only one submission is needed for each pair, as long as you tell Web-CAT who the partners are.

(Optional) Write a summary of the important points you learned from this activity in your lab notebook.