









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!










Department of Computer Science University of Maryland, College Park
public class Name { private String myName; public Name(String n) { myName = n; } public boolean equals(Object o) { if (!(o instanceof Name)) return false; Name n = (Name)o; return myName.equals(n.myName); } public static void main(String[ ] args) { Set s = new HashSet(); s.add(new Name("Donald")); System.out.println( s.contains(new Name("Donald"))); } } Output
**1. True
public class Trivial { public static void main(String args[ ]) { System.out.print("H" + "a"); System.out.print('H' + 'a'); } }
**1. Ha
public final class Fval { private final float f; public Fval(float f) { this.f = f; } // ACCESSORS WITHOUT CORRESPONDING MUTATORS public float value( ) { return f; } // ALL OPERATIONS RETURN NEW Fval public Fval add(Fval x) { return new Fval(f + x.f); } // SUBTRACT, MULTIPLY, ETC. SIMILAR TO ADD
// EXPORTED CONSTANTS public static final Fval ZERO = new Fval(0); public static final Fval ONE = new Fval(1); // STATIC FACTORY CAN CACHE COMMON VALUES public static Fval valueOf(float f) { ... } // PRIVATE CONSTRUCTOR MAKES FACTORY MANDATORY private Fval (float f) { this.f = f; }
// GOAL – PERSON’S BIRTHDAY IS INVARIANT public class Person { // PROTECTS birthDate FROM MODIFICATION????? private final Date birthDate; public Person(Date birthDate){ this.birthDate = birthDate; } public Date bday() { return birthDate; } }