Example 3.13 Interface Measurable |
/** An interface for methods that return
* the perimeter and area of an object.
*/
public interface Measurable
{
/** Task: Gets the perimeter.
* @return the perimeter */
public double getPerimeter ();
/** Task: Gets the area.
* @return the area */
public double getArea ();
} // end Measurable
|
Example 3.14 Interface NameInterface |
/** An interface for a class of names. */
public interface NameInterface
{
/** Task: Sets the first and last names.
* @param firstName a string that is the desired first name
* @param lastName a string that is the desired last name */
public void setName (String firstName, String lastName);
/** Task: Gets the full name.
* @return a string containing the first and last names */
public String getName ();
public void setFirst (String firstName);
public String getFirst ();
public void setLast (String lastName);
public String getLast ();
public void giveLastNameTo (NameInterface aName);
public String toString ();
} // end NameInterface
|
Example 3.28 Class Circle |
public class Circle implements Comparable < Circle > , Measurable { private double radius; // Definitions of constructors and methods are here public int compareTo (Circle other) { int result; if (this.equals (other)) result = 0; else if (radius < other.radius) result = -1; else result = 1; return result; } // compareTo // Version 2 // assumes radius is an integer public int compareTo (Circle other) { return radius - other.radius; // Note: value returned not necessarily -1 or +1 // but satisfies the definition } // compareTo } |