Join 150,103 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,841 people online right now. Registration is fast and FREE... Join Now!
I need help on how to implement the Comparable interface for the private, inner class Date, which is in the Person class below. I was told its best to implement it at the lowest possible level to keep code as modular as possible. I think I did it correctly, but since Date is within Person, I have no idea how I would test it. If you could look at my code and see what I should do, I'd greatly appreciate it.
CODE
public class Person { private String name; private Date born; private Date died; //null indicates still alive.
public Person(String initialName, int birthMonth, int birthDay, int birthYear) { name = initialName; born = new Date(birthMonth, birthDay, birthYear); died = null; }
public Person(String initialName, int birthMonth, int birthDay, int birthYear, int deathMonth, int deathDay, int deathYear) { name = initialName; born = new Date(birthMonth, birthDay, birthYear); died = new Date(deathMonth, deathDay, deathYear); }
public String toString() { if (died == null) return name+", "+born.toString()+" - "; return name+", "+born.toString()+" - "+died.toString(); }
private class Date implements Comparable { private int month, day, year;
Date(int month, int day, int year) { this.month = month; this.day = day; this.year = year; }
public int compareTo(Object other) { Date otherDate = (Date)other;
if (this.precedes(otherDate)) return -1; else if (this.equals(otherDate)) return 0; return 1; }
/** Method to determine if this Date precedes another one. * @param otherDate - Date being compared * @return boolean - TRUE if this Date precedes otherDate */ public boolean precedes(Date otherDate) { if(otherDate == null) { return true; }
public String toString() { return (month+"/"+day+"/"+year); } } }
the driver (, so far, because i don't know how to test the Comparable interface):
CODE
public class Tester { public static void main(String[] args) { Person p1 = new Person("Bob", 2, 23, 1949, 11, 30, 2003); Person p2 = new Person("Alice", 3, 12, 1977, 1, 10, 1999);
Add a static void main() method in your Person class In this main() create a dummy person Now thru this dummy person you can access Date constructor Fill an array of Date and sort it Then:
java Person
CODE
public static void main(String[] arg) { // create a dummy person so I can access Date Person p = new Person("xxx", 10, 10, 10);
okay, i see what to do now... but if you have to create a dummy person to make the Comparable interface useful for the Date class, then wouldn't it be better to just implement it for the Person class and sort a Person array. Is there any other practical application of using compareTo() and the Comparable interface in the Date class?
but if you have to create a dummy person to make the Comparable interface useful for the Date class, then wouldn't it be better to just implement it for the Person class and sort a Person array.
YOU are the one who asked for it. I tried to do exactly what you asked for.
QUOTE
I was told its best to implement it at the lowest possible level to keep code as modular as possible
I've just tried to implement your wishes
But nothing should stop you to implement Comparable in Person and have Person compareTo() method to call Date compareTo() method Then you will test the compareTo() method of the Person class not the CompareTo() method of the Date class ... that was your original request
This post has been edited by pbl: 23 Jun, 2008 - 08:22 PM
I think the first question is, why should this be an inner class?
pbl is correct, if you want to test your inner Date, and you don't want it static, you have to do it from the parent. Here's another attack that does basically the same thing, in a slightly different way, with comments. Note, this example won't run on it's own, you can figure out what to add.
java
public class Person { // the addition of a private constructor, for testing only. private Person() {} private class Date implements Comparable { // I'd do all the work in the method, to avoid any redundancy. public int compareTo(Object other) { if (other==null) { return -1; } if (!this.getClass().equals(other.getClass())) { return -1; } Date otherDate = (Date)other; // your code here // hint, this is only six lines
return 0; }
// use the compareTo you made. // also, make this an object as well, // you're overiding the default object method, which is what you want public boolean equals(Object other) { return compareTo(other)==0; } }
// your test method, INSIDE private void testDate() { Date[] dts = new Date[] { new Date(10, 15, 1900), new Date(10, 10, 2000), new Date(1, 15, 1800), new Date(1, 1, 1800) }; Arrays.sort(dts); for(Date dt : dts) { System.out.println(dt); } }
public static void main (String[] args) { // this main method is in Person class, so it can do this new Person().testDate(); // other classes don't have access to either of those methods } }