Solved

please help

Posted on 2006-07-21
10
194 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

910 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now