In: Computer Science
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;
}
}
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