0
public class Project9
{
    public static void main(String args[])
    {
        InventoryItem[] items = new InventoryItem[3];

        items[0] = new InventoryItem("Coffee", 1);
        items[1] = new InventoryItem("Pencils", 2);
        items[2] = new InventoryItem("Notebooks", 3);

        System.out.println("Before sorting");
        System.out.println(items[0]);
        System.out.println(items[1]);
        System.out.println(items[2]);

        InventoryItem.sort(items, items.length);

        System.out.println("After sorting");
        System.out.println(items[0]);
        System.out.println(items[1]);
        System.out.println(items[2]);
    }
}

class InventoryItem implements Comparable<InventoryItem>
{
    private String name;
    private int uniqueItemID;

    public InventoryItem()
    {
        name = " ";
        uniqueItemID = 0;
    }

    public InventoryItem(String newName, int newItemID)
    {
        name = newName;
        uniqueItemID = newItemID;
    }

    public InventoryItem(InventoryItem i)
    {
        name = i.name;
        uniqueItemID = i.uniqueItemID;
    }

    public void setName(String newName)
    {
        name = newName;
    }

    public void setItemID(int newItemID)
    {
        uniqueItemID = newItemID;
    }

    public int getItemID()
    {
        return uniqueItemID;
    }

    public String getName()
    {
        return name;
    }

    public int compareTo(InventoryItem i)
    {
        int anotherUniqueID = ((InventoryItem) i).getItemID();
        return (this.uniqueItemID - anotherUniqueID);
    }

    public static void sort(Comparable[] a, int numberUsed)
    {
        int index, indexOfNextSmallest;

        for(index = 0; index < numberUsed - 1; index++)
        {
            indexOfNextSmallest = indexOfSmallest(index, a, numberUsed);
            interchange(index, indexOfNextSmallest, a);
        }
    }

    private static int indexOfSmallest(int startIndex, Comparable[] a, int numberUsed)
    {
        Comparable min = a[startIndex];
        int indexOfMin = startIndex;
        int index;

        for(index = startIndex + 1; index < numberUsed; index++)
        {
            if(a[index].compareTo(min) < 0)
            {
                min = a[index];
                indexOfMin = index;
            }
        }
        return indexOfMin;
    }

    private static void interchange(int i, int j, Comparable[] a)
    {
        Comparable temp;
        temp = a[i];
        a[i] = a[j];
        a[j] = temp;
    }
}

When I compile my code it throws the error message "Project9.java uses unchecked or unsafe operations."

When I recompile with Xlint:unchecked like it recommends, I get this message "warning: [unchecked] unchecked call to compareTo(T) as a member of the raw type Comparable

        if(a[index].compareTo(min) < 0)
                             ^

where T is a type-variable:

T extends Object declared in interface Comparable

What do I need to do/understand so I can fix this? Many thanks!

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • Please don't paste a link to code, but rather paste the *code itself*. I've taken the liberty of doing this for you, but next time, we appreciate it if you take the effort and do this yourself. – Hovercraft Full Of Eels Jul 30 '13 at 20:40
  • Note that what you're seeing is a warning, not an error. Your code will compile, but is at *some* risk since you're not using generics. For now, if you haven't yet learned generics, you can ignore the warning. Either that or go to the generics tutorial and study them. – Hovercraft Full Of Eels Jul 30 '13 at 20:43
  • How would I go about using generics? I am in an entry level OOP class and don't recognize that term. – user2635737 Jul 30 '13 at 20:45
  • Google the tutorial. Your a variable is a Comparable, not a `Comparable` type. Consider declaring it as the latter. – Hovercraft Full Of Eels Jul 30 '13 at 20:45

2 Answers2

0

Maybe add cast to InventoryItem?

private static int indexOfSmallest(int startIndex, Comparable<InventoryItem>[] a, int numberUsed)
{
    Comparable<InventoryItem> min = a[startIndex];
    int indexOfMin = startIndex;
    int index;

    for(index = startIndex + 1; index < numberUsed; index++)
    {
        if(a[index].compareTo((InventoryItem) min) < 0)
        {
            min = a[index];
            indexOfMin = index;
        }
    }
    return indexOfMin;
}
MGorgon
  • 2,547
  • 23
  • 41
0

Don't use Comparable as your method parameter but rather InventoryItem.

For example:

private static int indexOfSmallest(int startIndex, InventoryItem[] a, int numberUsed)
{
    InventoryItem min = a[startIndex];
    int indexOfMin = startIndex;
    int index;

    for(index = startIndex + 1; index < numberUsed; index++)
    {
        if(a[index].compareTo(min) < 0)
        {
            min = a[index];
            indexOfMin = index;
        }
    }
    return indexOfMin;
}

private static void interchange(int i, int j, InventoryItem[] a)
{
    InventoryItem temp;
    temp = a[i];
    a[i] = a[j];
    a[j] = temp;
}

This way the compiler can do compile-time type checking to make sure that your parameter is the correct type.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373