Basic (?) sorting and comparing help.
Hi everyone! I'm a long time lurker on the boards, but I've got myself into such a panic about being unable to finish a project that I've registered to ask you all for help specifically.
"The project is intended to give you experience of working with one of thestandard Java interfaces (Comparable) and with implementing asimple sorting algorithm, applied to data about people and their birthdays."
Even though it's supposed to be easy, I'm having problems jumping in and getting started and the deadline for its submission is fast approaching. I'm not asking to be spoonfed code, but really firmly prodded in the right direction. Here's what I've got to do:
1. Write and test a person class that implements the Comparable interface. Use the compareTo method of the class to find out whether two people's birthdays are the same.
2. Write and test a People class that has a method to read in a list of names and ages, storing them as an array of Person objects, and a method to print out the list.
3. Write and test a method to sort an array of Person objects into birthday order
4. Add a method to check the sorted list for shared birthdays and use this to test a given set of data for examples of the paradox.
It all sounds really easy written out like that, but I switched degrees (from Politics to Computer Science) really late this academic year so I've missed out on all the basics, especially how to pass data between classes.
Any help you can give would be greatly appreciated, and sorry that this is so long and rambly.
[1608 byte] By [
KatieMa] at [2007-11-27 3:06:15]

For number 1:In your Person class, you need to have that class implement Comparable. Then, you'd have to to override the compareTo() method. Return 0 if the two are the same (based on the variable(s) that you are using to compare).
> 1. Write and test a person class that implements the
> Comparable interface. Use the compareTo method of the
> class to find out whether two people's birthdays are
> the same.
Object-Oriented Programming Concepts:
http://java.sun.com/docs/books/tutorial/java/concepts/
> 2. Write and test a People class that has a method to
> read in a list of names and ages, storing them as an
> array of Person objects, and a method to print out
> the list.
Passing arguments to a method:
http://java.sun.com/docs/books/tutorial/java/javaOO/arguments.html
Working with arrays:
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/arrays.html
> 3. Write and test a method to sort an array of Person
> objects into birthday order
Ordering Objects:
http://java.sun.com/docs/books/tutorial/collections/interfaces/order.html
> 4. Add a method to check the sorted list for shared
> birthdays and use this to test a given set of data
> for examples of the paradox.
A simple for-statement will do just fine for that (after you have sorted you array or list!):
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/for.html
> 1. Write and test a person class that implements the
> Comparable interface. Use the compareTo method of the
> class to find out whether two people's birthdays are
> the same.
Well, Date already implements compareTo, so your Person class just neeeds to delegate that call. One-liner. Return birthday compared To otherPerson's birthday.
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Comparable.html
http://java.sun.com/docs/books/tutorial/collections/interfaces/order.html
should give you all the info you need.
> 2. Write and test a People class that has a method to
> read in a list of names and ages, storing them as an
> array of Person objects, and a method to print out
> the list.
Search the web for tutorials about file reading in Java (see fileReader). Then look at String.split() - closely - and at passing the results to the Person c'tor to have all the data for a person available upon initialization.
> 3. Write and test a method to sort an array of Person
> objects into birthday order
See the tutorial I linked above.
> 4. Add a method to check the sorted list for shared
> birthdays and use this to test a given set of data
> for examples of the paradox.
Unclear specification. Is b'day defined as day in year, or a certain date including the year? Depending on that, you might not be able to use the Date's compareTo method for sorting.
> It all sounds really easy written out like that, but
> I switched degrees (from Politics to Computer
> Science) really late this academic year so I've
> missed out on all the basics, especially how to pass
> data between classes.
http://java.sun.com/docs/books/tutorial/
>
> Unclear specification. Is b'day defined as day in
> year, or a certain date including the year? Depending
> on that, you might not be able to use the Date's
> compareTo method for sorting.
>
It's defined as day and month, which are both integers. Thankyou so much, by the way!
[url http://linux.wku.edu/~lamonml/algor/sort/index.html]Sorting algorithms[/url]