1 class design cs 3331 sec 6.1 & 6.3 of [jia03]. 2 outline organizing classes design...
DESCRIPTION
3 Public vs. Helper Classes Motivation To hide implementation decisions and details from clients Approach Separate helper classes and make them NOT visible to clients Public classes: for general use by clients Helper (or auxiliary) classes: used solely to implement public classes ClientLinkedListNode > public class helper class 0,2TRANSCRIPT
![Page 1: 1 Class Design CS 3331 Sec 6.1 & 6.3 of [Jia03]. 2 Outline Organizing classes Design guidelines Canonical forms of classes equals method hashCode](https://reader035.vdocuments.net/reader035/viewer/2022070605/5a4d1ad17f8b9ab0599719b9/html5/thumbnails/1.jpg)
1
Class Design
CS 3331
Sec 6.1 & 6.3 of [Jia03]
![Page 2: 1 Class Design CS 3331 Sec 6.1 & 6.3 of [Jia03]. 2 Outline Organizing classes Design guidelines Canonical forms of classes equals method hashCode](https://reader035.vdocuments.net/reader035/viewer/2022070605/5a4d1ad17f8b9ab0599719b9/html5/thumbnails/2.jpg)
2
Outline
Organizing classes Design guidelines Canonical forms of classes
equals method hashCode method
![Page 3: 1 Class Design CS 3331 Sec 6.1 & 6.3 of [Jia03]. 2 Outline Organizing classes Design guidelines Canonical forms of classes equals method hashCode](https://reader035.vdocuments.net/reader035/viewer/2022070605/5a4d1ad17f8b9ab0599719b9/html5/thumbnails/3.jpg)
3
Public vs. Helper Classes
Motivation To hide implementation decisions and details from clients
Approach Separate helper classes and make them NOT visible to
clients Public classes: for general use by clients Helper (or auxiliary) classes: used solely to implement
public classes
Client LinkedList Node<<use>>
public class helper class
0,20,2
![Page 4: 1 Class Design CS 3331 Sec 6.1 & 6.3 of [Jia03]. 2 Outline Organizing classes Design guidelines Canonical forms of classes equals method hashCode](https://reader035.vdocuments.net/reader035/viewer/2022070605/5a4d1ad17f8b9ab0599719b9/html5/thumbnails/4.jpg)
4
Organizing Helper Classes
Approaches Separate (standalone) classes in separate files Separate (standalone) classes in the same file Nested classes: static, non-static (inner), local inner,
anonymous inner
public class LinkedList { protected Node head, tail; /* … */ protected static class Node { private Object value; private Node next, prev; /* … */ }}
![Page 5: 1 Class Design CS 3331 Sec 6.1 & 6.3 of [Jia03]. 2 Outline Organizing classes Design guidelines Canonical forms of classes equals method hashCode](https://reader035.vdocuments.net/reader035/viewer/2022070605/5a4d1ad17f8b9ab0599719b9/html5/thumbnails/5.jpg)
5
Outline
Public vs helper classes Design guidelines Canonical forms of classes
equals method hashCode method
![Page 6: 1 Class Design CS 3331 Sec 6.1 & 6.3 of [Jia03]. 2 Outline Organizing classes Design guidelines Canonical forms of classes equals method hashCode](https://reader035.vdocuments.net/reader035/viewer/2022070605/5a4d1ad17f8b9ab0599719b9/html5/thumbnails/6.jpg)
6
Design Guidelines G1: Avoid public fields
To minimize accessibility of fields Make fields non-public (e.g., private attributes) Provide accessors (e.g., getAttr() or isAttr()) Provide mutators (e.g., setAttr())
G2: Completeness of public interfaces G3: Separate interface from implementation
![Page 7: 1 Class Design CS 3331 Sec 6.1 & 6.3 of [Jia03]. 2 Outline Organizing classes Design guidelines Canonical forms of classes equals method hashCode](https://reader035.vdocuments.net/reader035/viewer/2022070605/5a4d1ad17f8b9ab0599719b9/html5/thumbnails/7.jpg)
7
Question
Which is better and why?
public class StudentBody1 { private ArrayList students; /* … */ public void addAll(ArrayList students) { /* … */ }}
public class StudentBody2 { private List students; /* … */ public void addAll(List students) { /* … */ }}
![Page 8: 1 Class Design CS 3331 Sec 6.1 & 6.3 of [Jia03]. 2 Outline Organizing classes Design guidelines Canonical forms of classes equals method hashCode](https://reader035.vdocuments.net/reader035/viewer/2022070605/5a4d1ad17f8b9ab0599719b9/html5/thumbnails/8.jpg)
8
Separation through Interfaces
Client <<interface>>List
uses
LinkedList ArrayList
service provider
service user
![Page 9: 1 Class Design CS 3331 Sec 6.1 & 6.3 of [Jia03]. 2 Outline Organizing classes Design guidelines Canonical forms of classes equals method hashCode](https://reader035.vdocuments.net/reader035/viewer/2022070605/5a4d1ad17f8b9ab0599719b9/html5/thumbnails/9.jpg)
9
Outline
Public vs. helper classes Design guidelines Canonical forms of classes
equals method hashCode method
![Page 10: 1 Class Design CS 3331 Sec 6.1 & 6.3 of [Jia03]. 2 Outline Organizing classes Design guidelines Canonical forms of classes equals method hashCode](https://reader035.vdocuments.net/reader035/viewer/2022070605/5a4d1ad17f8b9ab0599719b9/html5/thumbnails/10.jpg)
10
Canonical Form of Classes Objective
To ensure that objects behave “well” when manipulated by Java runtime environment and other classes.
// E.g., what will be printed?
List<Point> points = new LinkedList<>();points.add(new Point(10, 20));If (points.contains(new Point(10, 20))) { System.out.println(“Found!”);} else { System.out.println(“Not found!”);}
![Page 11: 1 Class Design CS 3331 Sec 6.1 & 6.3 of [Jia03]. 2 Outline Organizing classes Design guidelines Canonical forms of classes equals method hashCode](https://reader035.vdocuments.net/reader035/viewer/2022070605/5a4d1ad17f8b9ab0599719b9/html5/thumbnails/11.jpg)
11
Canonical Form (Cont.) Canonical forms of public classes
String representation toString method
No-argument constructor Object equality
equals and hashCode methods Cloning
Cloneable interface and clone method Serialization
Serializable interface and readObject and writeObject methods
![Page 12: 1 Class Design CS 3331 Sec 6.1 & 6.3 of [Jia03]. 2 Outline Organizing classes Design guidelines Canonical forms of classes equals method hashCode](https://reader035.vdocuments.net/reader035/viewer/2022070605/5a4d1ad17f8b9ab0599719b9/html5/thumbnails/12.jpg)
12
The toString Method
Why?
public class Point {
private int x, y;
/* … */
public String toString() { return “Point(“ + x + “, “ + y + “)”; }}
Default behavior: Point@1a46e30
![Page 13: 1 Class Design CS 3331 Sec 6.1 & 6.3 of [Jia03]. 2 Outline Organizing classes Design guidelines Canonical forms of classes equals method hashCode](https://reader035.vdocuments.net/reader035/viewer/2022070605/5a4d1ad17f8b9ab0599719b9/html5/thumbnails/13.jpg)
13
No-argument Constructor
Why?public class Point {
private int x, y;
/* … */
public Point() { this(0, 0); }
public Point(int x, int y) { this.x = x; this.y = y; }}
![Page 14: 1 Class Design CS 3331 Sec 6.1 & 6.3 of [Jia03]. 2 Outline Organizing classes Design guidelines Canonical forms of classes equals method hashCode](https://reader035.vdocuments.net/reader035/viewer/2022070605/5a4d1ad17f8b9ab0599719b9/html5/thumbnails/14.jpg)
14
Equality Identity equality vs. value quality
“==“ vs. equals()“==“ implies equals()
Is “p1 == p2” ?Is “p1.equals(p2)” ?Is “p1 == p3” ?Is “p1.equals(p3)” ?
: Point
x = 10y = 20
p1:
: Point
x = 10y = 20
p2:
p3:
Point p1 = new Point(10, 20);Point p2 = new Point(10, 20);Point p3 = p1;
![Page 15: 1 Class Design CS 3331 Sec 6.1 & 6.3 of [Jia03]. 2 Outline Organizing classes Design guidelines Canonical forms of classes equals method hashCode](https://reader035.vdocuments.net/reader035/viewer/2022070605/5a4d1ad17f8b9ab0599719b9/html5/thumbnails/15.jpg)
15
Equality (Cont.) Default implementation of equals method
Inherited from the class Object Test for object identity
Can be overridden by subclasses
![Page 16: 1 Class Design CS 3331 Sec 6.1 & 6.3 of [Jia03]. 2 Outline Organizing classes Design guidelines Canonical forms of classes equals method hashCode](https://reader035.vdocuments.net/reader035/viewer/2022070605/5a4d1ad17f8b9ab0599719b9/html5/thumbnails/16.jpg)
16
Contract of Equality Equivalence relation
Reflective: x.equals(x) Symmetric: x.equals(y) y.equals(x) Transitive: x.equals(y) y.equals(z) x.equals(z)
Consistency x.equals(y) consistently returns true or false
Nonnullity x.equals(null) always returns false
![Page 17: 1 Class Design CS 3331 Sec 6.1 & 6.3 of [Jia03]. 2 Outline Organizing classes Design guidelines Canonical forms of classes equals method hashCode](https://reader035.vdocuments.net/reader035/viewer/2022070605/5a4d1ad17f8b9ab0599719b9/html5/thumbnails/17.jpg)
17
Exercise Define equals method for the class Point.
Assume that the class has two fields x and y, both of type int.
![Page 18: 1 Class Design CS 3331 Sec 6.1 & 6.3 of [Jia03]. 2 Outline Organizing classes Design guidelines Canonical forms of classes equals method hashCode](https://reader035.vdocuments.net/reader035/viewer/2022070605/5a4d1ad17f8b9ab0599719b9/html5/thumbnails/18.jpg)
18
Template for equals MethodProposal 1: for class T
public boolean equals(Object other) { if (this == other) { return true; } if (other instanceof T) { T otherObj = (T) other; << compare each significant field and return false if not equals>> return true; } return false;}
Q: What is wrong?
For class Point:
public boolean equals(Object o) { if (this == o) { return true; } if (o instanceof Point) { Point p = (Point) o; return x == p.x && y == p.y; } return false;}
Point-x: int-y: int
![Page 19: 1 Class Design CS 3331 Sec 6.1 & 6.3 of [Jia03]. 2 Outline Organizing classes Design guidelines Canonical forms of classes equals method hashCode](https://reader035.vdocuments.net/reader035/viewer/2022070605/5a4d1ad17f8b9ab0599719b9/html5/thumbnails/19.jpg)
19
Perfect equals Method
public boolean equals(Object other) {
if (this == other) { return true; }
if (other == null) { return false; }
if (getClass() != other.getClass()) { return false; }
T otherObj = (T) other; << compare each significant field and return false if not equals>>
return true;}
![Page 20: 1 Class Design CS 3331 Sec 6.1 & 6.3 of [Jia03]. 2 Outline Organizing classes Design guidelines Canonical forms of classes equals method hashCode](https://reader035.vdocuments.net/reader035/viewer/2022070605/5a4d1ad17f8b9ab0599719b9/html5/thumbnails/20.jpg)
20
Comparing Fields
if (f != otherObj.f) { return false;}
Primitive types
if (f == null ? otherObj.f != null : !f.equals(otherObj.f)) { return false;}
Reference types
![Page 21: 1 Class Design CS 3331 Sec 6.1 & 6.3 of [Jia03]. 2 Outline Organizing classes Design guidelines Canonical forms of classes equals method hashCode](https://reader035.vdocuments.net/reader035/viewer/2022070605/5a4d1ad17f8b9ab0599719b9/html5/thumbnails/21.jpg)
21
Template for equals Method
When T has a superclass:
public boolean equals(Object other) {
if (!super.equals(other)) { return false; }
T otherObj = (T) other; << compare each significant field and return false if not equals>>
return true;}
![Page 22: 1 Class Design CS 3331 Sec 6.1 & 6.3 of [Jia03]. 2 Outline Organizing classes Design guidelines Canonical forms of classes equals method hashCode](https://reader035.vdocuments.net/reader035/viewer/2022070605/5a4d1ad17f8b9ab0599719b9/html5/thumbnails/22.jpg)
22
Exercise Define equals method for the class ColoredPoint,
a subclass of the class Point. Assume that the class has an additional field named color of type Color.
![Page 23: 1 Class Design CS 3331 Sec 6.1 & 6.3 of [Jia03]. 2 Outline Organizing classes Design guidelines Canonical forms of classes equals method hashCode](https://reader035.vdocuments.net/reader035/viewer/2022070605/5a4d1ad17f8b9ab0599719b9/html5/thumbnails/23.jpg)
23
Outline
Public vs. helper classes Design guidelines Canonical forms of classes
equals method hashCode method
![Page 24: 1 Class Design CS 3331 Sec 6.1 & 6.3 of [Jia03]. 2 Outline Organizing classes Design guidelines Canonical forms of classes equals method hashCode](https://reader035.vdocuments.net/reader035/viewer/2022070605/5a4d1ad17f8b9ab0599719b9/html5/thumbnails/24.jpg)
24
The hashCode Method Purpose
Returns the hash code of an object Used by hash-table-based collection classes
(e.g., HashMap, HashSet) Contract for hashCode
x.equals(y) x.hashCode() == y.hashCode()
![Page 25: 1 Class Design CS 3331 Sec 6.1 & 6.3 of [Jia03]. 2 Outline Organizing classes Design guidelines Canonical forms of classes equals method hashCode](https://reader035.vdocuments.net/reader035/viewer/2022070605/5a4d1ad17f8b9ab0599719b9/html5/thumbnails/25.jpg)
25
Defining hashCode Methods General scheme
Compute hash code for each significant field Combine hash code of all significant fields
public int hashCode() { int result = 0; // accumulative hash code int h; // hash code for a field <<for each field compute and combine the hash code>> return result;}
![Page 26: 1 Class Design CS 3331 Sec 6.1 & 6.3 of [Jia03]. 2 Outline Organizing classes Design guidelines Canonical forms of classes equals method hashCode](https://reader035.vdocuments.net/reader035/viewer/2022070605/5a4d1ad17f8b9ab0599719b9/html5/thumbnails/26.jpg)
26
Computing hash code for fields boolean fields
f ? 0: 1 byte, char, short, and int fields
(int) f long fields
(int)(f ^ (f >>> 32)) float fields
Float.floatToIntBits(f) double fields
Double.doubleToLongBits(f) and then to int References
If null, then 0 (or some fixed value), Recursive equals recursive hash on fields, or Hash on canonical representation
![Page 27: 1 Class Design CS 3331 Sec 6.1 & 6.3 of [Jia03]. 2 Outline Organizing classes Design guidelines Canonical forms of classes equals method hashCode](https://reader035.vdocuments.net/reader035/viewer/2022070605/5a4d1ad17f8b9ab0599719b9/html5/thumbnails/27.jpg)
27
How to Combine Hash Code? Bitwise-or (|)
result = result << n | h;where n is an arbitrary integer constant, e.g., 8.
Additionresult = result * p + h;where p is a prime number, e.g., 37.
![Page 28: 1 Class Design CS 3331 Sec 6.1 & 6.3 of [Jia03]. 2 Outline Organizing classes Design guidelines Canonical forms of classes equals method hashCode](https://reader035.vdocuments.net/reader035/viewer/2022070605/5a4d1ad17f8b9ab0599719b9/html5/thumbnails/28.jpg)
28
Example Hashcode method for class Point
public int hashCode() { int result = x; result = result << 8 | y; return result;}
public int hashCode() { int result = x; result = result * 37 + y; return result;}
![Page 29: 1 Class Design CS 3331 Sec 6.1 & 6.3 of [Jia03]. 2 Outline Organizing classes Design guidelines Canonical forms of classes equals method hashCode](https://reader035.vdocuments.net/reader035/viewer/2022070605/5a4d1ad17f8b9ab0599719b9/html5/thumbnails/29.jpg)
29
Exercise Write a hashCode method for the class
ColoredPoint, a subclass of the class Point. Assume that the class has an additional field color of type Color.