Thursday, December 14, 2006

finalMethodsReview.java



      
public static int randomUpTo(int n) // generate random num up to n
{
 Random generator = new Random();
 return generator.nextInt(n);
}
public static int randomBetween(int first, int second) // inclusive
{
 Random generator = new Random();
 return generator.nextInt(second - first + 1) + first;
}
public static void printLettersOnSeparateLines(String s)
{
 for(int i = ; i < s.length(); i++)
  System.out.println(s.charAt(i));
} 
public static boolean longerThanSecond(String first, String second)
{
 return first.length() > second.length();
public static boolean comesBeforeSecond(String first, String second)
{
 return first.compareTo(second) < 0;
}
public static int countVowels(String s)
{ 
 char ch;
 int count = 0;
 for(int i = ; i < s.length(); i++)
  {
   ch = s.charAt(i);
   if(ch == 'a' || ch == 'e' || ch == 'i' || ch == '0' || ch == 'u')
    count++; 
  }
 return count;
}
public static String replaceLetterWithAsterisk(String s, char letter)
{
 for(int i = 0; i < s.length(); i++)
  if(s.charAt(i) == letter) s.charAt(i) = '*';
 return s;
}
public static boolean isAdverb(String s) // check if s ends in "ly"
{
 String ending = new String();
 ending = s.substring(s.length() - 2);
 return ending.equals("ly");
}
public static void print(int[] list)
{
 for(int i = 0; i < list.length; i++)
  System.out.println(list[i]);
}
public static void printBackwards(int[] list)
{
 for(int i = list.length - 1; i >= 0; i--)
  System.out.println(list[i]);
}  
public static int sum(int[] list)
{
 int sum = 0;
 for(int i = 0; i < list.length; i++)
  sum += list[i];
 return sum;
}
public static double average(double list[])
{
 double sum = 0;
 for(int i = 0; i < list.length; i++)
  sum += list[i];
 return sum / list.length;
}
public static int countEvens(int[] list)
{
 int count = 0;
 for(int i = 0; i < list.length; i++)
  if(list[i] % 2 == 0) count++;
 return count;
}
public static void reverse(int[] list)
{
 int[] newList = new int[list.length];
 int k = 0;
 for(int i = list.length - 1; i >= 0; i--)
  newList[k++] = list[i];
 list = newList;
} 
public static void swapPairs(int [] list) // assume even sized list
{
 int temp;
 for(int i = 0; i <= list.length - 2; i += 2)
  {
   temp = list[i];
   list[i] = list[i+1];
   list[i+1] = temp;
  }
}
public static boolean allValuesUnique(int[] list)
{
 for(int i = 0; i < list.length; i++)
  for(int j = 0; j < list.length; j++)
   if(list[i] == list[j] && i != j) return false;
 return true;
}
public static void printTable(int[][] table)
{
 for(int row = 0; row < table.length; row++)
  for(int col = 0; col < table[row].length; row++)
   System.out.println(table[row][col]);
}
public static void printRow(int[][] table, int row)
{
 for(int col = 0; col < table.length; col++)
   System.out.println(table[row][col]);
}  
public static void printCol(int[][] table, int col)
{ 
 for(int row = 0; row < table[row].length; row++)
   System.out.println(table[row][col]);
}  


public static int search(int[] list, int n) // returns the index of n else returns -1
{
 for(int i = 0; i < list.length; i++)
  if(list[i] == n) return i;
 return -1;
}
public static boolean isInOrder(int[] list)
{
 for(int i = 0; i < list.length - 1; i++)
  if(list[i] > list[i+1])
   return false;
 return true;
}
public static void printArrayList(ArrayList list) // assume list contains String objects
{
 for(int i = 0; i < list.size(); i++)
  System.out.println(list.get(i));
}
public static int myIndexOf(ArrayList list, String s)// returns the indexOf s
{
 return list.indexOf(s);
}
public static void setAll(ArrayList list, String s) // sets all of the elements in list to s
{
 for(int i = 0; i < list.size(); i++)
  list.set(i,s); 
}
public static void removeFirstAndLast(ArrayList list) // removes first and last elements of list
{
 list.remove(0);
 list.remove(list.size()-1);
}