...

View Full Version : JAVA compare() help?



VietBoyVS
07-04-2004, 07:21 PM
Here is my code..

How would I use to compare the blocks with highest also with by name too?
I'm kind stuck so far.. Any help??



import java.util.*;

public class NBA{

private ArrayList playerList = new ArrayList();
public NBA(BPlayer a){
playerList.add(a);
}

//print out all the players in alphetical order
public void printPlayer(){
Collections.sort(playerList);
Iterator teamItr = playerList.iterator();
while(teamItr.hasNext()){
BPlayer curTeam = (BPlayer) teamItr.next();
System.out.println((BPlayer)curTeam);
}
}

//print blocks with highest first with that, also name is by alphetical too
public void printPlayerBlocks(){

}
}
--------------------------------------------------------------------------
import java.util.*;

public abstract class BPlayer implements Comparable,Comparator{
private String name;
private int gPlays;


//model a basketball player
public BPlayer(String name, int gPlays){
this.name = name;
this.gPlays = gPlays;

}

//Get name
public String getName(){
return this.name;
}

//Get number of games plays
public int getPlays(){
return this.gPlays;
}

public int compareTo(Object otherPlayer){
return getName().compareTo(((BPlayer)otherPlayer).getName());
}

public String toString(){
String player = ":PLAYER: :Name: " +getName()+ " :PLAYS: " +getPlays();
return player;
}



//////////////////////
public int compare(Object obj1, Object obj2){
int number = 0;
if (obj1 instanceof BPlayer && obj2 instanceof BPlayer){


}
return number;
}
//////////////////////

}
--------------------------------------------------------------------------
public class BForward extends BPlayer {
private int blocks;

public BForward(String name, int gPlays, int blocks){
super(name,gPlays);
this.blocks = blocks;
}

//get number of blocks
public int getBlocks(){
return this.blocks;
}

//compare two bforward object, and by last name
public int compareTo(Object otherPlayer){
return getName().compareTo(((BForward)otherPlayer).getName());
}

public String toString(){
String player = ":PLAYER: :Name: " +getName()+ " :PLAYS: " +getPlays()+ " :BLOCKS: " +getBlocks();
return player;
}


//compare by last name of bforward and by blocks, highest block first then by last name
public int compare(Object obj1, Object obj2){
int number = 0;
if (obj1 instanceof BForward && obj2 instanceof BForward){


}
return number;
}

}
--------------------------------------------------------------------------

Spookster
07-04-2004, 07:25 PM
How would I use to compare the blocks with highest also with by name too?


What?

VietBoyVS
07-05-2004, 12:11 AM
I want to compare the player with highest number of blocks to print out first, also print in alphectical by names too.

Some how I have to use the Comparator class.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum