Question

In: Computer Science

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

Baseball statistics sorting, addition to the old code.

Following is what I have right now what more specifications of the pseudocode

I need addition to the program for the following pseudocode:

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

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

Follow the Pseudo-code for the selectionSort() method

Step 0) // declare local variables Most variables that are not parameters are indices of for statements. You will need a local int variable named smallestIndex to update the position of the k’th smallest Player Number and a Player variable named temp for swaps within team[] positions.

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

Step 2) // save Index to smallestIndex This assignment assumes that the k’th smallest Player Number is in the correct position.

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

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

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

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

Steps 5a)-5c) // save team[index] to temp // save team[smallestIndex] to team[index] // save temp to team[smallestIndex] This is the exchange of data between the array elements team[Index] and team[smallestIndex] that places the k’th smallest 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, int numberOfHits, 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 by line
           * using split method create objects of Player
           */
           Scanner sc = new Scanner(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 player detail
       * just call setters
       */
       System.out.print("Enter the number of player:");
       Scanner scan = new Scanner(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, int playerNumber, 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;
   }
}

Solutions

Expert Solution

Updated Java Code:

/******************************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 by line
* using split method create objects of Player
*/
Scanner sc = new Scanner(new FileReader("d:\\Java\\baseball.txt"));
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 player detail
* just call setters
*/
System.out.print("Enter the number of player:");
Scanner scan = new Scanner(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());
}
     
   System.out.println("\n Sorting team by number: \n");
       SelectionSort(players, 20);
      
       System.out.println("Player\tHits\tWalks\tOuts");
       for (Player player : players) {
try {
System.out.println(player.toString());
} catch (Exception e) {
}
       }
}
/**
*
* @param players
* @param playerNumber
* @param teamSize
* @return
*/
public static int findNumber(Player[] players, int playerNumber, 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;
}

//Selection sort Method
   public static void SelectionSort(Player[] players, int teamSize)
   {   
       int index;
       Player temp;
      
       //Iterating over array
for (int i = 0; i < (teamSize-1); i++)
{
index = i;
          
           //Iterating over remaining elements
for (int j = i+1; j < teamSize; j++)
           {
               //Comparing for largest value
if (players[j].getPlayerNumber() < players[index].getPlayerNumber())
               {
                   //Storing index
index = j;
               }
           }
          
           //Swapping largest element with last element
temp = players[index];
players[index] = players[i];
players[i] = temp;
}
}

}

___________________________________________________________________________________________________

Sample Run: Just for testing i am using only 5 players


Related Solutions

Can I get some assistance with this request. Right now the code that converts a hex...
Can I get some assistance with this request. Right now the code that converts a hex character read on input into its decimal value is in the main routine. To make the main routine easier to read, make the conversion code concise and reusable, move it into a function named hex2Dec. The hex2Dec function should take a character parameter which is the character read from input. The function should return an integer which is the converted decimal value of the...
All public companies now have a Code of Ethical Conduct. In addition there are professional organizations...
All public companies now have a Code of Ethical Conduct. In addition there are professional organizations that assist in helping their members resolve ethical conflicts. Go to the Institute of Management Accountant’s website (http://www.imanet.org/docs/default-source/press_releases/ethics_prof_prac.pdf?sfvrsn=2) to view its Statement on Ethical Professional Practice. If you experience an ethical dilemma at your organization, would the recommendations presented in the “Resolution of Ethical Conflict” be helpful? Have you, or any you have known, been faced with an ethical dilemma at work? How has...
Java Searching and Sorting, please I need the Code and the Output. Write a method, remove,...
Java Searching and Sorting, please I need the Code and the Output. Write a method, remove, that takes three parameters: an array of integers, the length of the array, and an integer, say, removeItem. The method should find and delete the first occurrence of removeItem in the array. If the value does not exist or the array is empty, output an appropriate message. (After deleting an element, the number of elements in the array is reduced by 1.) Assume that...
Suppose I have two mutually exclusive projects right now. For Project 1, it gives your $...
Suppose I have two mutually exclusive projects right now. For Project 1, it gives your $ 4200 dollar instantly, but you have to pay $1000 at the end of each year for the coming five years. For Project 2, it requires you to pay $1000 each year for five years. The first payment happens right now. Every payment happens at the start of the year. But at the end of the fifth year, you will get $6000 as a return....
I NEED IT RIGHT NOW Write about the following topic: Machines are increasingly being used to...
I NEED IT RIGHT NOW Write about the following topic: Machines are increasingly being used to do jobs that were previously done by humans. What problems are associated with this? What do you think can be done to solve this problem? Give reasons for your answer and include any relevant examples from your own knowledge or experience. Write at least 250 words. Please write your essay below.
I have this mystery code. I dont know what the code does. Can somone please explain...
I have this mystery code. I dont know what the code does. Can somone please explain with examples. (python 3.xx) from typing import Dict, TextIO, Tuple, List def exam(d1: Dict[str, List[int]], d2: Dict[int, int]) -> None: """ *Mystery code* """ for key in d1: value = d1[key] for i in range(len(value)): value[i] = d2[value[i]]   
Hi, the following is the question that I have been facing now and I really hope...
Hi, the following is the question that I have been facing now and I really hope if someone can help me with that. Problem analysis: This section is your analysis of the issues surrounding greenhouse gases and a carbon tax. You need spell out what the main problems of issues are facing Canadians. Use specific references when making your arguements. If more than one solution exists, then make an argument for focusing on just that one solution. In addition, how...
Hi, the following is the question that I have been facing now and I really hope...
Hi, the following is the question that I have been facing now and I really hope somebody can help me with it.    Problem analysis: This section is your analysis of the issues surrounding greenhouse gases and a carbon tax. You need to spell out what are the main problems or issues facing Canadians. Use specific references when making your arguments. If more than one solution exists, then make an argument for focusing on just that one solution. In addition,...
I have the following python code. I get the following message when running it using a...
I have the following python code. I get the following message when running it using a test script which I cannot send here: while textstring[iterator].isspace(): # loop until we get other than space character IndexError: string index out of range. Please find out why and correct the code. def createWords(textstrings): createdWords = [] # empty list for textstring in textstrings: # iterate through each string in trxtstrings iterator = 0 begin = iterator # new begin variable while (iterator <...
I have a code and it works and runs as it supposed too. What is the...
I have a code and it works and runs as it supposed too. What is the UML for it? Any help will be awesome. Thanks. import java.util.Scanner; public class StringToMorseCode { public static void main(String[] args){ Scanner input = new Scanner(System.in);    char[] letters = { ' ', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '1', '2', '3', '4', '5', '6',...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT