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

playerData2.txt Anthony 2416 Caleb 73 Christian 94 Daniel 3968 Elijah 3553 Gabri

ID: 3877357 • Letter: P

Question




playerData2.txt

Anthony 2416
Caleb 73
Christian 94
Daniel 3968
Elijah 3553
Gabriel 1870
James 2700
Jaylen 1440
Jordan 3583
Joshua 2388
Justin 3170
Malik 1946
Michael 1754
Tyler 92
Xavier 2584
Aaliyah 452
Alexis 2312
Angel 979
Brianna 2752
Destiny 4991
Gabrielle 2992
Hannah 4830
Isis 3258
Jasmine 4391
Jordan 2720
Kennedy 1760
Laila 4636
Makayla 535
Sydney 4348
Tiana 4347
Dalila 3372
Marlie 1577
Irene 1476
Demetra 2485
Gaynor 2460
Birgit 2387
Aubine 2344
Noel 490
Sarah 1920
Patrica 1541
Karee 1842
Cacilie 3847
Eimile 4974
Kaila 4823
Ileana 800
Olive 2903
Arlina 3427
Verile 1752
Eirena 1517
Lindie 1774
George 702
Cindie 1977
Persis 4611
Buffy 226
Priscilla 3276
Micheline 3032
Trudie 4256
Libbie 4090
Hermina 3559
Millicent 376
Candide 3442
Darline 2159
Clarine 859
Hetty 3205
Dorelia 15
Rosamund 2039
Doralia 4551
Dorisa 1085
Stephine 477
Conni 4696
Lucila 3028
Rubie 3594
Felicle 1075
Zola 265
Ilene 3005
Mildred 4972
Cinnamon 874
Mercy 4044
Bernardine 443
Carie 852

Arrays, Methods and File Processing This lab requires you to write methods to read and manipulate data that is stored in a text file. It is designed to give you more hands on practice with arrays and methods before Classes and Objects are reintroduced. You will also read and write data to a file using exception handling (try/catch block). try t //perform some operation that could cause an exception catch ( SOME EXCEPTION TYPE exception { /Actions to perform if there is an exception Use your programming IDE to create a new project. Name your project PlayerStats. In the main method, declare and instantiate two arrays of size 80. One array to hold the player's name (names) and another array to hold the player's integer high game (scores). These arrays are considered parallel because corresponding elements in the arrays refer to the same player. For example, the high score in scores 15] belongs to the player in names[15]. So, if you ever move a player name to another location in the names array (for example, during sorting), you must also move the score to the same location in the scores array to keep them synchronized. names Before After Kia Adam Caleb Caleb Before After 568 239 450 568 50 39 Kia For each of the following problems, you should modify the main method to invoke the method and show output that proves that your method is working correctly 1. Write a method that will read player names and high scores from the input file into the parallel arrays. The parameters for the method are the two arrays and the input file name (String). The method has a void return type. For this problem, set a breakpoint on the method call and then issue a step-over debugger command. After the step-over command, click on the "Variables tag and inspect the names and scores arrays to see if they contain the expected 80 values. Write a method that will output the parallelarrays to the console. Your output should have two columns: Player Name and High Score. Research the String.format() method in order to get your output in aligned columns. 2.

Explanation / Answer

Copy to code:

package normalClasses;

import java.io.File;

import java.io.FileNotFoundException;

import java.util.Scanner;

