Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Jan 2004
    Posts
    61
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question JAVA compare() help?

    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??

    Code:
    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; 
      }
      
    }
    --------------------------------------------------------------------------

  • #2
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,278
    Thanks
    4
    Thanked 83 Times in 82 Posts
    Quote Originally Posted by VietBoyVS
    How would I use to compare the blocks with highest also with by name too?

    What?
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #3
    New Coder
    Join Date
    Jan 2004
    Posts
    61
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Cool

    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •