Implement the Table class. A skeleton can be found here. The API can be found here. A jar with the Furniture and Leg classes can be found here.
package test4; import java.util.HashSet; import java.util.Set; /** * A table is a piece of furniture. Each table * has a collection of legs. The table and its collection * of legs form a composition. */ public class Table extends Furniture implements Comparable<Table> { private Set<Leg> legs; /** * Initializes this table with the given brand and the * given collection of legs. * * @param brand the brand of the table. * @pre. brand != null * @param legs the legs of the table. * @pre. legs != null */ public Table(String brand, Set<Leg> legs) { super(brand); this.setLegs(legs); } /** * Returns the legs of this table. * * @return the legs of this table. */ public Set<Leg> getLegs() { return new HashSet<Leg>(this.legs); } /** * Sets the legs of this table to the given collection of legs. * * @param legs the legs of this table. * @pre. legs != null */ public void setLegs(Set<Leg> legs) { this.legs = new HashSet<Leg>(legs); } /** * Tests for equality of this table and the given other object. * Two tables are equal if they have the same brand and the * same number of legs. * * @param object Another object. * @return true if this table and the other are the same, false otherwise. */ public boolean equals(Object object) { boolean equal; if (object != null && this.getClass() == object.getClass()) { Table other = (Table) object; equal = super.equals(other) && this.getLegs().size() == other.getLegs().size(); } else { equal = false; } return equal; } /** * Compares this table with the given table. * This table is smaller than the other table if its unique id * is smaller than the unique id of the other table. * * @param other Another table. * @return (unique id of this table) - (unique id of other table). * @throws NullPointerException if other is null. */ public int compareTo(Table other) { return Integer.parseInt(this.toString()) - Integer.parseInt(other.toString()); } }
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
To delegate to the constructor of the super class, one uses the keyword super
Consider the following classes.
public class CreditCard { private double balance; /** * 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"); } } } public class RewardCard extends CreditCard { private int points; /** * Checks if the points balance of this card is positive. * * @throws Exception if the points balance of this card is not positive. */ public void checkPositive() throws Exception { if (this.points <= 0) { throw new Exception("Points balance is negative"); } } }