Solved

please help

Posted on 2006-07-21
10
199 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
  • 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 43 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 41 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 41 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

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…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

749 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