Solved

please help

Posted on 2006-07-21
10
193 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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
reasons why a "inside Menu" would not appear for a web server 6 48
Java and GPO 11 47
eclipse formatting 6 43
maven project error 5 24
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…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

744 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

10 Experts available now in Live!

Get 1:1 Help Now