EECS2030E Test 4

Tuesday October 25, 2016, 16:00-17:20
Lab 02
Version F

Instructions

There is one programming question and a set of short answer questions.

Instructions for submitting your programming question solution are given in the Programming question section. You may submit as many times as you want; your most recent submission will be the one recorded.

Instructions for submitting your answers to the short answer questions are given in the Short Answer Questions section. You may submit as many times as you want; your most recent submission will be the one recorded.

Programming question

Implement this API. A skeleton can be found here here. The JAR file for this test can be found here. The skeleton compiles, but the constructors are not implemented and most of the methods do not return the correct values; you should complete the constructors and methods to satisfy their APIs. The provided code does not provide you with any fields; you need to decide which fields the class requires.

For reference: the Java API is here.

Submit your program using the following command in a terminal (make sure you are in the directory containing your file Temperature.java):

submit 2030 test4F SmartPhone.java


package eecs2030.test4;

import java.util.Set;
import java.util.TreeSet;

/**
 * A class that represents a smart phone. Every smart is a telephone
 * that has a set of contacts. The smart phone and its set of contacts
 * forms a composition.
 * 
 * @author EECS2030 Fall 2016-17
 * 
 */
public class SmartPhone extends Telephone {

    private Set<Contact> contacts;

    /**
     * Initializes this smart phone given its telephone number.
     * 
     * @param number
     *            the telephone number of this phone
     * @throws IllegalArgumentException
     *             if number does not have 10 digits
     */
    public SmartPhone(long number) {
        super(number);
        this.contacts = new TreeSet<Contact>();
    }

    /**
     * Initializes this smart phone given its telephone number and a set of
     * contacts.
     * 
     * @param number
     *            the telephone number of this phone
     * @param contacts
     *            a set of contacts for this phone
     * @throws IllegalArgumentException
     *             if number does not have 10 digits
     */
    public SmartPhone(long number, Set<Contact> contacts) {
        super(number);
        this.contacts = new TreeSet<Contact>(contacts);
    }

    /**
     * Returns the set of contacts for this phone. The set of contacts for this
     * phone cannot be modified using the set returned by this method.
     * 
     * @return the set of contacts for this phone
     */
    public Set<Contact> getContacts() {
        return new TreeSet<Contact>(this.contacts);
    }

    /**
     * Add a new contact to the set of the contacts for this phone. 
     * If the argument contact is already in this phone's set of
     * contacts then the set of contacts is not modified.
     * 
     * @param contact
     *            a contact to add to this phone
     * @throws Exception
     *             if the phone already has the specified contact in its set of
     *             contacts
     */
    public void addContact(Contact contact) throws Exception {
        if (this.contacts.contains(contact)) {
            throw new Exception("already have this contact");
        }
        this.contacts.add(contact);
    }

    /**
     * Call a contact. Before the contact is called, the phone checks if it
     * already has the specified contact in its set of contacts. If the contact
     * is not in the set of contacts for this phone, then the contact is added
     * to the set of contacts.
     * 
     * @param contact
     *            a contact to call
     * @see eecs2030.test4.Telephone#call(eecs2030.test4.Contact)
     */
    @Override
    public void call(Contact contact) {
        if (!this.contacts.contains(contact)) {
            this.contacts.add(contact);
        }
        super.call(contact);
    }

    /**
     * Returns a string representation of this smart phone. The returned string
     * is the phone number (as returned by the Phone version of toString)
     * followed by the string ", contacts " followed by the set of contacts (as
     * returned by the Set version of toString()).
     * 
     * @return a string representation of this smart phone
     * @see eecs2030.test4.Telephone#toString()
     */
    @Override
    public String toString() {
        return super.toString() + ", contacts " + this.contacts;
    }
}

Short Answer Questions

Question 1

What is a possibly significant disadvantage of using composition over aggregation? (Be concise in your answer; try to use no more than one sentence.)

Composition requires memory and time to make defensive copies.


Question 2 (1 mark)

Suppose that a method has an int parameter named y. Choose the strongest precondition on the value of y from the following choices:

A. @pre. none
B. @pre. y > 0
C. @pre. y > 0 && y < 100
D. @pre. y != 0

C. @pre. y > 0 && y < 100


Question 3 (3 marks)

The method SmartPhone.addContact throws a checked exception. A student decides to override the method in a subclass so that it throws a RuntimeException instead:

/**
 * Add a new contact to the set of the contacts for this phone. 
 * If the argument contact is already in this phone's set of
 * contacts then the set of contacts is not modified.
 * 
 * @param contact
 *            a contact to add (or replace) to this phone
 * @throws RuntimeException
 *             if the phone already has the specified contact in its set of
 *             contacts
 */
@Override
public void addContact(Contact contact) {
    // implmentation not shown
}

Has the student done anything wrong by changing the type of the thrown exception? Explain why or why not (using one or two sentences).

No, the student has not done something wrong.

The exception thrown by the child class (RuntimeException) is substitutable for the exception thrown by the parent class (Exception or a child of Exception that is not an ancestor of RuntimeException).


Enter your answers into a text file named answers.txt. You can use the editor of your choice for this. You can also create a text file in Eclipse by selecting "New" and then "Untitled Text File" in the "File" menu. Make sure that you save your file. Also make sure that you indicate which question you are answering, e.g. Q1: answer to question 1 Q2: answer to question 2 Q3:...

Submit your answers using the following command in a terminal (make sure you are in the directory containing your file answers.txt):

submit 2030 test4F answers.txt