obviously in need of more java help :|

Status
Not open for further replies.

wafflehammer

Fully Optimized
Messages
2,502
Location
Logan, WV
Uhm k. I've been tasked with this(minus everything that has to do with merge sorting..they told us to leave that out):

Code:
Templated Array Utilities
ï‚· Write a class called ArrayUtil with the following signature
public class ArrayUtil<E extends Comparable>. Note that this
makes ArrayUtil a template class.
 ArrayUtil should have one member variable – a templated Array called a. The
declaration of a is the same as in the selectionSort example shown in class and
in your lab implementation of mergeSort.
ï‚· ArrayUtil should have the following methods that are written to work with objects
from any class
1. mergeSort
2. insertionSort
3. findMax – this method finds and returns the largest element in the array as
determined by the compartTo method
4. findMin – this method finds and returns the smallest element in the array as
determined by the compareTo method.
5. search – the signature for this method should be
int search(E toFind).
The method should return the index of the element toFind in the array if it
exists and ‐1 if toFind is not in the array.
6. contains – the signature for this method should be
boolean contains(E toFind)
It should return true if the element toFind is in the list and false otherwise.
Note that an array becomes permanently attached to an object of the class ArrayUtil. We can
perform any of the 6 operations above on this array. However, if we wish to sort/search
another array, we must declare a new instance of ArrayUtil.
ï‚· Test your class with a test program that creates two arrays. One should be an array of
Double, the second should be an array of String as shown below:
o {58.7, 67.3, 11.4, ‐9.0, 21.7, 99.4, 200.0}
o {“Donald”, “Daisy”, “Huey”, “Dewey”, “Louie”, “Mickey”, “Minnie”, “Mortimer”}
ï‚· Print each array in its original order (across the page!)
ï‚· For each array create an instance of ArrayUtil and
o Sort the first array by insetionSort
o Sort the second array by mergeSort
o Print the sorted arrays (across the page)
o Find and print the maximum and minimum value in each array


But I pretty much have no clue on how to do any of it really :|

Well okay..so I do

I know

Code:
public static void selectSort(int[] a)
    {
        for(int i=0; i< a.length -1; i++)
        {
            int minValue=a[i];
            int minIndex = i;
            
            for(int k=+1; k<a.length; k++)
            if(a[k]<a[minIndex])
            minIndex=k;
            
            int tmp=a[i];
            a[i]=a[minIndex];
            a[minIndex]=tmp;
        }
    }
    
    public static void insertionSort(int[] a)
    {
        for(int j=1; j<a.length; j++)
        {
            int i=j-1;
            int temp = a[j];
            while(i>=0 && temp < a[i])
            {
                a[i+1] = a[i];
                i--;
            }
            a[i+1]=temp;
        }
    }

Is the code to get select and insertion sorts to work

and I know

Code:
    public int mybinarySearch(E toFind)
    {
        int low = 0;
        int high = list.length-1;
        
        while(low <= high)
        {
            int mid = (low+high)/2;
            
            if(list[mid].compareTo(toFind) ==0)
            return mid;
            
            if(list[mid].compareTo(toFind) <0)
            low = mid+1;
            else
            high = mid-1;
        }
        return -1;
        
    }

Is the code for a template binary search. But it's also for an array of "list' or something like that..which I don't understand

But beyond that I have no clue how any of it goes together, where i declare stuff, how do I make the searches template searches, etc :| I can do this stuff separately. But when it comes to making a template class and doing it all together I just go blank :\

Any help/pointers/toss in the right direction is appreciated.

