import java.util.*; //add to it: implements comparable: public class PlayerScore implements Comparable{ //class variables: private static int numScores = 0;//keeps track of the number of PlayerScore objects created private static int topScore = 0; //stores teh highest score entered so far private static Scanner stdIn = new Scanner(System.in); //instance variables: private String name;//store's the player's name private int score; //stores the player's score //constructor: public PlayerScore(String name, int score){ this.name = name; this.setScore(score); PlayerScore.incNumberScores(); } //instance methods: public void setScore(int score){ //takes an integer score, sets variables and checks topScore this.score = score; this.checkAndSetTopScore(); } public int getScore(){ //returns the score return this.score; } public String getName(){ //returns the name of the player return this.name; } public void checkAndSetTopScore(){ //compares this.score to the static topScore and call appropriate actions if (PlayerScore.isTopScore(this.score)){ PlayerScore.topScore = this.score;//static, use classname to set } } public String toString(){ //returns a representation of this instance return "Player Name: " + this.name + ", Score: " + this.score; } //class methods: public static void incNumberScores(){ //increments static number of score objects PlayerScore.numScores++; } public static int getNumberScores(){ //returns the number of scores created return PlayerScore.numScores; } public static int getTopScore(){ //returns the top score seen so far return PlayerScore.topScore; } public static boolean isTopScore(int score){ //checks whether the passed in score is greater than the static topScore return (score > PlayerScore.topScore); } //******* Need to add a compareTo method public int compareTo (PlayerScore other){ if (this.score > other.getScore()){ return 1; } else if (this.score < other.getScore()){ return -1; } else { return this.name.compareTo(other.name); } } //First thing to check is score, second is names public static void main (String [] args){ //main method to test class: //1. prompt user to enter a name and score: PlayerScore ps1 = new PlayerScore(PlayerScore.getNameFromUser(), PlayerScore.getScoreFromUser()); PlayerScore ps2 = new PlayerScore(PlayerScore.getNameFromUser(), PlayerScore.getScoreFromUser()); PlayerScore ps3 = new PlayerScore(PlayerScore.getNameFromUser(), PlayerScore.getScoreFromUser()); //2. print score of first player: System.out.println("\nThe first player's score is: " + ps1.getScore() + "\n"); //3. print number of scores: System.out.println(PlayerScore.getNumberScores() + " scores have been entered"); //4. print top score: System.out.println("\nThe top score is: " + PlayerScore.getTopScore()); //5. change first player's score to 7823 ps1.setScore(7823); //6. print the names and scores (using toString) for each of the 3 objects: System.out.println(ps1); System.out.println(ps2); System.out.println(ps3); //7. Print the topScore to the screen: System.out.println("\nThe top score is: " + PlayerScore.getTopScore()); } //other methods: public static String getNameFromUser(){ //promp user for a name System.out.print("Enter the player name: "); String nm = PlayerScore.stdIn.nextLine(); return nm; } public static int getScoreFromUser(){ //prompts user for a score and clear input stream System.out.print("Enter score: "); int sc = PlayerScore.stdIn.nextInt(); PlayerScore.stdIn.nextLine();//clears input return sc; } }