Evil Pop Quizzes for CS-2

  1. visit/display all the values in an array in order ("traverse")
  2. visit/display all the values in an array in reverse order
  3. find the smallest value in an array (return smallest)
  4. find the largest value in an array (return largest)
  5. get the sum of all the values in an array (return sum)
  6. display all values greater than some threshold (filter) in an array
  7. find the location (index) for a value in an array (return slot)
  8. determine if a value is present in an array (return true/false)

Canonical Solutions - Arrays

Traverse Array

// diplay all the values in an array in order
public static void displayForward( int[] a )
{
	for ( int n : a )
		out.println(n);
}

Traverse backwards

// diplay all the values in an array in *reverse* order
public static void displayBackward( int[] a )
{
	for ( int i=a.length-1 ; i>=0; i-- )
		out.println( a[i] );
}

Find smallest

// find the smallest value in an array (return smallest)
public static int findSmallest( int[] arr )
{
	int min = Integer.MAX_VALUE;
	for ( int v : arr )
		if ( v < min )
			min = v;
	return min;
}

Find largest

// find the largest value in an array (return largest)
public static int findLargest( int[] a )
{
	int max = Integer.MIN_VALUE;
	for ( int q : a )
		if ( q > max )
			max = q;
	return max;
}

Sum Values

// get the sum of all the values in an array (return sum)
public static int getSum( int[] values ) // we're up all night to...
{
	int totes = 0;
	for ( int n : values )
		totes += n;
	return totes;
}

Filter Values

// display all values greater than some threshold (filter) in an array
public static void displaySome( int[] thingys, int limit )
{
	for ( int t : thingys )
		if ( t > limit )
			out.println(t);
}

Locate value

// find the location (index) for a value in an array (return slot)
public static int locate( int[] arr, int toFind )
{
	for ( int i=0 ; i<arr.length; i++ )
		if ( arr[i] == toFind )
			return i;
	return -1;
}

Is value present?

// determine if a value is present in an array (return true/false)
public static boolean contains( int[] arr, int toFind )
{
	for ( int k : arr )
		if ( k == toFind )
			return true;
	return false;
}

Canonical Solutions - ArrayLists

Traverse ArrayList

// diplay all the values in an ArrayList in order
public static void displayForward( ArrayList<String> names )
{
	for ( String s : names )
		out.println(s);
}

Reverse Traversal

// diplay all the values in an ArrayList in *reverse* order
public static void displayBackward( ArrayList<String> winners )
{
	for ( int i=winners.size()-1 ; i>=0; i-- )
		out.println( winners.get(i) );
}

Find smallest or largest

// find the smallest value in an ArrayList (return smallest)
public static String findSmallest( List<String> list )
{
	String min = "";
	for ( String v : list )
		if ( v.compareTo(min) < 0 )
			min = v;
	return min;
}

Sum Values

// get the sum of all the values in an ArrayList (return sum)
public static int getTotal( ArrayList<Double> sales )
{
	int sum = 0;
	for ( double s : sales )
		sum += s;
	return sum;
}

Filter Values

// display all values greater than some threshold (filter) in an ArrayList
public static void displaySome( ArrayList<Integer> stuff, int limit )
{
	for ( int t : stuff )
		if ( t > limit )
			out.println(t);
}

Locate value

// find the location (index) for a value in an ArrayList (return slot)
public static int locate( ArrayList<String> nicks, String wanted )
{
	for ( int i=0 ; i<nicks.size(); i++ )
		if ( nick.get(i).equals(wanted) )
			return i;
	return -1;
}

Is value present?

// determine if a value is present in an ArrayList (return true/false)
public static boolean contains( ArrayList<Integer> arr, int toFind )
{
	for ( int k : arr )
		if ( k == toFind )
			return true;
	return false;
}



©2013–2015 Graham Mitchell

This assignment is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 United States License.
Creative Commons License