Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

HELP WITH JAVA PLEASE! Instructions from Absolute Java 5th Edition by Walter Sav

ID: 665641 • Letter: H

Question

HELP WITH JAVA PLEASE!

Instructions from Absolute Java 5th Edition by Walter Savage:

Write a class that keeps track of the top five high scores that could be used for a video game. Internally, the class should store the top scores in a data structure of your choice (the most straightforward way is to use arrays). Each entry consists of a name and a score. The data stored in in memory should be synchronized with a text file for persistent storage. For example, here are the contents of a sample file were Ronaldo hasthe highest score and Pele has the third highest score:

Ronaldo

10400

Didier

9800

Pele

9750

Kaka

8400

Cristiano

8000

The constructor should test if the file exists. If it does not exist, then the file hsould be created with blank names for each of the players and a sscore of 0. If the file does exist, then the data from the file should be read into the class's instance variables. Along with appropriate constructors, accessors, and mutators, add the following methods:

void playerScore(String name, int score) - Whenever a game is over, this method is called with the player's name and ifnal score. If the name is one of hte top five, then it should be added to the list and the lowest score shoiuld be dropped out. If the score is not in the top five, then nothing happens.

String[] getTopNames() - Returns an array ofthe names of the top players, with the top player first, the second best player second, etc.

int[] getTopScores() - Returns an array of the scores of the top players, with the highest score first, the second highest score second, etc.

Test your program with several calls to playerScore and print out the list of top names and scores to ensure that the correct values are stored. When the program is restarted, it should remember the top scores from the last session.

Explanation / Answer

Answer:

/***************************************************************

******JAVA PROGRAM FOR STORE TOP FIVE PLAYER SCORE AND NAME*****

***************************************************************/

import java.iio.*;

import java.util.*;

import java.lang.*;

class GAMECLASSDRIVER

{

     public static void main (String[] args)

     {

          GAMECLASS newGAMECLASS=new GAMECLASS("GAMESCORES.txt");

          newGAMECLASS.playerScore("NewPlayer",1000);

          String[] TOPPLAYERNAME=getTopNames();

          int[] TOPPLAYERSCORE=getTopscores();

          for(int k1=0;k1<TOPPLAYERNAME.length;k1++)

          {

              system.out.println("PLAYER NAME:"+TOPPLAYERNAME[i]);

              system.out.println("PLAYER SCORE:"+TOPPLAYERSCORE[i]);

          }

     }

}//DRIVER CLASS ENDS

//class GAMECLASS

class GAMECLASS   

{

//DECLARE PRIVATE VARIABLES

     String[] GAMEnames;

     int[] GAMEscores;

//MAXIMUM SIZE OF THE ARRAYS

     final int GAMETop = 5;

//CONSTRUCTOR

     GAMECLASS(String a_NEWFILENAME) throws IOExeception

     {

          GAMEscores = new int[GAMETop];

          GAMEnames = new String[GAMETop];

         

          File a_newFILE = new File(a_NEWFILENAME);

          try

          {

              //SET SCANNER TO READ VALUES FROM FILE

              Scanner inputSCAN = new Scanner(a_newFILE);

              //UNTIL THE REACH ARRAY END READ VALUES FROM FILE AND STORE IT IN VARIABLES

              for (int k1=0; k1 <GAMEscores.length; k1++)

              {

                   if (inputSCAN.hasNext())

                   {

                        GAMEnames[k1] = inputSCAN.next();

                        if (inputSCAN.hasNextInt())

                        {

                             GAMEscores[k1] = inputSCAN.nextInt();

                             if (GAMEscores[k1] == 0 && GAMEnames[k1].equals(" "))

                             {

                                  inputSCAN.close();

                                 

                             }

                        } else

                        {

                             inputSCAN.close();

                            

                        }

                   }

                   else

                   {

                        inputSCAN.close();

                       

                   }

              }

              inputSCAN.close();

             

          }

          catch (IOException ioexe)

          {

              for (int k1=0; k1 < GAMEscores.length; k1++)

              {

                   GAMEnames[k1] = "";

                   scores[k1] = 0;

              }

          }

     }

     //STORE THE PLAYER NAME , SCORE IN ARRAY IF SCORE IS GREATER THAN ANY TOP 5 VALUES

     //SWAP OUT THE LEAST SCORE FROM THE ARRAY TO MAKE SPACE FOR THE NEW SCORE AND NAME

     public void playerScore(String GAMEname, int GAMEscore)

     {

          int a_index12 = 0;

          while (GAMEscore <= GAMEscores[a_index12] && a_index12 < GAMEscores.length) {

              a_index12++;

          }

          if (a_index12 < GAMEscores.length)

          {

              for (int k1 = GAMEscores.length - 1; k1 > a_index12; k1--)

              {

                   GAMEscores[k1] = GAMEscores[k1 - 1];

                   GAMEnames[k1] = GAMEnames[k1 - 1];

              }

              GAMEscores[a_index12] = GAMEscore;

              GAMEnames[a_index12] = GAMEname;

          }

          SAVEGAMESCORE();

          return;

     }

     // RETURNS TOP PLAYERS NAMES.

     public String[] getTopNames()

     {

          return GAMEnames;

     }

     // RETURNS TOP PLAYERS SCORES.

     public int[] getTopScores()

     {

          return GAMEscores;

     }

     //SAVE THE UPDATED SCORE IN FILE

     public void SAVEGAMESCORE()throws IOExeception

     {        

          try

          {

              File NEWfile = new File("GAMESCORES.txt");

              PrintWriter PRINToutput= new PrintWriter(NEWfile);

              for (int k1 = 0; k1 < GAMEscores.length; k1++)

              {

                   PRINToutput.println(GAMEnames[k1] );

                   PRINToutput.println( GAMEscores[k1]);

              }

              //CLOSE THE PRINTWRITER

              PRINToutput.close();

          }

          catch (Exception ioexe)

          {

              System.out.println("NOT ABLE TO STORE THE UPDATED SCORE.");

          }

     }

}