EECS1030M Test 2

Wednesday January 28, 2015, 16:00-17:30
Lab 01
Version F


import java.util.HashSet;
import java.util.Set;

/**
 * This utility class contains some methods for (sets of) integers.
 * 
 * @author Franck van Breugel
 */
public class Test2F 
{
    private Test2F() {}
    
    /**
     * Tests whether the given integer is zero.
     * 
     * @param number an integer.
     * @return true if number is zero, false otherwise.
     */
    public static boolean zero(int number)
    {
	return number == 0;
    }
    
    /**
     * Returns the string "empty" if the given set is empty,
     * and returns the string "nonempty" otherwise.
     * 
     *  @param set a set of integers.
     *  @pre. set != null
     *  @return "empty" if the given set is empty, "nonempty" otherwise.
     */
    public static String empty(Set set)
    {
	String result;
	if (set.isEmpty())
	{
	    result = "empty";
	}
	else
	{
	    result = "nonempty";
	}
	return result;
    }
    
    /**
     * Tests whether the given set of integers contains an even integer.
     * For example, for the set { 1, 3, 5, 7 } false is returned and
     * for the set { 1, 2, 3 } true is returned.
     * 
     * @param set a set of integers.
     * @return true if the set contains an even integer, false otherwise.
     */
    public static boolean containsEven(Set set)
    {
	boolean containsEven = false;
	for (Integer element : set)
	{
	    containsEven = containsEven || element % 2 == 0;
	}
	return containsEven;
    }
    
    /**
     * Returns the sum of the elements of the symmetric difference of the
     * two given sets.  An element belongs to the symmetric difference if
     * it is contained in one of the two sets, but not in the other.
     * For example, for the sets
* { 1, 2, 5, 8 } and {1, 3, 8, 9, 10}
* 29 is returned, since 2 + 3 + 5 + 9 + 10 = 29. * * @param first a set of integers. * @pre. first != null * @param second a set of integers. * @pre. second != null * @return the sum of the elements of the symmetric difference of the * two given sets. */ public static int sumOfSymmetricDifference(Set first, Set second) { Set symmetricDifference = new HashSet(); for (Integer element : first) { if (!second.contains(element)) { symmetricDifference.add(element); } } for (Integer element : second) { if (!first.contains(element)) { symmetricDifference.add(element); } } int sum = 0; for (Integer element : symmetricDifference) { sum += element; } return sum; } }

Other questions

Question 1

The class MyInteger implements the interface Comparable. The parameter of the compareTo method is of type MyInteger



Question 2

The class Circle contains the private attribute radius of type double and the public method getRadius. The class Circle also contains the public method getCircumference which returns the circumference of the circle (which is equal to 2 × π × the radius). The class Circle does not contain a private attribute circumference because
A. the attribute circumference would be redundant.
B. the clients can compute the circumference themselves.
C. the attribute circumference should be public.
D. of a reason not mentioned above.



Question 3

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

MyInteger one = new MyInteger(1);
MyInteger two = new MyInteger(2);
Class x = one.getClass();
Class y = two.getClass();
When the first three lines of code have been executed, memory can be depicted as follows.
    |                  |
100 | main invocation  |
    | 300              | one
    | 400              | two
    | 500              | x
    |                  | y
    |                  |
200 | MyInteger class  |
    |                  |
300 | MyInteger object |
    | 1                | value
    |                  |
400 | MyInteger object |
    | 2                | value
    |                  |
500 | Class class      |
    |                  |
600 | Class object     |
    |                  |
Draw how memory can be depicted after the execution of all four lines. You only have to draw those parts that change.
    |                  |
100 | main invocation  |
    | 300              | one
    | 400              | two
    | 600              | x
    | 600              | y
    |                  |

Note: There was a mistake in this question. The variable x should refer to the Class object at address 600 (not 500). Answers in which the variable y refers to either 500 or 600 will both be considered correct.