EDIT: and I know that ArrayUtil.selectSort(arraynameinhere); and ArrayUtil.insertionSort(arraynameinhere); are what I'd use in the test class to do the sorting. But still everything else is bleh...dunno when/where I'm declaring the array/variables...blah blah blah :( totally lost
 
You're going to have to be a bit more specific about what you need. You should have something that looks a bit like this right?

Code:
public class ArrayUtil<E extends Comparable> {
private E[] a = null; //you have been shown how to declare this, apparently

public E[] mergeSort() {
}

public E[] insertionSort() {
}

public E findMax() {
}

public E findMin() {
}

public int search(E toFind) {
}

public boolean contains(E toFind) {
return search(toFind) != -1;
}

}
 
Code:
public class ArrayUtilTest
{
    public static void main(String[] args)
    {
        
        Double[] a = {58.7 , 67.3 , 11.4 , -9.0 , 21.7 , 99.4 , 200.0};
        
        String[] b = {"Donald ","Daisy ","Huey ","Dewey ","Louie ","Mickey ","Minnie ","Mortimer"};
        
        // Prints array a
        for(int i = 0; i<a.length;i++)
        System.out.print(a[i]+" ");
       
        System.out.println();
        
        // Prints array b
        for(int i = 0; i<b.length;i++)
        System.out.print(b[i]);
        
        System.out.println();
        System.out.println();
        
        ArrayUtil<Double> array1 = new ArrayUtil<Double>(a);
        
        // Sorts array a
        array1.insertionSort();
       
        // Prints array a after sorting
        for(int i = 0; i<a.length;i++)
        System.out.print(a[i]+" ");
       
        System.out.println();
        
        ArrayUtil<String> array2 = new ArrayUtil<String>(b);
        
        // Sorts array b
        array2.insertionSort();

       
        // Prints array b after sorting
        for(int i = 0; i<b.length;i++)
        System.out.print(b[i]);
       
        System.out.println();
        System.out.println();
        
        System.out.println("The minimum of array 1 is: "+array1.findMinimum());
        System.out.println("The minimum of array 2 is: "+array2.findMinimum());
        
        System.out.println();
        
        System.out.println("The maximum of array 1 is: "+array1.findMaximum());
        System.out.println("The maximum of array 2 is: "+array2.findMaximum());
    }
}

Code:
public class ArrayUtil<E extends Comparable>
{   
    private E[] a;

    public ArrayUtil(E[] a)
    {
        this.a = a;
    }

    public void insertionSort()
    {
        for(int j=1; j<a.length; j++)
        {
            int i=j-1;
            E temp = a[j];
            while(i>=0 && temp.compareTo(a[i]) < 0)
            {
                a[i+1] = a[i];
                i--;
            }
            a[i+1]=temp;
        }
    }
    
    public int mybinarySearch(E toFind)
    {
        int low = 0;
        int high = a.length-1;
        
        while(low <= high)
        {
            int mid = (low+high)/2;
            
            if(a[mid].compareTo(toFind) == 0)
            return mid;
            
            if(a[mid].compareTo(toFind) < 0)
            low = mid+1;
            else
            high = mid-1;
        }
        return -1;
        
    }
    
    public E findMinimum()
    {
        E min = a[0];
        
        for(int i=1; i< a.length; i++)
        if(a[i].compareTo(min)<0)
        min=a[i];
        
        return min;
    }
    
    public E findMaximum()
    {
        E max = a[0];
        
        for(int i = 1; i< a.length; i++)
        if(a[i].compareTo(max)>0)
        max=a[i];
        
        return max;
    }
    
}


Me and some other people in the class teamed up and figured it out. Sorry about being so vague :\ I wasn't asking for anything specific anyway, just needed a kick in the right direction lol

thanks for the help/response though :D
 
well the min and max is in there, dunno how you missed it

and in the end they told us to leave out the rest because we didn't know how to do them. they thought it was the same but something about merge sorting is hard to do in a template array ..or something
 
I know it seems fussy and as a learning exercise it's fine (which is why I said "technically") but you were asked for findMin() and findMax() and provided findMaximum() and findMinimum().
Merge sort shouldn't be any more difficult than any other search, search(E toFind) is easy and contains(E toFind) is one line (I showed it to you in post 2). mybinarySearch(E toFind) will not work on unsorted data.
 
I really don't know the thing about the merge sort. All I know is our professor said he tried it out and couldn't figure it out..then he remember it had something to do with it's a lot different to do in a template array and we haven't learned enough to do it yet or something. And don't be picky over my min/max :p I actually used blah and bleh as member variables before on an assignment. I did change them though before I actually turned it in though..there was just so many I used them to keep from getting confused lol

And I know it won't work on unsorted data because of the way it works (divides up, blah blah blah..all that good stuff). I'll probably go back and toy with it once I get back to my dorm (spring break) but at this point I'm not to worried, I done enough to get full credit and I learned a pretty good lot (first time doing the templates)

Thanks again for the continuing input :D
 
Status
Not open for further replies.
Back
Top Bottom