EECS1030M Test 4

Wednesday March 18, 2015, 13:00-14:15
Lab 02
Version B


Programming question

Implement the WordPuzzle class. A skeleton can be found here. The API can be found here. A jar with the Lock class can be found here.

package test4;

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

/**
 * A word puzzle is a special kind of puzzle.  Each word puzzle
 * has a collection of words.  The word puzzle and its collection
 * of words form a composition.
 *
 */
public class WordPuzzle extends Puzzle 
{
    private Set<String> words;
    
    /**
     * Initializes this word puzzle to be easy and have the given 
     * collection of words.
     * 
     * @param words the collection of words of this word puzzle.
     * @pre. words != null
     */
    public WordPuzzle(Set<String> words)
    {
	super();
	this.setWords(words);
    }

    /**
     * Returns the collection of words of this word puzzle.
     * 
     * @return the collection of words of this word puzzle.
     */
    public Set<String> getWords() 
    {
	return new HashSet<String>(this.words);
    }

    /**
     * Sets the collection of words of this word puzzle to
     * the given collection.
     * 
     * @param words the new collection of words of this word puzzle.
     * @pre. words != null
     */
    public void setWords(Set<String> words) 
    {
	this.words = new HashSet<String>(words);
    }
    
    /**
     * Returns the number of words of this word puzzle.
     * 
     * @return the number of words of this word puzzle.
     */
    public int numberOfWords()
    {
	return this.getWords().size();
    }
    
    /**
     * Returns a string representation of this word puzzle.
     * For example, an easy word puzzle with the words test,
     * awesome and question is represented as
     * "Easy puzzle: awesome question test"
     * 
     * @return "Easy puzzle: ", "Intermediate puzzle: " or "Difficult puzzle: ",
     * depend of the level of this puzzle followed by the words of this
     * word puzzle sorted lexicographically (as in a dictionary).
     */
    public String toString()
    {
	StringBuffer representation = new StringBuffer(super.toString());
	representation.append(":");
	for (String word : new TreeSet<String>(this.getWords()))
	{
	    representation.append(" ");
	    representation.append(word);
	}
	return representation.toString();
    }
}


Other questions

Question 1

Are non-static attributes of the super class part of the state of instances of the sub class? Select the best answer.
A. Yes.
B. No.
C. Only if they are private.
D. Only if they are public.



Question 2

Another name for the inheritance relation is the is-a relation.



Question 3

Consider the following classes.

public class CreditCard
{
   private double balance;

   /**
    * Tests whether the balance of this card is positive.
    *
    * @return true if the balance of this card is positive, false otherwise.
    */
   public boolean positive()
   {
      return this.balance > 0;
   }
}

public class RewardCard extends CreditCard
{
   private int points;

   /**
    * Tests whether the balance or the points balance of this card is positive.
    *
    * @return true if the balance or the points balance of this card is positive, false otherwise.
    */
   public boolean positive()
   {
      return super.positive() || this.points > 0;
   }
}
Point out what is wrong in the above code. ("Use this.getBalance() instead of this.balance" is not the answer.)
The postcondition of the super class does not imply the postcondition of the sub class (the super class returns true if the balance is positive, whereas the sub class returns true if the balance is positive or the point balance is positive).