EECS1030Z Test 3

Thursday February 26, 2015, 14:30-15:45
Lab 01
Version H


Programming question

Implement the USBPort class having the API shown here. A skeleton can be found here.

package test3;
import java.util.HashMap;
import java.util.Map;


/**
 * A class that represents USB ports on a computer. Each USB port
 * has a unique integer port number, and it is guaranteed that there
 * is only one USBPort for each port number. Each USB port is either connected
 * or not connected to an external device of some kind (such as a
 * USB key, for example).
 * 
 * @author EECS1030_2014_14W
 *
 */
public class USBPort {
  private static Map<Integer, USBPort> instances = new HashMap<Integer, USBPort>();
  
  private int portNumber;
  private boolean isConnected;
  
  /**
   * Create a new USB port having the given port number. The USB port
   * is not allowed to be connected to an external device when it is
   * created.
   * 
   * @param portNumber the port number of the USB port
   * @pre. portNumber >= 0
   * @pre. the USB port is not connected to an external device
   */
  private USBPort(int portNumber) {
    this.portNumber = portNumber;
    this.isConnected = false;
  }
  
  /**
   * Return the port number of this USB port.
   * 
   * @return the port number of this USB port
   */
  public int getPortNumber() {
    return this.portNumber;
  }
  
  /**
   * Connect the port to an external device. The state of the port
   * does not change if the port is already connected to an external
   * device. 
   */
  public void connect() {
    this.isConnected = true;
  }
  
  /**
   * Disconnect the port from the external device. The state of the port
   * does not change if the port is already disconnected. 
   */
  public void disconnect() {
    this.isConnected = false;
  }
  
  /**
   * Returns <code>true</code> if the port is connected to an external
   * device, and <code>false</code> otherwise.
   * 
   * @return <code>true</code> if the port is connected to an external
   * device, and <code>false</code> otherwise.
   */
  public boolean hasConnection() {
    return this.isConnected;
  }
  
  /**
   * Get the USB port with the given port number. The port number must
   * be an integer value greater than zero. A new USB port is created if
   * a USB port with the given port number does not exist. If a new USB
   * port is created then it is created assuming that the port is not
   * connected to an external device.
   * 
   * @param portNumber the port number of the desired USB port
   * @return a USB port with the desired port number
   * @throws IllegalArgumentException if the port number is less
   * than or equal to zero
   */
  public static USBPort getPort(int portNumber) {
    if (portNumber <= 0) {
      throw new IllegalArgumentException();
    }
    USBPort p = USBPort.instances.get(portNumber);
    if (p == null) {
      p = new USBPort(portNumber);
      USBPort.instances.put(portNumber, p);
    }
    return p;
  }
  
  /**
   * Returns the number of USB ports that have been created.
   * 
   * @return the number of USB ports that have been created
   */
  public static int getNumberOfPorts() {
    return USBPort.instances.size();
  }
  
  

}

Other questions

Question 1

What is an obligatory method?

A. a method that can be invoked using any object reference
B. a method that the client must invoke, such as a constructor
C. a method that the implementer must override
D. a method that the implementer must not override

Question 2

public static void main(String[] args) {
  USBPort u = USBPort.getPort(1);
  USBPort v = USBPort.getPort(2);
  USBPort w = USBPort.getPort(3);
  USBPort x = USBPort.getPort(1);
  
  // remainder of main not shown
}

How many USBPort objects are in memory?

A. 1
B. 2
C. 3 (1 for port one, 1 for port two, and 1 for port 3
D. 4

Question 3

What is the name of the relationship between the two classes shown in the following UML diagram:



Type your answer here: aggregation

Question 4

The USBPort method named connect seemingly changes the state of a USBPort object. What name is given to a method that changes the state of an object?

Type your answer here: mutator

Question 5

Reconsider Question 4. Under what condition can a multiton class have a public method that changes the state of an instance?


If some part of the state is not needed to retrieve an instance then that part of the state can be safely mutated. For example, USBPort.getInstance(int) does not use the connectivity of the port to retrieve an instance; therefore, the port connectivity can be safely mutated.


Question 6

Suppose that two USB ports are equal if their port numbers are equal (i.e., whether or not the port is connected is not important for equals). Do we need to override equals for USBPort? Explain why or why not.


No. USBPort is a multiton and the class ensures that there is exactly one USBPort instance for each unique port number. This means that a USBPort can only be equal to itself, so the Object version of equals is sufficient.