A Class Implements the Comparable Interface
From Java Example Source Code
Contents |
[edit] Overview - A Class Implements the Comparable Interface
This is a Java example program.
[edit] Java Source Code
- Package: example.comparator
- File: Time.java
package example.comparator;
public class Time implements Comparable {
private int hour, minute;
public Time(int hh, int mm) {
this.hour = hh;
this.minute = mm;
}public int compareTo(Object o) {
Time t = (Time) o;
return hour != t.hour ? hour - t.hour : minute - t.minute;
}public boolean equals(Object o) {
Time t = (Time) o;
return hour == t.hour && minute == t.minute;
}public int hashCode() {
return 60 * hour + minute;
}}
[edit] What Result You Can Get
Coming soon...
[edit] Required External Libraries and/or Files for this Java Example
Need nothing.
[edit] How to Run this Java Example Program
We recommend running this Java example program with Eclipse.
For assistance in working with Eclipse, please see How to Run Java Program with Eclipse.
It's fairly easy.
[edit] Question & Answer
Any question?
Click edit and post your question or answer here.
