1 / 34

More Useful Methods

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.

april
Télécharger la présentation

More Useful Methods

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. More Useful Methods Implementing equals compareTo toString Drew University

  2. 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

  3. 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

  4. 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

  5. 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

  6. 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

  7. Comparing Strings • DO NOT use ( <, >, <=, >=, ==, !=)to compare String values. Use compareTo. Drew University

  8. One more time!!! • Strings are objects… • Careful with == and equals! • compareTo and equals ARE defined for Strings! • Look at the API! Drew University

  9. 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

  10. 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

  11. A bit about the Object class • A look at the API first. • Methods we need to concern ourselves with: • equals • toString Drew University

  12. 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

  13. What does it mean to ask if two points are equal? Drew University

  14. 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

  15. We have to write equals for the Point class. if (getX() == p.getX() && getY() == p.getY()) { return true; } else { return false; } Drew University

  16. We are overridingObject's equals method. • public boolean equals(Object obj) • We MUST have an equals method with this same header. Drew University

  17. 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

  18. 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

  19. Point class – another way public boolean equals(Object o) { Point p = (Point) o; //cast return (getX() == p.getX() && getY() == p.getY()) } Drew University

  20. Suppose I want to write the point in a System.out.println statement? • System.out.println(p1); Drew University

  21. Suppose I want to write the point in a System.out.println statement? • System.out.println(p1); • Output: Point@108786b Drew University

  22. 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

  23. 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

  24. The Object class • toString()           Returns a string representation of the object. public StringtoString() Drew University

  25. The Point class • public StringtoString() Drew University

  26. The Point class public String toString() { String s = "(" + getX() + "," + getY() + ")"; return s; } Drew University

  27. What about comparing Points… • Do we ever ask, "Is point p < point p2?" • What does less than "mean" for points? Drew University

  28. Can you think of a class where we might want to include compareTo? Drew University

  29. Can you think of a class where we might want to include compareTo? What about the Purse class? Drew University

  30. How do we compare two Purses? public class Purse implements Comparable { public boolean equals(Object o) { } public int compareTo(Object o) { } } Drew University

  31. 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

  32. How do we compare two Purses? What does it mean for two Purses to be equal? How do we define equals? Drew University

  33. What does toString look like for a Purse? Drew University

  34. Summary • For your own classes, • include toString • think about including compareTo • think about including equals Drew University

More Related