|
home—info—lects—labs—exams—hws
tutor/PIs—Object120 + its docs—java.lang docs—java.util docs
Notes:
// static version: if ((length(s1.title) > length(s2.title)) … // object-oriented version: if (s1.title.length() > s2.title.length()) … |
Challenge:
given two
Review hw05-soln-Student.java.
static void recordGrade( Student stu, char grade, int hrs ) { int updatedCredits = stu.creditHours + hrs; stu.creditHours = updatedCredits; ⋮ } |
Minor thing: we wrote on the board that we want to convert
input | desired | |
'A' | 65 | 4 |
'B' | 66 | 3 |
'C' | 67 | 2 |
'D' | 68 | 1 |
'F' | 70 | 0 |
One disadvantage of this is that
4 - ((int)gr - (int)'A') |
Equality:
Review: When comparing two object references,== returnstrue if (and only if) they reference (point to) the identically-same object.
What `equals` means: If you don't do anything to say otherwise, your own classes have aequals (inherited fromjava.lang.Object ) which just does the same thing== does (?!):If you want
// Inside class java.lang.Object, deep in the bowels of Java: public boolean equals(Object that) { return this == that; }equals to do something smarter, you have to write it yourself (“overriding the version inherited fromObject ”). That's whatclass String does, which is whyequals works forString s.
Robot r1, r2, r3, r4; r1 = new Robot("cell phone",true,true); r2 = new Robot("cell phone",true,true); r3 = r2; r4 = r2.evilCopy(); |
three things extending Object120 gave us:
Deeper equality-testing:
When not using
Task:
Write
Actually, for
1
Note that there is a third meaning to “
Exercise: explain what each “
2
If you really want to know,
you should make your
// Inside class Robot: public boolean equals(Object that) { if (that == null || that.class != this.class) { return false; } else { // Now, compare each field of `this` and `that` ⋮ } } |
home—info—lects—labs—exams—hws
tutor/PIs—Object120 + its docs—java.lang docs—java.util docs
©2010, Ian Barland, Radford University Last modified 2010.Oct.21 (Thu) |
Please mail any suggestions (incl. typos, broken links) to ibarlandradford.edu |