340 likes | 436 Views
More Useful Methods. Implementing equals compareTo toString. Another look at the String class. Strings cannot be compared using the relational operators ( <, >, <=, >=, ==, !=). WHY NOT? To compare String s we use equals and compareTo.
E N D
More Useful Methods Implementing equals compareTo toString Drew University
Another look at the String class. • Strings cannot be compared using the relational operators ( <, >, <=, >=, ==, !=). WHY NOT? • To compare Strings we use equals and compareTo. Drew University
class java.lang.String implements java.lang.Comparable • boolean equals(Object other) • Compares the string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object. Drew University
class java.lang.String implementsjava.lang.Comparable • int compareTo(Object other) • compares this String to another Object. If the object other is a String, • returns 0 if the argument is lexicographically equal to this string • returns a value less than 0 if the argument is a string that is lexicographically greater than this string • returns a value greater than 0 if the argument is a string that is lexicographically less than this string • else throws a ClassCastException • strings are comparable only to other strings Drew University
Example: String s = "apple"; String s2 = "dog"; if (s.compareTo(s2) < 0) { System.out.println(s1 + " comes before " + s2); } else if (s.compareTo(s2) > 0) { System.out.println(s1 + " comes after " + s2); } else { System.out.println("The strings are equal."); } Drew University
Comparing Strings • DO NOT use == to compare strings! == asks if two String variables are referencing the same String. equals compares the values referenced by two String variables. Drew University
Comparing Strings • DO NOT use ( <, >, <=, >=, ==, !=)to compare String values. Use compareTo. Drew University
One more time!!! • Strings are objects… • Careful with == and equals! • compareTo and equals ARE defined for Strings! • Look at the API! Drew University
class java.lang.String implements java.lang.Comparable Methods you are responsible for: • int length() • returns the length of this string • String substring (int from, int to) • returns a string beginning at from and ending at to - 1 • String substring (int from) • returns substring(from, length()) • int indexOf(String s) • returns the index of the first occurrence of s; • returns -1 if not found Drew University
class java.lang.String implements java.lang.Comparable String methods you are responsible for: • boolean equals(Object o) Compares the string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object. • int compareTo(Object other) • compares this String to another Object. If the object other is a String, • returns 0 if the argument is lexicographically equal to this string • returns a value less than 0 if the argument is a string that is lexicographically greater than this string • returns a value greater than 0 if the argument is a string that is lexicographically less than this string Drew University
A bit about the Object class • A look at the API first. • Methods we need to concern ourselves with: • equals • toString Drew University
Let's consider our Point class public class Point { public Point(double x, double y) { xCoordinate = x; yCoordinate = y; } public double getX() { return xCoordinate; } public double getY() { return yCoordinate; } private double xCoordinate; private double yCoordinate; } Drew University
What does it mean to ask if two points are equal? Drew University
What does it mean to ask if two points are equal? • Do they have the same xCoordinate? • Do they have the same yCoordinate? Drew University
We have to write equals for the Point class. if (getX() == p.getX() && getY() == p.getY()) { return true; } else { return false; } Drew University
We are overridingObject's equals method. • public boolean equals(Object obj) • We MUST have an equals method with this same header. Drew University
Point class (wrong) public boolean equals(Object o) { if(getX() == p.getX() && getY() == p.getY()) // Object does not have a getX method!!! { return true; } else { return false; } Drew University
Point class public boolean equals(Object o) { Point p = (Point) o; //cast if(getX() == p.getX() && getY() == p.getY()) { return true; } else { return false; } Drew University
Point class – another way public boolean equals(Object o) { Point p = (Point) o; //cast return (getX() == p.getX() && getY() == p.getY()) } Drew University
Suppose I want to write the point in a System.out.println statement? • System.out.println(p1); Drew University
Suppose I want to write the point in a System.out.println statement? • System.out.println(p1); • Output: Point@108786b Drew University
Suppose I want to write the point in a System.out.println statement? • System.out.println(p1); • Output: Point@108786b • Not very informative or helpful! • What happened? Drew University
In the classes YOU create…. • provide toString method • so that you can print info about your object • System.out.println(objectName); • The Point class • We want to see: (4,3) printed. Drew University
The Object class • toString() Returns a string representation of the object. public StringtoString() Drew University
The Point class • public StringtoString() Drew University
The Point class public String toString() { String s = "(" + getX() + "," + getY() + ")"; return s; } Drew University
What about comparing Points… • Do we ever ask, "Is point p < point p2?" • What does less than "mean" for points? Drew University
Can you think of a class where we might want to include compareTo? Drew University
Can you think of a class where we might want to include compareTo? What about the Purse class? Drew University
How do we compare two Purses? public class Purse implements Comparable { public boolean equals(Object o) { } public int compareTo(Object o) { } } Drew University
How do we compare two Purses? public int compareTo(Object o) { Purse p = (Purse)o; if (getTotal()< p.getTotal()) return -1; if (getTotal()> p.getTotal()) return 1; return 0; } Drew University
How do we compare two Purses? What does it mean for two Purses to be equal? How do we define equals? Drew University
What does toString look like for a Purse? Drew University
Summary • For your own classes, • include toString • think about including compareTo • think about including equals Drew University