Problem with class-exercise using Java

As a good Java programming exercise I found the following exercise:

You are to write a ranking list in Java for the International Ski Federation. Write a class for a racer that stores a name, a race number and a time (as a double).
Write also a class RankingList which stores the racers sorted by time. It should be possible to insert racers and output the list of racers on the console. You can assume that there is always a maximum of 100 racers in a race. Also write a main or test method that shows how to create and output a ranking list with 2 racers.

I am able to write the class for a racer (incl. constructor). But for the class “RankingList” I don’t know how to enter the methods Sort, Insert and Print. Can someone help me there? Which data structure would be best here? A LinkedList or an array? Or can someone program the code?

  1. Use an array of doubles. And write your own sort routine. E.g.: Google bubble sort.
    OR
  2. Google sortable java 11 collection

Personally, I would use an array of Racer objects (the other one they tell you to make) that has a capacity of 100 racers given this specific constraint (max of 100 racers). You would also need something to track the number of racers that have been added.

You can use Arrays.sort and pass a Comparator that compares the Racer objects by the particular condition (time). You could also just do a bubble sort as you insert the racers (but you would have to do that manually).

1 Like