vote up 0 vote down star

E2A

The Code will probably claify things:

http://pastebin.com/d43446384 - Team class

http://pastebin.com/d63ae5281 - League Class

http://pastebin.com/d4dd519d9 - Driver (Contains main method)


I have a "League" class which contains 3 "Team" objects each of which have a String labelled "team", an integer "points" and an array of integers called "players".

The constructor sets up the "name" and the number of "players" and a method called "play" modifies the values of "points" and elements of the "players" array (meant to be a fictional sports match in where random numbers are chosen, compared and the outcome assigns points to a single player and the team).

Problem 1 - Sorting without a sorting method

However later I need to call a method that sorts the clubs based upon their points value and then sort the players of each object by their score. In the case of a tie they are to be sorted by largest average player score and if still tied by alphabetical order.

We were told:

The methods within the League class can be designed so that the method PrintFullTable does not need to use a sorting method to arrange the team records. How?

So using the attributes listed above (or any additional ones), once a game is played and points have been assigned to a single player and the team, the teamss and the players can be printed in the order specified above without using a sorting method.

Problem 2 - User selects objects

In order to create a match the user needs to pick two of the 3 club objects, so I get the input from the user.

c1 = input.next().toUpperCase();

But when I tried to enter this into the constructor which is set like so...

public void play (Club c1, Club c2)

I get the inevitable type mismatch error...

The only way I can get around this is by an if else statement like below...

        if(c1.equals("CLUBA")) {
            opponentA = ClubA;
        }

        else if(c1.equals("CLUBB")) {
            opponentA = ClubB;
        }

        else if(c1.equals("CLUBC")) {
            opponentA = ClubC;
        }

Surely there's a better way to do the above and use the input to choose an object.

If you need me to elaborate please ask.

Thanks.

flag

2 Answers

vote up 1 vote down

Problem 1: use Collections#sort() with a Comparator. Also see the Sun tutorial about this.

Problem 2: you may want an enum Clubs or to provide a new Club(String name) constructor.

link|flag
vote up 0 vote down

What is the Table class?

Regarding Problem 2:

The best way i would think of is using a Map<String, Club>, where the keys are the club names or whatever fits best to identify a club.

link|flag
Table class should be called "League", apologies I changed the names to cause less confusion. – Anachist2010 Dec 16 at 2:14

Your Answer

Get an OpenID
or

Not the answer you're looking for? Browse other questions tagged or ask your own question.