-1

This is generic array sort code. I have no idea how to use setComparator that are in BubbleSort class and ArraySort. I keep getting an error about a "raw type" ...

Here is the full code:

package sortNumbers;

import java.util.Scanner;
import java.util.Random;

class sortNumbers
{
    public static void main(String[] args)
    {
        int arrayLength = 4;
        String chooseArray = "BubblesSort";
        boolean asORds = true;

        Integer[] arr = new Integer[4];

        Random randomGen = new Random() ;

        for(int i = 0; i<arrayLength; i++)
        {
            arr[i] = randomGen.nextInt(101);
        }

        intComparator ic = new intComparator(asORds);
        BubbleSort bs = new BubbleSort(arr);
        //bs.setComparator(ic);
    }
}

interface Comparator<T>
{
    public int compare(T a, T b);
}

class intComparator<T> implements Comparator<T>
{
    boolean ascend;

    public intComparator(boolean ascend) {
        this.ascend = ascend;
    }

    public int compare(T a, T b)
    {
        int aA = (Integer)a;
        int bB = (Integer)b;
        //int temp;
        int changePosition = 1;

        if(this.ascend == false)
            changePosition = -1;            

        /*if(this.ascend == false)
        {
            temp = aA;
            aA = bB;
            bB = temp;
        }*/

        if(aA == bB)
            return 0*changePosition;
        else if(aA < bB)
            return -1*changePosition;
        else
            return 1*changePosition;
    }
}

class strComparator<T> implements Comparator<T>
{
    protected boolean ascend;

    public strComparator(boolean ascend) {
        this.ascend = ascend;
    }

    public int compare(T a, T b)
    {
        String aA = (String)a;
        String bB = (String)b;
        //int temp;
        int changePosition = 1;

        if(this.ascend == false)
            changePosition = -1;            

        /*if(this.ascend == false)
        {
            temp = aA;
            aA = bB;
            bB = temp;
        }*/

        if(aA.compareTo(bB) == 0)
            return 0*changePosition;
        else if(aA.compareTo(bB) == -1)
            return -1*changePosition;
        else
            return 1*changePosition;
    }
}

class floatComparator<T> implements Comparator<T>
{
    protected boolean ascend;
    public floatComparator(boolean ascend)
    {
        this.ascend = ascend;
    }

    public int compare(T a, T b)
    {    
        float aA = (Float)a;
        float bB = (Float)b;
        //int temp;
        int changePosition = 1;

        if(this.ascend == false)
            changePosition = -1;            

        /*if(this.ascend == false)
        {
            temp = aA;
            aA = bB;
            bB = temp;
        }*/

        if(aA == bB)
            return 0*changePosition;
        else if(aA < bB)
            return -1*changePosition;
        else
            return 1*changePosition;
    }
}
abstract class ArraySort<T>
{
    protected T[] inArray;
    public ArraySort(T[] inArray)
    {
        this.inArray = inArray;
    }

    abstract public void iSort(T[] inArray);
    abstract public T[] oSort(T[] inArray);

    public void setComparator(Comparator<T> comparator)
    {

    }
}

class BubbleSort<T> extends ArraySort<T>
{
    protected Comparator<T> forCompare;

    public BubbleSort(T[] inArray)
    {
        super(inArray);
    }

    public void iSort(T[] inArray)
    {
        T temp;
        for(int i = 0; i < inArray.length-1; i++)
        {
            for(int j=1; j<inArray.length-i; j++)
            {
                //int compared = compare(in)
                if(forCompare.compare(inArray[j-1],inArray[j]) < 0)
                    //    inArray[j-1] > inArray[j])
                {
                    temp=inArray[j-1];
                    inArray[j-1] = inArray[j];
                    inArray[j] = temp;
                }
            }
        }

        if(inArray.length <= 10)
        {
            for(int i = 0; i <inArray.length; i++)
            {
                System.out.print(inArray[i] + " ");
            }
            System.out.println("");
        }
    }

    public T[] oSort(T[] inArray)
    {
        T[] tempArray = (T[]) new Object[inArray.length];
        //is this right way to creat generic type array?
        for(int i = 0; i<tempArray.length; i++)
        {
            tempArray[i] = inArray[i];
        }
        T temp;



        for(int i = 0; i < tempArray.length-1; i++)
        {
            for(int j=1; j<tempArray.length-i; j++)
            {
                if(forCompare.compare(tempArray[j-1],tempArray[j]) < 0)
                {
                    temp=tempArray[j-1];
                    tempArray[j-1] = tempArray[j];
                    tempArray[j] = temp;
                }
            }
        }

        if(tempArray.length <= 10)
        {
            for(int i = 0; i <tempArray.length; i++)
            {
                System.out.print(tempArray[i] + " ");
            }
            System.out.println("");
        }

        return tempArray;
    }

    public void setComparator(Comparator<T> comparator) {
    // How should I use this...please some o
    }
}

In BubbleSort, oSort method, I am trying to create a new T array. Is this the way to do it?

T[] tempArray = (T[]) new Object[inArray.length]; 
  • Really, please respect naming conventions: class names should start with uppercase, fields and methods with lowercase. – bvdb Oct 29 '15 at 19:51

1 Answers1

1

You're implementing your Comparators wrong. For example, your intComparator should be declared as:

class intComparator implements Comparator<Integer>
{
    //...
    public int compare(Integer a, Integer b)
    {
        ...
    }
}

Instantiating generics in Java is kind of tricky. Find out why and what options you have here.

Read more about generic types here.

FYI, remember that class names start with Uppercase letters.

Community
  • 1
  • 1
AxiomaticNexus
  • 5,666
  • 2
  • 34
  • 56
  • Oh..!!!Okay. How about in ArraySort or BubbleSort class, there is setComparator method. I really dont know what to put inside of these methods... – HyoDong Kim Oct 29 '15 at 19:54
  • @HyoDongKim I suppose you want to set the `Comparator`. Assign it to an instance variable so that it can be used in the other methods of your `BubbleSort` and `ArraySort` classes. – AxiomaticNexus Oct 29 '15 at 20:03