10 - 23:25:02 CS 61B Lecture 10 Wednesday Todays reading...

Info icon This preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
02/11/09 23:25:02 1 10 CS 61B: Lecture 10 Wednesday, February 11, 2009 Today’s reading: Sierra & Bates, pp. 95-109, 662. equals() ======== Every class has an equals() method. If you don’t define one explictly, then "r1.equals(r2)" returns the same boolean value as "r1 == r2", where r1 and r2 are references. However, many classes redefine equals() to compare the _content_ of two objects. String is such a class. In the following example, "s1 == s2" is false, but "s1.equals(s2)" is true. "s2 == s3" and "s2.equals(s3)" are both true. --- --------- --- --------- --- s1 |.+--->| "Yes" | s2 |.+--->| "Yes" |<---+.| s3 --- --------- --- --------- --- For Homework 3, you use the equals() method to compare Integer objects. Integer is a class of the java.lang library; an Integer object contains one primitive int field. Two distinct Integer objects are equals() if they contain the same int. There are at least four different degrees of equality. (1) Reference equality, ==. (2) Shallow structural equality: two objects are "equals" if all their fields are ==. For example, two SLists whose "size" fields are equal and whose "head" fields point to the same SListNode. (3) Deep structural equality: two objects are "equals" if all their fields are "equals". For example, two SLists that represent the same sequence of items (though the SListNodes may be different). (4) Logical equality. Two examples: (a) Two "Set" objects are "equals" if they contain the same elements, even if the underlying lists store the elements in different orders. (b) The Fractions 1/3 and 2/6 are "equals", even though their numerators and denominators are all different. The "equals" method for a particular class may test any of these four levels of equality, depending on what seems appropriate. Let’s write an equals() method for SLists that tests for deep structural equality. The following method returns true only if the two lists represent identical sequences of items. public class SList { public boolean equals(SList other) { if (size != other.size) { return false; } SListNode n1 = head; SListNode n2 = other.head; while (n1 != null) { if (!n1.item.equals(n2.item)) { return false; } n1 = n1.next; n2 = n2.next; } return true; } } Note that this implementation may fail if the SList invariants have been corrupted. TESTING ======= Complex software, like Project 1, is easier to debug if you write test code to make sure that the methods you write are working correctly. We’ll consider three types of testing: (1) Modular testing: testing each method and each class separately. (2) Integration testing: testing a set of methods/classes together.
Image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern