Baseball statistics sorting, addition to the old code. Following is what I have right now what more specifications...

50.1K

Verified Solution

Question

Programming

Baseball statistics sorting, addition to the oldcode.

Following is what I have right now what morespecifications of the pseudocode

I need addition to the program for the followingpseudocode:

Step 10 // sort the team[] on increasing order of Number This isan invocation of the method selectionSort() with the parametersteam[] and team size. 5 CSC 156 - Assignment 9 Baseball

Step 11) // display Player’s Number, Hits, Walks, Outs This isno different than the invocation of method displayArray() from step9. Now we’ll discuss the code necessary for the missing methodselectionSort().

Follow the Pseudo-code for the selectionSort()method

Step 0) // declare local variables Most variables that are notparameters are indices of for statements. You will need a local intvariable named smallestIndex to update the position of the k’thsmallest Player Number and a Player variable named temp for swapswithin team[] positions.

Step 1) // loop over Index from 0 to team size-2 This is a forstatement over the available int values. Index will contain thearray position for the k’th smallest Player Number in thearray.

Step 2) // save Index to smallestIndex This assignment assumesthat the k’th smallest Player Number is in the correctposition.

Step 3) // loop over minIndex from Index+1 to team size-1 Thisis a search for the k’th smallest Player Number through theremainder of the team[] array.

Step 4) // if team[minIndex]’s Number < team[smallestIndex]’sNumber This is an if statement to test if a new k’th smallestPlayer Number has been found.

Step 4a) // save minIndex to smallestIndex This assignmentconfirms that a new candidate for the k’th smallest Player Numberhas been found so save the location in smallestIndex.

Step 5) // if Index not equal to smallestIndex This is an ifstatement to avoid any swaps that are not necessary.

Steps 5a)-5c) // save team[index] to temp // saveteam[smallestIndex] to team[index] // save temp toteam[smallestIndex] This is the exchange of data between the arrayelements team[Index] and team[smallestIndex] that places the k’thsmallest Player Number into the correct position.

/*****************************Player.java*******************************/


public class Player {

   /*
   * data field
   */
   private int playerNumber;
   private int numberOfWalks;
   private int numberOfHits;
   private int numberOfOuts;

   /**
   * no arg constructor
   */
   public Player() {
       this.playerNumber = 0;
       this.numberOfWalks = 0;
       this.numberOfHits = 0;
       this.numberOfOuts = 0;
   }

   /**
   *
   * @param playerNumber
   * @param numberOfWalks
   * @param numberOfHits
   * @param numberOfOuts
   */
   public Player(int playerNumber, int numberOfWalks, intnumberOfHits, int numberOfOuts) {
       super();
       this.playerNumber =playerNumber;
       this.numberOfWalks =numberOfWalks;
       this.numberOfHits =numberOfHits;
       this.numberOfOuts =numberOfOuts;
   }

   /*
   * getter and setter
   */
   public int getPlayerNumber() {
       return playerNumber;
   }

   public void setPlayerNumber(int playerNumber){
       this.playerNumber =playerNumber;
   }

   public int getNumberOfWalks() {
       return numberOfWalks;
   }

   public void setNumberOfWalks(int numberOfWalks){
       this.numberOfWalks =numberOfWalks;
   }

   public int getNumberOfHits() {
       return numberOfHits;
   }

   public void setNumberOfHits(int numberOfHits){
       this.numberOfHits =numberOfHits;
   }

   public int getNumberOfOuts() {
       return numberOfOuts;
   }

   public void setNumberOfOuts(int numberOfOuts){
       this.numberOfOuts =numberOfOuts;
   }

   @Override
   public String toString() {
       return playerNumber + \"\t\" +numberOfWalks + \"\t\" + numberOfHits + \"\t\" + numberOfOuts;
   }

}

/******************************Baseball8.java**************************/

import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;

public class Baseball8 {

   static Player[] players = new Player[20];

   public static void main(String[] args) {
       int i = 0;
       try {
           /*
           * read line byline
           * using splitmethod create objects of Player
           */
           Scanner sc = newScanner(new FileReader(\"baseball.txt\"));
          sc.nextLine();
           while(sc.hasNext()) {
              String str = sc.nextLine();
              int playerNumber = Integer.parseInt(str.split(\"\")[0]);
              int numberOfWalks = Integer.parseInt(str.split(\"\")[1]);
              int numberOfHits = Integer.parseInt(str.split(\"\")[2]);
              int numberOfOuts = Integer.parseInt(str.split(\"\")[3]);
              players[i] = new Player(playerNumber,numberOfWalks, numberOfHits, numberOfOuts);
              i++;
           }
       } catch (IOException e) {
          System.out.println(\"File not found!\");
       }

      System.out.println(\"Player\tHits\tWalks\tOuts\");
       for (Player player : players){
           try {
              System.out.println(player.toString());
           } catch(Exception e) {

           }

       }
       /*
       * find the player
       * print the details
       * if you want to edit playerdetail
       * just call setters
       */
       System.out.print(\"Enter the numberof player:\");
       Scanner scan = newScanner(System.in);
       int number = scan.nextInt();
       int indexOfPlayer =findNumber(players, number, 20);

       if (indexOfPlayer == -1) {

          System.out.println(\"Player not found!\");
       } else {

          System.out.println(players[indexOfPlayer].toString());
       }

   }

   /**
   *
   * @param players
   * @param playerNumber
   * @param teamSize
   * @return
   */
   public static int findNumber(Player[] players, intplayerNumber, int teamSize) {
       int index = -1;
       for (int i = 0; i < teamSize;i++) {
           try {
              if (players[i].getPlayerNumber() ==playerNumber) {

                  index = i;
              }
           } catch(Exception e) {

           }
       }
       return index;
   }
}

Answer & Explanation Solved by verified expert
3.8 Ratings (560 Votes)
Updated JavaCodeBaseball8javaimport javaioFileReaderimport javaioIOExceptionimport javautilScannerpublic class Baseball8 static Player players new Player20public static void mainString args int i 0try read line by line using split method create objects of PlayerScanner sc new ScannernewFileReaderdJavabaseballtxtwhile schasNext String str scnextLineint    See Answer
Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students