EECS1030M Test 2

Wednesday January 28, 2015, 13:00-13:30
Lab 02
Version A


import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * This utility class contains some methods for lists of strings.
 * 
 * @author Franck van Breugel
 */
public class Test2A 
{
    private Test2A() {}

    /**
     * Returns the size of the given list.
     * 
     * @param list a list of strings.
     * @pre. list != null
     * @return the size of the given list.
     */
    public static int size(List<String> list)
    {
	return list.size();
    }
	
    /**
     * Returns the first element of the given list.
     * 
     * @param list a list of strings.
     * @return the first element of the given list.
     * @throws IllegalArgumentException if the list is null or has size zero.
     */
    public static String first(List<String> list) throws IllegalArgumentException
    {
	if (list == null || list.size() == 0)
	 {
	     throw new IllegalArgumentException();
	 }
	else
	{
	    return list.get(0);
	}
    }
	
    /**
     * Returns the length of a longest string in the given list.
     * 
     * @param list a list of strings.
     * @pre. list != null && list.size() > 0
     * @return the length of a longest string in the given list.
     */
    public static int lengthOfLongestString(List<String> list)
    {
	int length = 0;
	for (String element : list)
	{
	    length = Math.max(length, element.length());
	}
	return length;
    }
	
    /**
     * Returns the string of the given list with the most occurrences of
     * the same character.  If multiple strings have the most occurrences of
     * the same character, of those strings the one which is smallest in the
     * lexicographic order (as usesd for example in a dictionary) is returned.
     * For example, consider the following list.<br/>
     * ["aaa", "abab", "accacc", "beeee"]<br/>
     * The strings "accacc" and "beeee" both contain four occurrences of the 
     * same character.  Since "accacc" is smaller than "beeee" in the lexicographic
     * order, "accac" would be returned by this method.
     * 
     * @param list a list of strings.
     * @pre. list != null && list.size() > 0
     * @return the string of the given list with the most occurrences of
     * the same character.
     */
    public static String max(List<String> list)
    {
	int maxCount = 0;
	String maxWord = null;
	
	for (String word : list)
	{
	    Map<Character, Integer> characterCount = new HashMap<Character, Integer>();
	    for (int i = 0; i < word.length(); i++)
	    {
		char letter = word.charAt(i);
		int count;
		if (!characterCount.containsKey(letter))
		{
		    count = 0;
		}
		else
		{
		    count = characterCount.get(letter);
		}
		characterCount.put(letter, count + 1);
	    }
	    
	    int count = 0;
	    for (Character c : characterCount.keySet())
	    {
		if (characterCount.get(c) > count)
		{
		    count = characterCount.get(c);
		}
	    }
	    
	    if (count > maxCount ||
	        (count == maxCount && (maxWord == null || word.compareTo(maxWord) < 0)))
	    {
		maxWord = word;
		maxCount = count;
	    }
	}
	return maxWord;
    }
}

Other questions

Question 1

Non-static methods and constructors have an implicit parameter named this



Question 2

The class MyInteger has a single attribute named value of type int. Consider the following implementation of the compareTo method.

public int compareTo(MyInteger other)
{
   int comparison;
   if (this.value < other.value)
   {
      comparison = -1;
   }
   else
   {
      comparison = 1;
   }
   return comparison;
}
Which of the following properties does the above compareTo not satisfy?
For all x, y and z different from null,
A. x.compareTo(y) returns 0 if and only if y.compareTo(x) returns 0.
B. x.compareTo(y) returns a value smaller than 0 if and only if y.compareTo(x) returns a value greater than 0.
C. x.compareTo(y) throws an exception if and only if y.compareTo(x) throws an exception.
D. if x.compareTo(y) returns a value smaller than 0 and y.compareTo(z) returns a value smaller than 0 then x.compareTo(z) returns a value smaller than 0.


Question 3

Modify the above compareTo method so that it satisfies the above four properties.

public int compareTo(MyInteger other)
{
   int comparison;
   if (this.value < other.value)
   {
      comparison = -1;
   }
   else if (this.value == other.value)
   {
      comparison = 0;
   }
   else
   {
      comparison = 1;
   }
   return comparison;
}

// or

public int compareTo(MyInteger other)
{
   return this.value - other.value;
}