EECS1030M Test 3

Wednesday February 25, 2015, 13:00-14:15
Lab 02
Version A


Programming question

Implement the Badge class. A skeleton can be found here.

package test3;

import java.util.Random;

/**
 * This class represents a badge.  Each badge has a name.
 * 
 * @author Franck van Breugel
 */
public class Badge 
{
    private String name;

    /**
     * Initializes this badge with the name "No Name".
     */
    public Badge()
    {
	this("No Name");
    }
    
    /**
     * Initializes this badge with the given name.
     * 
     * @param name the name of this badge.
     * @pre. name != null
     */
    public Badge(String name) 
    {
	this.setName(name);
    }

    /**
     * Returns the name of this badge.
     * 
     * @return the name of this badge.
     */
    public String getName() 
    {
	return this.name;
    }

    /**
     * Sets the name of this badge to the given name.
     * 
     * @param name the new name of this badge.
     * @pre. name != null
     */
    public void setName(String name) 
    {
	this.name = name;
    }
    
    /**
     * Tests whether this badge is the same as the given object.
     * Two badges are the same if their names are the same, ignoring
     * capitalizations.  For example, badges with the names "John doe"
     * and "john DOE" are considered equal.
     * 
     * @param object an object.
     * @return true if this badge is the same as the given object,
     * false otherwise.
     */
    public boolean equals(Object object)
    {
	boolean equal;
	if (object != null && this.getClass() == object.getClass())
	{
	    Badge other = (Badge) object;
	    equal = this.getName().toLowerCase().equals(other.getName().toLowerCase());
	}
	else
	{
	    equal = false;
	}
	return equal;
    }
    
    // Although equals is overridden, you do not have to override hashCode in this test
    
    /**
     * Random object that can be used in the getRandom method.
     */
    private static Random random = new Random(System.currentTimeMillis());
    
    /**
     * Returns a badge with either the name "John Doe" or the
     * name "Jane Doe".  The name is chosen randomly.
     * 
     * @return a badge with either the name "John Doe" or the
     * name "Jane Doe".
     */
    public static Badge getRandom()
    {
	Badge badge;
	if (Badge.random.nextBoolean())
	{
	    badge = new Badge("John Doe");
	}
	else
	{
	    badge = new Badge("Jane Doe");
	}
	return badge;
    }
}


Other questions

Question 1

In the declaration of an attribute, what does the keyword final denote?
A. That is not inherited by a sub class.
B. That it is a constant.
C. That is belong to a class, not an individual object.



Question 2

Suppose that a class has a copy constructor, but not a default constructor. Which of the following statements is correct?
A. The class does not compile.
B. The compiler adds a default constructor.
C. The compiler does not add a default constructor.



Question 3

A static attribute is part of the state of a(n) class



Question 4

The design pattern that ensures that there is at most one instance of a class is known as the singleton design pattern



Question 5

The class IP has an attribute named address of type String and implements the multiton design pattern. Two IP objects are the same if they have the same address. Do we have to override the equals method of the Object class? Explain your answer.
Since two IP objects are the same iff they have the same address and there is at most one IP object with the same address, an IP can only be equal to itself. This is exactly captured by the equals method of the Object class. Hence, we do not have to override the equals method of the Object class.



Question 6

Consider the following class.

public class Person 
{
   private int age;

   /**
    * Initializes this person with the given age.
    *
    * @param age the age of this person.
    * @pre. age >= 0
    */
   public Person(int age)
   {
      this.age = age;
   }


   /**
    * Increments the age of this person.
    */
   public void increment()
   {
      this.age++;
   }
}
Is
//@ invariant this.age >= 0
a class invariant? Explain your answer.
The following two constraints have to be met:
  1. The class invariant this.age >= 0 has to be true after the invocation of the constructor, provided that the precondition age >= 0 is met. Obviously, if age >= 0 then this.age >= 0 after the assignment this.age = age.
  2. The class invariant this.age >= 0 has to be maintained by the increment method. If this.age >= 0 holds before the invocation of the increment method, then this.age >= 0 also holds after the execution of the statement this.age++.
So, this.age >= 0 is a class invariant.