Help writing Java programme?

spinback

Beta member
Messages
1
Hi all,

Just found this forum, hoping someone will be able to help me out here. I'm trying to write a programme in Java. I've tried to write it myself but in all honesty I am completely lost with it, even though it seems like a fairly broad specification. Can anyone help me out?

Write a program that creates an ArrayList of Rationals and sorts them into increasing order. Use appropriate methods from the Collections Framework classes to sort the elements into increasing order.

Hopefully it isn't too time consuming; obviously if it is, forget it! Thanks very much to anyone who can help though.
 
Personally I would use quicksort rather than methods from the collection class but if thats what you're required to use then you'll have to do it the long way.

I would suggest looking into the API specification at http://java.sun.com/j2se/1.5.0/docs/api/ that should help you figure out how to do your task. I take it the question was set by an educational institution, in which case you'll have to try your best on your own (being in the 2nd year of my degree I can assure you you'll have to learn it at some point and it WILL benefit you). For general tutorials on Java see my post (quite far backdated) on "How and where to learn Java/C++"
 
My only suggestion would be to convert the rationals to floating point numbers. for example:
1/2 = .5.

Then, you can put the decimal values in the array, sort them, and then convert the numbers back to the rational form when outputting. The array class has a method called sort() so you can check out the java api from the link above and play with that method to get it to do what you want. Alternatively, you may be able to store the rationals as strings, and not worry about the conversion. Just use an Integer.parseInt(rationalNumber); type setup and store the result in the array.
 
Back
Top Bottom