Download searches.java | Computer Science homework help

1.

 Download Searches.java; this file defines a Searches class providing versions of  the

search algorithms linearSearch and binarySearch. Make sure

the Searches class compiles before continuing.

2. Make these algorithms generic, so that they will work on any object type. Using the

techniques described in Chapter 2, modify each method: change the types of  the

parameters, and (if  appropriate) local variables, to use the type specifier T for each

method.

Note: for static methods, Java requires us to add <T> after the keyword static.  So for

example: 

public static <T> boolean contains( …etc…)

3. Next, because we want to search for any kind of  objects and not just ints, we have to be

careful about how we compare them (both for equality and greater than/less than). The

operators for primitive types will no longer work correctly. You know that every class

inherits or overrides the equals method from Object. This will work in place of  the

== operator for the linear searches.

For other comparisons, as in binarySearch, take advantage of 

the compareTo method provided by the Comparable interface. Use this method in

place of  greater than/less than operators.  

You can read up on this interface in the text, Chapter 5, page 314-315; the basic idea is

that we can compare any objects which implement the interface using the following

method:

public int compareTo(String anotherString) // this is the

version for the String class

This method returns the value 0 if  the strings are equal; a value less than 0

if this string is lexicographically less than anotherString; and a value greater

than 0 if this string is lexicographically greater than anotherString. 


Note that we will simply use this method and assume that it is defined for the types

used with our search methods. You do not have to implement your own!

4. Now we have three generic methods that will work on different types of  objects.  The

trouble is, if  I call these methods with an array of  objects that don’t

implement Comparable, this code will fail. We’d like to ensure that the type parameter

used by our generic methods “screens out” object types that aren’t searchable. We do that

by using this extended form of  generics:

<T extends Comparable<? super T>>

What does all that mean?  For our purposes, focus in on the first part: here, T is just the

type parameter and <T extends Comparable> constrains that type parameter to

be one that provides a compareTo method, thereby implementing

the Comparable interface. If  you add this more complex form of  type parameter,

things that can’t be compared won’t compile if  someone tries to pass an array of  a noncomparable

type.

5.

 

6. Write a short program SearchTest.java to test your methods with two different array

types. Do 2 successful searches and 2 unsuccessful searches with each method and print

the results. Use these array declarations:

   Integer[] testInts = {-12, -7, -4, -2, 0, 3, 5, 9, 13,

18, 22, 45};

7.    String[] buildings = {“All Saints”, “Breslin”, “duPont”,

“Fulford”, “Gailor”, “Guerry”, “McClurg”, “Spencer”,

“Woods”};

Place your order
(550 words)

Approximate price: $22

Calculate the price of your order

550 words
We'll send you the first draft for approval by September 11, 2018 at 10:52 AM
Total price:
$26
The price is based on these factors:
Academic level
Number of pages
Urgency
Basic features
  • Free title page and bibliography
  • Unlimited revisions
  • Plagiarism-free guarantee
  • Money-back guarantee
  • 24/7 support
On-demand options
  • Writer’s samples
  • Part-by-part delivery
  • Overnight delivery
  • Copies of used sources
  • Expert Proofreading
Paper format
  • 275 words per page
  • 12 pt Arial/Times New Roman
  • Double line spacing
  • Any citation style (APA, MLA, Chicago/Turabian, Harvard)

Our guarantees

Delivering a high-quality product at a reasonable price is not enough anymore.
That’s why we have developed 5 beneficial guarantees that will make your experience with our service enjoyable, easy, and safe.

Money-back guarantee

You have to be 100% sure of the quality of your product to give a money-back guarantee. This describes us perfectly. Make sure that this guarantee is totally transparent.

Read more

Zero-plagiarism guarantee

Each paper is composed from scratch, according to your instructions. It is then checked by our plagiarism-detection software. There is no gap where plagiarism could squeeze in.

Read more

Free-revision policy

Thanks to our free revisions, there is no way for you to be unsatisfied. We will work on your paper until you are completely happy with the result.

Read more

Privacy policy

Your email is safe, as we store it according to international data protection rules. Your bank details are secure, as we use only reliable payment systems.

Read more

Fair-cooperation guarantee

By sending us your money, you buy the service we provide. Check out our terms and conditions if you prefer business talks to be laid out in official language.

Read more