# Problem with class-exercise using Java

**URL:** <https://forum.codewithmosh.com/t/problem-with-class-exercise-using-java/9434>\
**Category:** Java\
**Created:** [December 26, 2021, 7:02am UTC](https://forum.codewithmosh.com/t/problem-with-class-exercise-using-java/9434 "2021-12-26T07:02:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![timyliniger](https://avatars.discourse-cdn.com/v4/letter/t/e9a140/32.png) [@timyliniger](https://forum.codewithmosh.com/u/timyliniger)\
**Post date:** [December 26, 2021, 7:02am UTC](https://forum.codewithmosh.com/t/problem-with-class-exercise-using-java/9434/1 "2021-12-26T07:02:22Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![JerryC](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/jerryc/32/859_2.png) [@JerryC](https://forum.codewithmosh.com/u/JerryC)\
**Post date:** [January 5, 2022, 2:57am UTC](https://forum.codewithmosh.com/t/problem-with-class-exercise-using-java/9434/2 "2022-01-05T02:57:48Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jmrunkle](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/jmrunkle/32/3765_2.png) [@jmrunkle](https://forum.codewithmosh.com/u/jmrunkle)\
**Post date:** [August 18, 2022, 5:13am UTC](https://forum.codewithmosh.com/t/problem-with-class-exercise-using-java/9434/3 "2022-08-18T05:13:57Z")

</div>

> [@timyliniger](#):
>
> You can assume that there is always a maximum of 100 racers in a race.

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](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/Arrays.html#sort(T%5B%5D,java.util.Comparator)) 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).
