Test 3B Feedback

Constructors 
-ItemizedBill()
1 / 1  -is the date set to the current date?
1 / 1  -is a new empty collection of items created?
1 / 1  -is the total set to zero?
 
-ItemizedBill(Date date, List<Item> items)
1 / 1  -is the date copied correctly?
1 / 1  -is the collection of items set using a shallow or deep copy?
1 / 1  -is the total computed correctly?

ItemizedBill(ItemizedBill other)
1 / 1  -is the date copied correctly?
1 / 1  -is the collection of items set using a shallow or deep copy?
1 / 1  -is the total copied correctly?

Methods
-getItems
2 / 2  -returns a deep copy of the items?
       (give 1 mark for a shallow copy, 0 for an alias)

-addItem
1 / 1  -is the item added to the collection of items?
1 / 1  -is the total updated correctly?

-removeItem
1 / 1  -is the correct exception thrown if required?
1 / 1  -is the item removed from the collection of items?
1 / 1  -is the total updated correctly?
 
-getDate
1 / 1  -returns a copy of the date?

-getTotal
1 / 1  -returns the bill total?
 
--------------------
TA Comments:

--------------------
Unit tester output:

Passed all unit tests.

--------------------

Your submission:

package test3;

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

/**
 * A bill that includes an itemized list of purchased items, the
 * date of purchase, and the total price of the bill.
 * The itemized bill and its list form a composition. 
 * The itemized bill and its date 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.
 * 
 */
public class ItemizedBill {

        /**
         * The date of purchase of this bill. 
         */
        private Date date;
        
        /**
         * The total price of this bill.
         */
        private int total;
        
    /**
     * The list of items for this bill. 
     */
    private List<Item> items;
    
    /**
     * Initialize this bill so that it has the current date,
     * a total price of zero, and an empty list of items.
     */
    public ItemizedBill() {
        this.date = new Date();
        this.total = 0;
        this.items = new ArrayList<Item>();
    }
    
    /**
     * 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 purchase is equal to the specified
     * date.
     * 
     * @param date the date for this bill
     * @param items a list of purchased items
     */
    public ItemizedBill(Date date, List<Item> items) {
        this.date = new Date(date.getTime());
        this.total = 0;
        this.items = new ArrayList<Item>();
        for (Item item : items) {
                this.addItem(item);
        }
    }
    
    /**
     * Initialize this bill by copying another bill. The total
     * price of the bill, the date of purchase, and
     * the list of items purchased are all copied from the
     * other bill.
     * 
     * @param other the bill to copy
     */
    public ItemizedBill(ItemizedBill other) {
        this(other.getDate(), other.getItems());
    }

    /**
     * Returns the list of items purchased for this bill.
     * The returned list cannot be used to modify this bill nor
     * the items in this bill.
     * 
     * @return the list of items purchased for this bill
     */
    public List<Item> getItems() {
        List<Item> returnList = new ArrayList<Item>();
        for(Item item : this.items) {
                returnList.add(new Item(item));
        }
        return returnList;
    }
    
    /**
     * Add an item to the list of items purchased for this bill.
     * Modifying the item after adding it to the bill does not
     * affect the 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.total += item.getPrice();
        this.items.add(new Item(item));
    }
    
    /**
     * Removes the specified item from the list and updates the total
     * price of the bill. Only one item is removed from the bill if
     * there are duplicate items equal to the specified item.
     * 
     * @param item the item to remove from this bill
     * @throws NoSuchElementException if this bill does not have the
     * specified item
     */
    public void removeItem(Item item) {
         if (!this.items.contains(item)) {
                 throw new NoSuchElementException();
         }
         this.total -= item.getPrice();
         this.items.remove(item);
    }
    
    /**
     * Returns a new date equal to the date of this bill.
     * 
     * @return a new date equal to the date of this bill
     */
    public Date getDate() {
        return new Date(this.date.getTime());
    }

    /**
     * Returns the total price of this bill.
     * 
     * @return the total price of this bill
     */
    public int getTotal() {
        return this.total;
    }
  
}

--------------------

Written answers marking scheme

A 
0 / 2  -is the alias performed correctly?  
2 / 2  -is the deep copy performed correctly?
       (a new Date must be created inside the loop to receive any marks)
1 / 2  -are the correct big-O complexities given?

B
0 / 1  -is the answer "aggregation"?
0 / 1  -is a suitable explanation given?
0 / 1  -is there a suitable line of code that shows description is an 
       alias for the item's description?

C 
3 / 3  -is a suitable difference stated?
 
--------------------
TA Comments

A.
(Part 1)
Your answer is a shallow copy.

List<Date> alias = dates;

(Part 3)
O(1) for the alias because all that is required is an assignment.
O(n) for the deep copy because every date in the list is copied and 
added to the list. 

B.
(Part 1)
Item is an aggregation of a String. There is no need to use 
composition because strings are immutable.

(Part 2)
boolean isSameObject = item.getDescription() == description;

isSameObject will be true if Item uses aggregation, and it will be false 
if Item uses composition.

--------------------

Your submission:

A.
(Part 1)
        List<Date> newDateList = new ArrayList<>(dates);

(Part 2)
        List<Date> newDateList = new ArrayList<>();
        for (Date date: dates) {
                newDateList.add(new Date(date.getTime());
        }

(Part 3)
        O(Part 1) = O(n)
        O(Part 2) = O(n)

B.
(Part 1)
        Yes, Item is a composition of a string. This is because a string is immutable, so 
        changing anything about the string field will create a new string object and will not 
        modify the first one.
         
(Part 2)
    description = "I have now changed the description field of the item";
    System.out.println(description.equals(item.getDescription()));
    //Which would output false because the string field of item wouldn't have changed while 
        //the description variable has changed.
        

C.
        The main difference between aggregation and composition is that composition owns it's 
        fields in the sense that it's fields can not be be modified without using the methods 
        supplied by the class, meaning that all mutable fields have to be copied when being set 
        or accessed. An aggregation, however, does own it's field, which means that the fields 
        may be modified without using the methods supplied by the class.