4 Replies - 4462 Views - Last Post: 11 April 2009 - 12:37 PM Rate Topic: -----

#1 mrfinch  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 09-April 09

"Implement a new Constructor that passes a Comparator."

Posted 09 April 2009 - 09:30 AM

Hello, this is my first question here -- long time lurker though :) hope you can help.

I am writing a program that is a basic cricket club roster, and have been asked to "Implement a new Constructor for the Club class that passes a Comparator."

I have a class called "BattingAverageSorter" that does the following:
import java.util.Comparator;

public class BattingAverageSorter implements Comparator<Player>{
	public int compare(Player p1, Player p2){
		if(p1.getBattingAv() < p2.getBattingAv())
		{
			return -1;
		}
		else if(p1.getBattingAv() > p2.getBattingAv())
		{
			return 1;
		}
		else
		{
			return 0;
		}
	}
}


Which works with: Collections.sort(playerList, new BattingAverageSorter()); fine.

I don't quite understand what I am being asked. I have googled around trying to read up around it and even looked in the books/notes I have and still can't find anything.

Tried to do:
private List<Player> playerList = new ArrayList<Player>(new BattingAverageSorter());


But Eclipse gives me an error. Is there any where I can be pointed toward to understand what I am being asked?

This post has been edited by mrfinch: 09 April 2009 - 10:01 AM


Is This A Good Question/Topic? 0
  • +

Replies To: "Implement a new Constructor that passes a Comparator."

#2 markhazlett9  Icon User is offline

  • Coding is a lifestyle
  • member icon

Reputation: 60
  • View blog
  • Posts: 1,666
  • Joined: 12-July 08

Re: "Implement a new Constructor that passes a Comparator."

Posted 09 April 2009 - 09:46 AM

First of all, welcome to DIC. Glad to see you're choosing DIC as your source for programming help!


Can you please explain what you're being asked? I don't see anywhere in your explanation where it explains that you need to be using an array list. What is your program supposed to do in other words? This is just so that we can understand what you're doing a little better so we can help you out a little more. Cheers
Was This Post Helpful? 0
  • +
  • -

#3 mrfinch  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 09-April 09

Re: "Implement a new Constructor that passes a Comparator."

Posted 09 April 2009 - 09:58 AM

View Postmarkhazlett9, on 9 Apr, 2009 - 08:46 AM, said:

First of all, welcome to DIC. Glad to see you're choosing DIC as your source for programming help!


Can you please explain what you're being asked? I don't see anywhere in your explanation where it explains that you need to be using an array list. What is your program supposed to do in other words? This is just so that we can understand what you're doing a little better so we can help you out a little more. Cheers


Thank you! Sure, sorry for not doing so.

What I have to do is: Write a basic program that is based around a cricket team. Have to store certain (relevant) information in Player, Person, Bowler and/or WicketKeeper classes.

For example, I add a Bowler with it's information and by using inheritance it stores the relevant information in each class per object.

I have to "add" players to the club (which is where the ArrayList comes in) and sort them on add by last name (which is done)
I do this by:
// fName, lName, Age, Height, Weight, Games Played,  Runs Scored, Amount Dismissed, Balls Bowled, Wickets Taken, Runs Conceded
Bowler rob = new Bowler("Rob", "Finch", 47, 2.0, 70, 0, 314, 3, 0, 0, 0);

club.addPlayer(rob);
		
// Puts the players is another List so as not to give a carbon-copy of the master List
List<Player> playerList = new ArrayList<Player>(club.getPlayers());



If you print playerList it'll sort by lastname, firstname.
If you do Collections.sort(playerList, new BattingAverageSorter()); it'll sort with the class posted in the first post, which is is by lowest batting average first.

What I have to do next is:

Quote

Write a Comparator class that compares players by batting average (number of runs scored divided by no of times dismissed). Implement a new Constructor for the Club class that passes a Comparator. Hence write a main program 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 main program without modifying the code in any of your classes.


I have been told that an ArrayList might be the best method, I was originally using a SortedMap. The bit I am most confused about is:

Quote

Implement a new Constructor for the Club class that passes a Comparator.

Was This Post Helpful? 0
  • +
  • -

#4 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5761
  • View blog
  • Posts: 12,574
  • Joined: 16-October 07

Re: "Implement a new Constructor that passes a Comparator."

Posted 09 April 2009 - 10:13 AM

Just guessing. Sounds like you might be looking for something like:
class Club extends ArrayList<Player> {
	private Comparator<Player> comp;
	
	public Club(Comparator<Player> comp) {
		this.comp = comp;
	}
	
	public void sort() { 
		Collections.sort(this, this.comp);
	}
}



Also, for that compare, depending on the type returned, you may be able to get away with:
public class BattingAverageSorter implements Comparator<Player>{
	public int compare(Player p1, Player p2){
		return p1.getBattingAv().compareTo(p2.getBattingAv());
	}
}



To go the other direction, multiply by -1.
Was This Post Helpful? 0
  • +
  • -

#5 mrfinch  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 09-April 09

Re: "Implement a new Constructor that passes a Comparator."

Posted 11 April 2009 - 12:37 PM

Hey guys, thanks for all the help. I managed to understand what i needed to do :)

For future ref ... my constructors are now:
// Constructor
public Club(String clubName)
{
	this(clubName, null);
}

// Constructor with a comparator
public Club(String clubName, Comparator<Player> sorter)
{
	this.clubName = clubName;
	this.sorter = sorter;
}


With 'sorter' pointing to a certain comparator class.


Once again thank you :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1