Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

please help

Posted on 2006-07-21
10
Medium Priority
?
202 Views
Last Modified: 2010-03-31

i asked this previous n u asked me to try myself n come back f stuck,well here i am
Q


Write a Java program that creates a number of Player objects (with ‘name’, ‘points’, and ‘rank’ properties) and assigns them random points.

Sort the players and populate the ‘rank’ property based on the player’s points.”


The sorting of the rankings should be dynamic.


Provide unit tests that exercise your solution.

Provide a short explanation of your code.


For extra credit:

Provide a timer that assigns further points to each player and as a consequence the player’s rankings change.

This timer should be off by default.




stuck on


for(int name=0;name<number;name++){
            Player[name]   = new Player();
            int[] xpoints =  Player[name].getpoints();
      }



      Arrays.sort(xpoints);

can set up object of arrays but get error also dont know how to set rankings
your help would b appreciated cheers
0
Comment
Question by:tadgh
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
10 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 17155544
First, you need to create the array:

Player[] players   = new Player[name];
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 172 total points
ID: 17155562
In the loop, you can then do


for(int name=0;name<number;name++){
          players[name]   = new Player();
          players[name].setPoints((int)(Math.random() * 100));
}
0
 

Author Comment

by:tadgh
ID: 17155584
no sorry have points awarded already just wanna sort it and populate rank field ??

public class Player {


      String name;
      int rank=0;
      int points;




      Player(){//constructor taking in the name on creation
      System.out.print("Enter Player Name : ");


            //  open up standard input
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));


            try {
              name = br.readLine();
            } catch (IOException ioe) {
               System.out.println("IO error trying to read your name!");
               System.exit(1);
            }


      Random random = new Random();
      points = random.nextInt();
      }


    public void display(){

            System.out.println("The Player Name : "+name);
            System.out.println("The Player's points is: " + points);
            System.out.println("The Player rank is: " + rank);

            System.out.println("***************************************");
      }

      void getpoints(int newPoints){
                  points = newPoints;
            }



      //******************************************************************

    public static void main(String[] args){//main
      int number = 0;
      String temp;

      try
                  {

    System.out.print("How many players wanted : ");
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));


              temp = br.readLine();
                  number = Integer.parseInt(temp);
            } catch (IOException ioe) {
               System.out.println("IO error trying to read number!");
               System.exit(1);
            }


Player[] Player  = new Player[number];

      for(int name=0;name<number;name++){
            Player[name]   = new Player();
            int[] xpoints =  Player[name].getpoints();
      }



      Arrays.sort(xpoints);

      for(int name=0;name<number;name++){

                  Player[name].display();
            }







      }

}//end Player
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Assisted Solution

by:RoyalNepal
RoyalNepal earned 164 total points
ID: 17155764
/////////////////////Your code//////////////////

for(int name=0;name<number;name++){
          Player[name]   = new Player();
          int[] xpoints =  Player[name].getpoints();
     }
     Arrays.sort(xpoints);
////////////////////////////////////////////////////
Comment: xpoints is out of scope..

Solution:

int[] xpoints =null;

for(int name=0;name<number;name++){
          Player[name]   = new Player();
          xpoints =  Player[name].getpoints();
     }
     Arrays.sort(xpoints);

it should work....
0
 

Author Comment

by:tadgh
ID: 17156628
no help sorry. I think my logic is wronng ,what would be the best avenue to take to solve this?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17156741
>> int[] xpoints =  Player[name].getpoints();

Does each player have a 'set' of points?
0
 
LVL 2

Expert Comment

by:RoyalNepal
ID: 17157000
Have you the value for each player?? because when you instantiate objects like player = new player()....it sets the default value. So...if u havent set the value...it wont sort it properly.

0
 
LVL 13

Assisted Solution

by:Webstorm
Webstorm earned 164 total points
ID: 17166124
Hi tadgh,

You should directly sort the players array :
    Arrays.sort(players, new Comparator(){
              public boolean equals(Object obj) { return false; }

              public int compare(Object o1,Object o2) // <-- compare two player objects
              {
                   return ((Player)o1).points-((Player)o2).points;
              }
        });

then ranking number is the index of each element in the array (don't need additionnal member variable).

see :
   http://java.sun.com/j2se/1.4.2/docs/api/java/util/Arrays.html
   http://java.sun.com/j2se/1.4.2/docs/api/java/util/Comparator.html

0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question