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

The following data have been stored in a text file called “scores.txt”. 101,John

ID: 3846941 • Letter: T

Question

The following data have been stored in a text file called “scores.txt”.

101,John,200

102,Lisa,175

103,Mary,205

104,Sonu,190

Each line in that text file contains participant id, participant name and participant’s score in a Game. Complete the missing lines of codes in the following AverageScore class. The AverageScore class has to open the “scores.txt” file, read the Score details one by one, find out the average score and print it appropriately.Java Object Orientated

import java.io.FileReader;

import java.util.NoSuchElementException;

import java.util.Scanner;

import java.util.StringTokenizer;

class GameScore{

    int participantId;

    String name;

    Int score;

public GameScore() {

}

public GameScore(int participantId, String name, int score) { this.participantId = participantId;

this.name = name;

this.score = score;

}

public int getParticipantId() { return participantId;

}

public void setParticipantId(int participantId) { this.participantId = participantId;

}

public String getName() { return name;

}

           

public void setName(String name) { this.name = name;

}

public int getScore() { return score;

}

public void setScore(int score) { this.score = score;

}

@Override

public String toString() { return "Participant Id : " + participantId + ", Name : " + name + ", Score : " +

score + ' ';

}

}

public class AverageScore {

public static void main(String[] args) throws java.io.IOException

{

// opening the file for reading

FileReader reader = new FileReader("scores.txt");

Scanner in = new Scanner(reader);

int numGameScores = 0; double totalScores = 0.0;

while(in.hasNextLine())

{

// String variable to read one line from the file String strGameScore;

// reading GameScore details from each line   strGameScore = in.nextLine();

//Creating tokens

StringTokenizer>

String strParticipantId;

String strName;

String strScore;

           

                                                                                         (Question 5 continued next page)

                                                                                 Page    of

Question 5 (continued)

while(OneLineTokens.hasMoreTokens())

{

int tempScore;

try

{

//Missing lines of code to be completed

//Convert the string value to appropriate data type

//Assign suitable value to tempScore

totalScores += tempScore;

    numGameScores++;

}

catch(NoSuchElementException e)

{

System.out.println("Error in reading file content");

}

} // end of inner while } // end of outer while in.close();

// printing

If (numGameScores > 0)

System.out.printf("Average score is %.2f", totalScores/numGameScores); else

System.out.println(“The file does not contain any scores”); }// end of main

}// end of class

Explanation / Answer

Sharing the complete code. Just change the path of score.txt

Code:

import java.io.FileReader;

import java.util.NoSuchElementException;

import java.util.Scanner;

import java.util.StringTokenizer;

class GameScore{

    int participantId;
    String name;
    int score;

   public GameScore() {
  
   }
   public GameScore(int participantId, String name, int score)
   {
   this.participantId = participantId;
   this.name = name;
   this.score = score;
   }
  
   public int getParticipantId() {
       return participantId;
       }
  
   public void setParticipantId(int participantId) {
       this.participantId = participantId;
       }
  
   public String getName() {
       return name;
       }
  
   public void setName(String name) {
       this.name = name;
       }
  
  
  
   public int getScore() {
       return score;
       }
  
  
  
   public void setScore(int score) {
       this.score = score;
       }
  
  
  
   @Override  
   public String toString() {
       return "Participant Id : " + participantId + ", Name : " + name + ", Score : " +score + ' ';

}
}

public class AverageScore {

   public static void main(String[] args) throws java.io.IOException
  
   {
  
   // opening the file for reading
   FileReader reader = new FileReader("C:\Users\ADMIN\workspace\testnew\src\chegg\score.txt");
   Scanner in = new Scanner(reader);
   int numGameScores = 0; double totalScores = 0.0;
   while(in.hasNextLine())
   {
   // String variable to read one line from the file String strGameScore;
   // reading GameScore details from each line   strGameScore = in.nextLine();
   //Creating tokens
   String strGameScore=in.nextLine();
   StringTokenizer StringTokenizer(strGameScore,",");
  
   String strParticipantId;
   String strName;
   String strScore;
  
   while(OneLineTokens.hasMoreTokens())
   {
   int tempScore;
   try
   {
       strParticipantId=OneLineTokens.nextToken();
       strName=OneLineTokens.nextToken();
       strScore=OneLineTokens.nextToken();
       tempScore=Integer.parseInt(strScore);
       System.out.println(strParticipantId+" "+strName+" "+strScore);
       totalScores += tempScore;
        numGameScores++;
        System.out.println(numGameScores);
   }
  
       catch(NoSuchElementException e)
       {
       System.out.println("Error in reading file content");
       }
   } // end of inner while
   } // end of outer while in.close();
  
   // printing
   if (numGameScores > 0)
   System.out.printf("Average score is %.2f", totalScores/numGameScores);
   else
   System.out.println("The file does not contain any scores");
   }// end of main

}// end of class

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote