Java Reference
In-Depth Information
char middleInitial;
String lastName;
}
8. An accessor provides the client with access to some data in the object, while a
mutator lets the client change the object's state in some way. Accessors' names
often begin with “get” or “is”, while mutators' names often begin with “set”.
9. // Returns the distance from this point to the
// given other point.
public double distance(Point other) {
int dx = x - other.x;
int dy = y - other.y;
return Math.sqrt(dx * dx + dy * dy);
}
10. // A Name object represents a name such as "John Q. Public".
public class Name {
String firstName;
char middleInitial;
String lastName;
// The name in normal order such as "John Q. Public".
public String getNormalOrder() {
return firstName + " " + middleInitial +
". " + lastName;
}
// The name in reverse order such as "Public, John Q.".
public String getReverseOrder() {
return lastName + ", " + firstName +
" " + middleInitial + ".";
}
}
11. To make the objects of your class printable, define a toString method in it.
12. // Returns a String representation of this point.
public String toString() {
return "java.awt.Point[x=" + x + ",y=" + y + "]";
}
13. // Returns the name in normal order such as "John Q. Public".
public String toString() {
return firstName + " " + middleInitial +
". " + lastName;
}
Search WWH ::




Custom Search