Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The question is in the picture Please write a program in Java to sort and search the scores for the participants in a game. The

The question is in the picture

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed
Please write a program in Java to sort and search the scores for the participants in a game. The public interface of ScoreManager is described below, along with a ready-to-use class ScoreRecord. Please follow the requirements in the description for each method to complete this class. Please also write a tester class to thoroughly test the four methods given in ScoreManager. The ScoreManager class should contain the following public methods. 1. public ArrayList readRecords (String pathName) throws IOException: . Read all the records from the binary file in the local file system as specified by the parameter pathName and return an ArrayList of ScoreRecord. The definition of ScoreRecord class is provided below. . We assume that each record in the file has a fixed length of 50 bytes, with name stored in the first 46 bytes and score stored in the following 4 bytes. We assume that all the scores are non-negative integers. There could be an arbitrary number of records in the file. A sample binary file containing 10 records is attached to this handout. 2. public ArrayList mergeSortRecords (ArrayList inputList) : Use the merge sort algorithm to sort all the records in inputList by score in descending order. . Use System. out . println () to output the intermediate results in each step during the merging process. For example, if at one step, two lists [ (Bill, 78) , (Tiffany, 69) , (Bob, 65) ] and [ (John, 90) ] are merged, the output should be Intermediate result: [ (John, 90) , (Bill, 78) , (Tiffany, 69) , (Bob, 65) ]. One line per result. 3. public void saveScores (String pathName, ArrayList sortedList) throws IOException: . Use RandomAccessFile to save all the ordered records from the sortedList to the given pathName on the disk.4. public ArrayList scoreSearch(String pathName, int minScore, int maxScore) throws IOException: 0 Given a score range [minSCore,maXScore] (both boundaries are inclusive) and the pathName of a le that stores records sorted by score (as produced by saveScores), use binary search to find and return an ArrayList of all the records within this score range without loading all records in the file into memory (i.e., do NOT attempt to read all records into an array/ArrayList and then perform the search there). For example, given the range [69, 80] and the file which contains [ (John, 90) , (Bill, 78), (Tiffany, 69), (Bob, 65) ], it should return [ (Bill, 78), (Tiffany, 69)]. If no records fall in the score range, return an empty list. To accomplish this, you can adapt the binary search algorithm (for arrays) taught in class to the setting of random access files. To retrieve records in a range, you may use binary search to locate the record with the given minScore within the range (or if such a record does not exist, the rst record with a score higher than minScore in the file) and then visit the subsequent records until the score is out of the given range or the end of the file is reached. 0 Use System.out .println () to output each record visited during the search, in the format of Search visit: (Bill,78) One line per record. Tms: 1. Use RandomAccessFile().seek(long pos) to do random access; 2. Use RandomAccessFile().length() to get the size of file (number of bytes). ScoreRecord.java fat-2% * A score record with a participant name and score. */ public class ScoreRecord{ private String name; private int score; /** * Constructs a score record. * @param name the name of the participant * @param score the score of the participant */ public ScoreRecord(String name, int score) { this.name = name; this.score = score; } /** * Gets the name of the participant * @return the name */ public String getNameO { return name; } /** * Gets the score of this record * @return the score */ public int getScoreO { return score; } /** * Returns a string representation of (name, score) pair * @return the string representation of this record in the format of (name, score) * / public String toString() { return "(" + name + ", " + score + ") "; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions