Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: A java question - Help Urgently needed?

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post A java question - Help Urgently needed?

    When a new player is registered, she/he
    should be inserted into the Club class in alphabetical order of last name (and then first name
    and then registration number if last names are the same). To do this make your Person and
    Player class implement the Comparable interface.Write a Comparator class that compares players by batting average (number of runs scored
    divided by no of times dismissed). If a player has never been dismissed list them at the
    bottom of the averages in decreasing order of runs scored. Implement a new method
    averages to the Club class that passes a Comparator to implement the above ordering.
    Hence write a main program in a class Testing that will print information about each
    player in the club where players are listed by decreasing order of batting average. This should
    allow ordering to be dictated by the Comparator passed by the main program without
    modifying the code in any of your classes.

    This is my code for club class and I dont know how to do the registration bit and how to ipmplement comparable for average ....Please help me.

    import java.util.*;




    public class Club
    {
    private List<Player> list;
    private List<WicketKeeper> list1;
    private List<Bowler> list2;
    private String clubname;
    private static int regNum=0;
    public Club(String name)
    {
    clubname=name;
    list=new ArrayList<Player>();
    list1=new ArrayList<WicketKeeper>();
    list2=new ArrayList<Bowler>();
    }

    public void removePlayer(int IdPlayer)
    {
    list.remove(IdPlayer);
    }
    public Club(int Id)
    {
    regNum = Id;
    }
    public int getId()
    {
    return regNum;
    }

    public void add(Player p)
    {
    list.add(p);
    regNum++;
    }
    public void add(WicketKeeper p)
    {
    list1.add(p);
    }
    public void add(Bowler p)
    {
    list2.add(p);
    }


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,660
    Thanks
    25
    Thanked 325 Times in 302 Posts

    Default Re: A java question - Help Urgently needed?

    Please put your code in code tags in the future.

    To put the Players in the list alphabetically (your add(Player p) method), you need to loop through the list and compare names. Once you find where the player should be inserted, use the list.add(index,Player) method, instead of the list.add(Player) method.

    As for implementing Comparable (here is the API: Comparable (Java Platform SE 6)), you first need to declare the Player (and Person) classes by saying:
    public class Player implements Comparable<Player>
    {...}
    And then you need to implement the compareTo(T o) method by saying:
    public int compareTo(Player o) {
    //compare batting averages
    }
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

Similar Threads

  1. need help urgently!!
    By kimwheeler in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 1st, 2011, 06:22 AM
  2. JAVA Coder required urgently-paid
    By sam_java in forum Paid Java Projects
    Replies: 2
    Last Post: January 15th, 2011, 03:04 AM
  3. Help needed urgently!!!
    By sonia11 in forum Loops & Control Statements
    Replies: 1
    Last Post: October 19th, 2010, 04:19 PM
  4. Reading from a text file. Help needed urgently.
    By TheAirPump in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: December 14th, 2009, 05:16 PM