EECS1030M Test 4

Wednesday March 18, 2015, 16:00-17:15
Lab 01
Version F


Programming question

Implement the PictureBook class. A skeleton can be found here. The API can be found here. A jar with the Book class can be found here.

package test4;

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

import javax.swing.ImageIcon;

/**
 * A picture book is a special type of book.
 * Each picture book has a collection of pictures.
 * The picture book and the collection of pictures
 * form a composition.
 */
public class PictureBook extends Book
{
    private List<ImageIcon> pictures;

    /**
     * Initializes this picture book with the given title and
     * collection of pictures.
     *
     * @param title the title of this picture book.
     * @param pictures the collection of pictures of this picture book.
     * @pre. pictures != null
     */
    public PictureBook(String title, List<ImageIcon> pictures)
    {
	super(title);
	this.setPictures(pictures);
    }

    /**
     * Initializes this picture book with the title and collection
     * of picture of the given other picture book.
     * 
     * @param book another picture book.
     * @pre. book != null
     */
    public PictureBook(PictureBook book)
    {
	super(book);
	this.setPictures(book.getPictures());
    }

    /**
     * Returns the collection of pictures of this picture book.
     * 
     * @return the collection of pictures of this picture book.
     */
    public List<ImageIcon> getPictures() 
    {
        List<ImageIcon> pictures = new ArrayList<ImageIcon>();
	for (ImageIcon picture : this.pictures)
	{
	    ImageIcon copy = new ImageIcon(picture.getImage());
	    pictures.add(copy);
	}
	return pictures;
    }

    /**
     * Sets the collection of pictures of this picture book to the
     * given collection of pictures.
     * 
     * @param pictures the collection of pictures of this picture book.
     * @pre. pictures != null
     */
    public void setPictures(List<ImageIcon> pictures) 
    {
        this.pictures = new ArrayList<ImageIcon>();
	for (ImageIcon picture : pictures)
	{
	    ImageIcon copy = new ImageIcon(picture.getImage());
	    this.pictures.add(copy);
	}
    }

    /**
     * Returns a string representation of this picture book.
     * This string representation consists of "The book entitled "
     * followed by the title of this book, followed by " with ",
     * followed by the number of picture, followed by " pictures".
     *
     * @return a string representation of this picture book.
     */
    public String toString()
    {
	return super.toString() + " with " + this.getPictures().size() + " pictures";
    }
}


Other questions

Question 1

The UML diagram
denotes that (select the best answer)
A. Classes A and B are in the aggregation relation
B. Classes A and B are in the inheritance relation
C. Classes A and B are in the composition relation
D. Classes A and B are in the usage relation



Question 2

In a method that overrides a method of the super class, to invoke that method of the super class, one uses the keyword super



Question 3

Consider the following classes.

public class CreditCard
{
   private double balance;

   /**
    * Checks if the balance of this card is positive.
    *
    * @throws RuntimeException if the balance of this card is not positive.
    */
   public void checkPositive() throws RuntimeException
   {
      if (this.balance <= 0)
      {
         throw new RuntimeException("Balance is negative");
      }
   }
}

public class RewardCard extends CreditCard
{
   private int points;

   /**
    * Checks if the balance of this card is positive.
    *
    * @throws Exception if the balance of this card is not positive.
    */
   public void checkPositive() throws Exception
   {
      if (this.balance <= 0)
      {
         throw new Exception("Balance is negative");
      }
   } 
}
Point out what is wrong in the above code. ("Use this.getBalance() instead of this.balance" is not the answer.)
The super class may throw a RuntimeException, but the sub class may throw a RuntimeException, which is a sub class of Exception.
The use of this.balance in the sub class is wrong as well, as the attribute in the super class is private and therefore cannot be accessed in the sub class.