Solved

please help

Posted on 2006-07-21
10
200 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 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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Problem to Alipay 10 95
Eclipse Help Java EE 5,6,7 Documentation, why not Java EE 8 8 70
collection output issue 9 92
spring maven example issues 3 104
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…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

738 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