EECS2030E Test 4

Tuesday November 15, 2016, 16:00-17:20
Lab 02
Version E

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 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 any fields; you must decide what fields are required by your implementation.

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 test4E ItemizedBill.java


package eecs2030.test4;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * A bill that includes an itemized list of purchased items.
 * The itemized bill and its list form a composition. The total
 * price of the bill is guaranteed to be equal to the sum
 * of the prices of the items in the bill.
 * 
 * @author EECS2030 Fall 2016-17
 *
 */
public class ItemizedBill extends Bill {

    private List<Item> items;
    
    /**
     * Initialize this bill from a list of purchased items. The total
     * price of the bill is computed as the sum of the prices of the
     * purchased items. The date of issue is the current date.
     * 
     * @param items a list of purchased items
     */
    public ItemizedBill(List<Item> items) {
        super(0);
        List<Item> copy = new ArrayList<Item>(items);
        int total = 0;
        for (Item item : copy) {
            total += item.getPrice();
        }
        this.items = copy;
        this.setTotal(total);
    }
    
    /**
     * Initialize this bill by copying another bill. The total
     * price of the bill, the date of issue, and
     * the list of items purchased are all copied from the
     * other bill.
     * 
     * @param other the bill to copy
     */
    public ItemizedBill(ItemizedBill other) {
        super(other);
        this.items = other.getItems();
    }

    /**
     * Returns the list of items purchased for this bill.
     * The returned list cannot be used to modify this bill.
     * 
     * @return the list of items purchased for this bill
     */
    public List<Item> getItems() {
        return new ArrayList<Item>(this.items);
    }
    
    /**
     * Add an item to the list of items purchased for this bill.
     * The total price of the bill is updated to include the price
     * of the added item.
     * 
     * @param item add an item to the list of items purchased for this bill
     */
    public void addItem(Item item) {
        this.setTotal(this.getTotal() + item.getPrice());
        this.items.add(item);
    }
    
    /**
     * Returns the sorted list of items purchased for this bill.
     * Items are sorted as specified in Item.compareTo.
     * 
     * @return the sorted list of items purchased for this bill
     */
    public List<Item> getSortedItems() {
        List<Item> it = this.getItems();
        Collections.sort(it);
        return it;
    }

    /**
     * Returns a hash code for this bill. The hash code is equal to the
     * hash code returned by the superclass Bill.
     * 
     * @return a hash code for this bill
     * @see eecs2030.test4.Bill#hashCode()
     */
    @Override
    public int hashCode() {
        return super.hashCode();
    }

    /**
     * Compares two itemized bills for equality. The result is true if and only if the
     * argument is not null and is an ItemizedBill object that has:
     * 
     * <ul>
     * <li>the same total price as this itemized bill
     * <li>the same date of issue as this itemized bill
     * <li>the same list of items purchased ignoring the order in which
     * items appear in the list
     * </ul>
     * 
     * <p>
     * <code>equals</code> does not modify the list of items in this bill.
     * 
     * @param obj the object to compare this item against
     * @return true if the given object represents an ItemizedBill equivalent to this
     *         bill, false otherwise
     * @see eecs2030.test4.Bill#equals(java.lang.Object)
     */
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (!super.equals(obj))
            return false;
        if (getClass() != obj.getClass())
            return false;
        ItemizedBill other = (ItemizedBill) obj;
        return this.getSortedItems().equals(other.getSortedItems());
    }
  
}

Short Answer Questions

Question 1 (1 mark)

What is the main advantage of using composition over aggregation? (Be concise in your answer; try to use no more than one sentence.)

Composition allows a class to control its internal state.


Question 2

Suppose that a method header states that it throws an exception of type X:

public void someMethod() throws X

If a child class overrides the method, what types of exceptions can the child class version of the method throw?

A. any type that is a child of Exception
B. any type that is a child of RuntimeException
C. any type that is a child of X
D. any type that is a parent of X

C. any type that is a child of X


Question 3

Examine your implementation of getItems and the provided implementation of getSortedItems in the class ItemizedBill.

Does getSortedItems contain a privacy leak? Using no more than three sentences, explain your answer.

getSortedItems should not contain a privacy leak if getItems is implemented correctly. getItems should return a copy of the list of items, which getSortedItems can safely sort and return to the client.




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