Wednesday, 13 October 2010

Mike's Awesome ArrayList Practice

I got most of the methods, especially the harder ones.

Here's my source code, hope it's good! :

import java.util.ArrayList;
import java.util.Random;


/**
* Demonstrates simple algorithms using ArrayLists
*
* @author (your name)
* @version (a version number or a date)
*/

public class ArrayListDemo
{
private ArrayList <Integer> myArrayList;

ArrayListDemo()
{
    myArrayList = new  ArrayList <Integer>();
}

/**
* postcondition: limit number of values are added to myArrayList. Each value is between 0-(limit-1)
*/

public void fillRandom(int limit)
{
     Random r = new Random();
     for(int i = 0; i<limit; i++)
        myArrayList.add(r.nextInt(limit));
}

/**
* postcondition: writes the ArrayList to console
*/

public void writeArrayList(ArrayList newArrayList)
{
     newArrayList = new ArrayList <Integer> ();
}

/**
* postcondition: one int is added to myArrayList
* note that the int value is "autoboxed" or wrapped into an Integer object to be stored in myArrayList
*/

public void addInt(int value)
{
     myArrayList.add(value);
}

/**
* postcondition: the value at position index is removed from myArrayList
*/
public void remove(int index)
{
     myArrayList.remove(index);
}

/**
* postcondition: returns the index of the first occurrence of target
* returns -1 if target not found
*/

public int sequentialSearch (int target)
{
      int i=0;
      while(i<myArrayList.size())
      {
          if(myArrayList.get(i) == target)
               return i;
          i++;
        }
      return -1;
}

/**
* returns the minimum value value in myArrayList
*/
public int minvalue ()
{
      int min = myArrayList.get(0);
      for(int i=0; i<myArrayList.size(); i++)
      {
          if(myArrayList.get(i)<min)
          min = myArrayList.get(i);
        }
     
      return min;
}

/**
* returns the sum of the values in myArrayList
*/
public int sum ()
{
    int sum = 0;
    for(int i=0; i<myArrayList.size(); i++)
    {
        sum=sum+myArrayList.get(i);
    }
    return sum;
}

/**
* returns the arithmetic mean
*/
public double mean ()
{
    int total = 0;
    for(int i=0; i<myArrayList.size(); i++)
    {
        total = total+myArrayList.get(i);
    }
    return total/myArrayList.size();
}

/**
* returns an array of Integers using the int values from myArray
*/
public Integer [] myIntegerArray ()
{
    Integer [] integerArray;
    integerArray = null; // Don't know the how to do this one, see above issue.
    return integerArray;
}

/**
* returns a String representation of the numbers in myArrayList
*/
public String numberString ()
{
    String str = "";
    for(int i=0; i<myArrayList.size(); i++)
    {
        str = str + myArrayList.get(i);
    }
    return str;
}

/**
* postcondition: returns true if target found
*
*/

public boolean binarySearch (int target)
{
    int i = 0;
    while(i<myArrayList.size())
    {
        if(myArrayList.get(i) == target)
           return true;
        i++;
    }
    return false;
}

/**
* sorts myArrayList in ascending order using insertion sort algorithm
* starting at [0] find smallest element, exchange with element at [0]
* starting at [1] find smallest element, exchange with element at [1]
* continue until sorted
*/

public void insertionSort()
{
    int min = myArrayList.get(0);
    for(int z = 0; z < myArrayList.size(); z++)
    {
    for(int i = 0; i < myArrayList.size(); i++)
    {
        if(myArrayList.get(i)<min)
           min = myArrayList.get(i);
    }
    min = myArrayList.set(z, min); //HOLY CRAP I CANNOT BELIEVE I GOT THIS! :)
    z++;
}
}

/**
* sorts myArrayList in ascending order using selection sort algorithm
* starting at [1] compare with [0] if not in order exchange
* starting at [2] compare with [1] if not in order exchange and keep moving
* until in correct position
*/

public void selectionSort()
{
    int min = myArrayList.get(0);
    for(int z = 0; z < myArrayList.size(); z++)
    {
    for(int i = 1; i < myArrayList.size(); i++)
    {
        if(myArrayList.get(i)<myArrayList.get(i-1))
        myArrayList.set(i-1, myArrayList.get(i));
        i++;
    }
    z++;
} // AND AGAIN!
}

/**
* returns the median value in myArrayList
*/
public int median ()
{
    int median = 0;
    if(myArrayList.size()/2 == 0)
        median = median + (myArrayList.get(myArrayList.size()/2 -1) + myArrayList.get(myArrayList.size()/2+1)/2);
    else median = median + myArrayList.get(myArrayList.size()/2 +1);
    return median;  // I think this is it, but when checking, the median didn't come out correct... I'm not sure of the issue.
}

/**
* returns the mode in myArrayList
*/
public int mode()
{
    int mode = 0;
    int most = 0;
    for(int z = 0; z<myArrayList.size(); z++)
    {
        for(int i = 0; i<myArrayList.size(); i++)
        {
            if(myArrayList.get(i) == myArrayList.get(z));
            mode = mode + 1;
            i++;
        }
        if(mode > most)
        most = mode;
        mode = 0;
        z++;
    }
    return most;
}
}
// I feel so much better now that I got these last few! Never thought we'd already use 2 dimensional loops though.

No comments:

Post a Comment