Assignment 1

CS 146
due February 5, 2003
60 points

Define a class DuplicateTester that contains methods to determine whether an array of integers contains duplicate elements. This class is to provide the following three public methods: hasDuplicatesUnsorted, checkDuplicatesSorted, and isSorted.

The hasDuplicatesUnsorted method is to take as its single argument an array of integers, and return a boolean value which is true if the array has two equal elements in different locations, and false otherwise.

The hasDuplicatesSorted method has the same specification, except that it may assume that that array is sorted in nondecreasing order. Your method should be more efficient than the hasDuplicatesUnsorted method. Don't forget to document that your method will not work properly for unsorted arrays.

The isSorted method is to take as its single argument an array of integers, and return a boolean value which is true if the array is sorted in nondecreasing order, and false otherwise.

For each of the first two methods, if duplicate elements are found, you are to print a message identifying the two elements. You should say which array locations contain the elements, so your message should read something like "elements 12 and 25 are equal". You need not identify more than one pair of duplicate elements per method invocation.

Test your class by executing the test file A1.java, which is provided on the class web site. To check your answer, it will be helpful to have a debugger available. If instead, you choose to print the contents of the array, please make sure that you remove the code that does so from your final submission.