EECS2030E Test 4

Monday November 21, 2016, 14:00-15:20
Lab 01
Version D

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 jar file containing some required resources can be found here. A skeleton can be found here here. The skeleton does not compile, the constructors are not implemented and 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 SmartPhone2.java):

submit 2030 test4D SmartPhone2.java


package eecs2030.test4;

import java.util.List;
import java.util.NoSuchElementException;
import java.util.ArrayList;

/**
 * A class that represents a smart phone. Every smart is a telephone that has a
 * call log. A call log is a list of contacts that have been called using this
 * smart phone. The order of contacts in the call log are from the  
 * The smart phone and its call log form a composition.
 * 
 * @author EECS2030 Fall 2016-17
 * 
 */
public class SmartPhone2 extends Telephone {

    private List<Contact> callLog;

    /**
     * Initializes this smart phone given its telephone number.
     * The call log for this smart phone is initialized as an empty list.
     * 
     * 
     * @param number
     *            the telephone number of this phone
     * @throws IllegalArgumentException
     *             if number does not have 10 digits
     */
    public SmartPhone2(long number) {
        super(number);
        this.callLog = new ArrayList<Contact>();
    }

    /**
     * Initializes this smart phone by copying the telephone number and call log
     * of another smart phone.
     * 
     * @param other
     *            the smart phone to copy
     */
    public SmartPhone2(SmartPhone2 other) {
        super(other.getNumber());
        this.callLog = new ArrayList<Contact>(other.callLog);
    }

    /**
     * Returns the call log for this phone. The call log for this phone cannot
     * be modified using the call log returned by this method.
     * 
     * @return the call log for this phone
     */
    public List<Contact> getCallLog() {
        return new ArrayList<Contact>(this.callLog);
    }

    /**
     * Call a contact. After the contact is called, the contact is added to the
     * call log.
     * 
     * @param contact
     *            a contact to call
     * @see eecs2030.test4.Telephone#call(eecs2030.test4.Contact)
     */
    @Override
    public void call(Contact contact) {
        super.call(contact);
        this.callLog.add(contact);
    }

    /**
     * Returns a new copy of the last contact that was called using this smart
     * phone. If the call log for this is empty, then a NoSuchElementException
     * is thrown.
     * 
     * @return a new copy of the last contact that was called using this smart
     *         phone
     * @throws NoSuchElementException
     *             if the call log for this smart phone is empty
     */
    public Contact lastCalled() {
        if (this.callLog.isEmpty()) {
            throw new NoSuchElementException();
        }
        return new Contact(this.callLog.get(this.callLog.size() - 1));
    }

    /**
     * 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 ", call log " followed by the call log (as
     * returned by the List version of toString()).
     * 
     * @return a string representation of this smart phone
     * @see eecs2030.test4.Telephone#toString()
     */
    @Override
    public String toString() {
        return super.toString() + ", call log " + this.callLog;
    }

}

Short Answer Questions

Question 1

Consider the following class:

// A Circle and its center Point form a composition.
public class Circle {
  private Point center;
  private double radius;
  
  public Center getCenter() {
    return this.center;
  }
  
  // rest of class implementation not shown
}

What term is used to describe the fact that the method getCenter returns a reference to the circle's center point (instead of returning a copy)?

Privacy leak


Question 2

Suppose that a parent class method has a postcondition and a child class overrides the method. What should be true about the postcondition of the child class version of the method?

A. it does not depend on the postcondition of the parent class version
B. it must be identical to the postcondition of the parent class version
C. it can be weaker than the postcondition of the parent class version
D. it must be substitutable for the postcondition of the parent class version

D. it must be substitutable for the postcondition of the parent class version


Question 3

Recall the API for the method SmartPhone2.lastCalled:

/**
  * Returns a new copy of the last contact that was called using this smart
  * phone. If the call log for this is empty, then a NoSuchElementException
  * is thrown.
  * 
  * @return a new copy of the last contact that was called using this smart
  *         phone
  * @throws NoSuchElementException
  *             if the call log for this smart phone is empty
  */
public Contact lastCalled() { // implementation not shown }    

Given that SmartPhone2 is a child class of Telephone is the method SmartPhone2.lastCalled allowed to throw a NoSuchElementException? Use one or two sentences to explain your answer.

Yes, it can throw a NoSuchElementException because lastCalled() is a new method in SmartPhone2 (not an overridden method inherited from Telephone).


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 test4D answers.txt