public class PlayerStats {

public static void main(String[] args){

//create two array each of 100 size

String playerName[] = new String[100];   

int score[] = new int[100];

//calling readfile function

int noOfPlayer=readFile(playerName, score);  

System.out.println("--------------------Original record--------------------");

//printing details

printDetails(playerName,score,noOfPlayer);

//calling average function

double averageHighScore = calculateAverage(score,noOfPlayer);

System.out.println("Players average high score is :: "+averageHighScore);

//calling sort by score function

sortListByScore(playerName,score,noOfPlayer);

System.out.println("--------------------Record after sort by score--------------------");

printDetails(playerName,score,noOfPlayer); //printing details

//topx will print list of size topx

int topX =10;

printTopXPlayer(playerName,score,topX); //printing details

}

public static int readFile(String playersName[],int playerScore[]){

File file = new File("input.txt"); //reading data from this file

Scanner reader;

int playerIndex=-1; //keep track of number of records

try {

reader = new Scanner(file);

while(reader.hasNext()){

playerIndex++;

String line = reader.nextLine();

playersName[playerIndex] = line.split(" ")[0]; //spliting line by space,0 is player name

playerScore[playerIndex] = Integer.parseInt(line.split(" ")[1]); //spliting line by space,1 is score

}

reader.close();

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return playerIndex;

}

private static double calculateAverage(int[] score, int noOfPlayer) {

double totalScore = 0;

for(int i=0;i<=noOfPlayer;i++){

totalScore+=score[i];

}

return totalScore/noOfPlayer;

}

private static void sortListByScore(String[] playerName, int[] score, int noOfPlayer) {

for (int i = 0; i < noOfPlayer; i++)

{

int min_idx = i;

for (int j = i+1; j <= noOfPlayer; j++)

if (score[j] > score[min_idx])

min_idx = j;

int temp = score[i];

score[i] = score[min_idx];

score[min_idx] = temp;

String temp1 = playerName[i];

playerName[i] = playerName[min_idx];

playerName[min_idx] = temp1;

}

}

public static void printDetails(String[] playerName, int[] score, int playerCount){

System.out.printf("%-5s%s"," ","Players information"); //using printf for format output

System.out.printf(" %-20s%-20s","Player Name","High Score");

for(int i=0;i<=playerCount;i++){

System.out.printf(" %-20s%-20d",playerName[i],score[i]);

}

System.out.println();

}

private static void printTopXPlayer(String[] playerName, int[] score, int topX) {

System.out.println("--------------------Top 10 score players are--------------------");

printDetails(playerName,score,topX);

}

}

Output::

--------------------Original record--------------------
Players information
Player Name High Score   
Anthony 2416   
Caleb 73   
Christian 94   
Daniel 3968   
Elijah 3553   
Gabriel 1870   
James 2700   
Jaylen 1440   
Jordan 3583   
Joshua 2388   
Justin 3170   
Malik 1946   
Michael 1754   
Tyler 92   
Xavier 2584   
Aaliyah 452
Alexis 2312   
Angel 979
Brianna 2752   
Destiny 4991   
Gabrielle 2992   
Hannah 4830   
Isis 3258   
Jasmine 4391   
Jordan 2720   
Kennedy 1760   
Laila 4636   
Makayla 535
Sydney 4348   
Tiana 4347   
Dalila 3372   
Marlie 1577   
Irene 1476   
Demetra 2485   
Gaynor 2460   
Birgit 2387   
Aubine 2344   
Noel 490
Sarah 1920   
Patrica 1541   
Karee 1842   
Cacilie 3847   
Eimile 4974   
Kaila 4823   
Ileana 800
Olive 2903   
Arlina 3427   
Verile 1752   
Eirena 1517   
Lindie 1774   
George 702
Cindie 1977   
Persis 4611   
Buffy 226
Priscilla 3276   
Micheline 3032   
Trudie 4256   
Libbie 4090   
Hermina 3559   
Millicent 376
Candide 3442   
Darline 2159   
Clarine 859
Hetty 3205   
Dorelia 15   
Rosamund 2039   
Doralia 4551   
Dorisa 1085   
Stephine 477
Conni 4696   
Lucila 3028   
Rubie 3594   
Felicle 1075   
Zola 265
Ilene 3005   
Mildred 4972   
Cinnamon 874
Mercy 4044   
Bernardine 443
Carie 852
Players average high score is :: 2473.7974683544303
--------------------Record after sort by score--------------------
Players information
Player Name High Score   
Destiny 4991   
Eimile 4974   
Mildred 4972   
Hannah 4830   
Kaila 4823   
Conni 4696   
Laila 4636   
Persis 4611   
Doralia 4551   
Jasmine 4391   
Sydney 4348   
Tiana 4347   
Trudie 4256   
Libbie 4090   
Mercy 4044   
Daniel 3968   
Cacilie 3847   
Rubie 3594   
Jordan 3583   
Hermina 3559   
Elijah 3553   
Candide 3442   
Arlina 3427   
Dalila 3372   
Priscilla 3276   
Isis 3258   
Hetty 3205   
Justin 3170   
Micheline 3032   
Lucila 3028   
Ilene 3005   
Gabrielle 2992   
Olive 2903   
Brianna 2752   
Jordan 2720   
James 2700   
Xavier 2584   
Demetra 2485   
Gaynor 2460   
Anthony 2416   
Joshua 2388   
Birgit 2387   
Aubine 2344   
Alexis 2312   
Darline 2159   
Rosamund 2039   
Cindie 1977   
Malik 1946   
Sarah 1920   
Gabriel 1870   
Karee 1842   
Lindie 1774   
Kennedy 1760   
Michael 1754   
Verile 1752   
Marlie 1577   
Patrica 1541   
Eirena 1517   
Irene 1476   
Jaylen 1440   
Dorisa 1085   
Felicle 1075   
Angel 979
Cinnamon 874
Clarine 859
Carie 852
Ileana 800
George 702
Makayla 535
Noel 490
Stephine 477
Aaliyah 452
Bernardine 443
Millicent 376
Zola 265
Buffy 226
Christian 94   
Tyler 92   
Caleb 73   
Dorelia 15   
--------------------Top 10 score players are--------------------
Players information
Player Name High Score   
Destiny 4991   
Eimile 4974   
Mildred 4972   
Hannah 4830   
Kaila 4823   
Conni 4696   
Laila 4636   
Persis 4611   
Doralia 4551   
Jasmine 4391   
Sydney 4348   

note: please include your input.txt file in the same folder in which main java class file there.