0

we are currently learning Generics in class but I am still confused. I am currently doing an assignment and what my teacher wants me to do is to use the binary search algorithm to search for a circle with a certain radius. I believe I have everything set up but I am confused about how I can pass an array of objects to a generic method. If I can't how else can I get access an array of objects using generics. I hope I am asking this correctly

Here is what I have of course it's not done.

public class Question1
{
public static void main(String[] args)
{
    Circle[] b = {new Circle(1),new Circle(2),new Circle(3),new Circle(4),new Circle(5)};
    System.out.println(b[0].getR());
    run(b,3,0, b.length-1);

}

/**
 *
 * @param <T>
 * @param b
 * @param key
 * @param low
 * @param high
 */
public static <T> Object run(T[] b , T key, int low, int high)
{
    int mid = ((low + high)/2);
    if(high < low)
    {
        System.out.print(-1);
    }
    if(key == b[mid])
    {
        System.out.println(b[mid]);
    }
    if(key < b[mid])
    {
        return run(b,key,low,mid-1);
    }
    else
    {
        return run(b,key,mid+1,high);
    }            
}
}

Thanks, fish

5
  • Do you have any example so far to work with? Commented Apr 7, 2018 at 23:52
  • Show us an example Commented Apr 7, 2018 at 23:56
  • Binary search implies that the list to be searched is sorted based on some criterion. What more can you tell us about this assignment? The information you've given us does not readily admit an answer. Commented Apr 8, 2018 at 0:11
  • yes, we are to assume that the array has been sorted. I posted my code its not finished of course. Commented Apr 8, 2018 at 0:14
  • Oh yeah, I have created a circle class. I am trying to call b[mid].getR() but I can't Commented Apr 8, 2018 at 0:16

2 Answers 2

1

Generics are a way of generalizing code to a variety of types while maintaining type safety. The usual way of introducing someone to generics is to compare the nongeneric List interface to the generic List<T> interface. A raw List can contain any object type and, therefore, is not typesafe. A raw list can contain both your favorite Coin and Stamp collection at the same time, which can cause difficult errors when you went to retrieve a Coin but got a Stamp instance instead.

A List<Coin> can contain only contain Coin instances. The compiler will not allow a Stamp to be inserted into the list. As long as the program compiles without errors OR warnings, the compiler guarantees that every time you go to get a Coin out of the list, you will only get Coins.

my teacher wants me to do is to use the binary search algorithm to search for a circle with a certain radius.

This assignment does not require a generic method so I'm not sure why you're being asked to write one, unless there are details about the assignment you haven't disclosed.

Your current solution contains one glaring problem: you cannot use the == operator to test for value equality among objects. In object comparisons, the == operator tests only for identity. This will print false:

System.out.println("my test" == new String("my test"));

The above has two objects with separate identities: the string literal, and the newly created String instance. They both have different identities (even though they have the same value) so the expression evaluates to false. For value comparisons among objects, you need to write an equals() and a hashCode() method. This evaluates to true: "my test".equals(new String("my test"));

Notice also that they expression with objects (key < b[mid]) is illegal because the < operator is not valid for object types.

Sign up to request clarification or add additional context in comments.

4 Comments

Hey thanks for the reply I think I am also getting confused about what he wants and I thought a method would be better. Here is the question. "Write Java code for the Binary search algorithm. Make sure to use generics. Now create an appropriate class to represent a circle. show that you can use your generic Binary search to search for a circle in an array of circles based on the radii. "
There's some big problems with the code you have. Have you studied the Comparable class yet?
yes, we have. I was thinking I have forgotten something.
A method which performs a binary search on an array of a single type would be a good target for generics. If you could pass any sortable type to such a method, how would the method know what criteria to sort and search on if the array can be any one of a variety of types? This is a case in which a strategy interface would be useful. The strategy interface most commonly used for this purpose is Comparable, as mentioned.
0

If I understand correctly, the type of the second parameter needs to be a Circle not the integer 3 that you have currently.

public static void main(String[] args)
{
    Circle[] b = {new Circle(1),new Circle(2),new Circle(3),new Circle(4),new Circle(5)};
    System.out.println(b[0].getR());
    run(b, new Circle(3), 0, b.length-1);  // <-- change this line

}

2 Comments

when I change it to that it doesn't do anything different.
What I gave you does pass an array correctly. However, there are other errors in your code. Could you please update your original question if you are asking about those other errors?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.