Question

Modify the program that you wrote for the last exercise in a file named Baseball9.java that...

Modify the program that you wrote for the last exercise in a file named Baseball9.java that uses the Player class stored within an array. The program should read data from the file baseball.txt for input. The Player class should once again be stored in a file named Player.java, however Baseball9.java is the only file that you need to modify for this assignment. Once all of the input data from the file is stored in the array, code and invoke a method named selectionSort() that sorts the array of Player objects in the ascending order of the Player Number field using the Selection Sort algorithm. Display the Number, Hits, Walks and Outs data attributes to the monitor for each array element both before and after sorting.

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;
   int smallestIndex =
try
   {
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) {

}

}
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());
}

}
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;
}
       }
      
   class Player
{
private int playerNumber;
private int numberOfWalks;
private int numberOfHits;
private int numberOfOuts;
       public Player()
       {
this.playerNumber = 0;
this.numberOfWalks = 0;
this.numberOfHits = 0;
this.numberOfOuts = 0;
}
public Player(int playerNumber, int numberOfWalks, int numberOfHits, int numberOfOuts)
{
super();
this.playerNumber = playerNumber;
this.numberOfWalks = numberOfWalks;
this.numberOfHits = numberOfHits;
this.numberOfOuts = numberOfOuts;
}
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;
}
public String toString()
{
return playerNumber + "\t" + numberOfWalks + "\t" + numberOfHits + "\t" + numberOfOuts;
}
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1
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 {
                        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!");
                }
                
                displayTeam(players, i);

                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());
                }

                // Sort players.
                selectionSort(players, i);
                
                System.out.println("\nAfter sorting: ");
                displayTeam(players, i);
                
        }
        public static void displayTeam(Player[] players, int teamSize) {

                System.out.println("Player\tHits\tWalks\tOuts");
                for (int i=0; i<teamSize; i++) {
                        System.out.println(players[i].toString());
                }
        }

        public static void selectionSort(Player[] players, int teamSize) {

                for (int i = 0; i < teamSize; i++) {
                        for (int j = i + 1; j < teamSize; j++) {
                                if (players[i].getPlayerNumber() > players[j].getPlayerNumber()) {
                                        Player temp = players[i];
                                        players[i] = players[j];
                                        players[j] = temp;
                                }
                        }
                }
        }

        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;
        }
}

class Player {
        private int playerNumber;
        private int numberOfWalks;
        private int numberOfHits;
        private int numberOfOuts;

        public Player() {
                this.playerNumber = 0;
                this.numberOfWalks = 0;
                this.numberOfHits = 0;
                this.numberOfOuts = 0;
        }

        public Player(int playerNumber, int numberOfWalks, int numberOfHits, int numberOfOuts) {
                super();
                this.playerNumber = playerNumber;
                this.numberOfWalks = numberOfWalks;
                this.numberOfHits = numberOfHits;
                this.numberOfOuts = numberOfOuts;
        }

        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;
        }

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


Hi. Please find the answer above.. In case of any doubts, you may ask in comments. You may upvote the answer if you feel i did a good work!

Add a comment
Know the answer?
Add Answer to:
Modify the program that you wrote for the last exercise in a file named Baseball9.java that...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I have a program that reads a file and then creates objects from the contents of...

    I have a program that reads a file and then creates objects from the contents of the file. How can I create a linked list of objects and use it with the package class instead of creating and using an array of objects? I am not allowed to use any arrays of objects or any java.util. lists in this program. Runner class: import java.util.Scanner; import java.io.*; class Runner { public static Package[] readFile() { try { File f = new...

  • Help check why the exception exist do some change but be sure to use the printwriter...

    Help check why the exception exist do some change but be sure to use the printwriter and scanner and make the code more readability Input.txt format like this: Joe sam, thd, 9, 4, 20 import java.io.File; import java.io.PrintWriter; import java.io.IOException; import java.io.FileNotFoundException; import java.io.FileWriter; import java.util.Scanner; public class Main1 { private static final Scanner scan = new Scanner(System.in); private static String[] player = new String[622]; private static String DATA = " "; private static int COUNTS = 0; public static...

  • FOR JAVA: Summary: Create a program that adds students to the class list (see below). The...

    FOR JAVA: Summary: Create a program that adds students to the class list (see below). The solution should be named Roster402_v2.java. Allow the user to control the number of students added to the roster. Ask if the user would like to see their new roster to confirm additions. If yes, then display contents of the file, if no, end the program. ------------------------------------------------------------------------------------- List of student names and IDs for class (this will be your separate text file): Jones, Jim,45 Hicks,...

  • I've previously completed a Java assignment where I wrote a program that reads a given text...

    I've previously completed a Java assignment where I wrote a program that reads a given text file and creates an index that stores the line numbers for where individual words occur. I've been given a new assignment where I need to modify some of my old code. I need to replace the indexer in my Index class with a NavigableMap<String, Word> and update my Word class with NavigableSet<Integer> lines. The instantiated objects should be TreeMap() and TreeSet(). I have below...

  • composed the following java code to read a string from a text file but receiving compiling...

    composed the following java code to read a string from a text file but receiving compiling errors. The text file is MyNumData.txt. Included the original java script that generated the output file. Shown also in the required output results after running the java program. I can't seem to search for the string and output the results. Any assistance will be greatly appreciated. import java.io.BufferedReader; import java.io.FileReader; import java.util.ArrayList; public class Main {   public static void main(String[] args) {     System.out.print("Enter the...

  • Problem: Use the code I have provided to start writing a program that accepts a large...

    Problem: Use the code I have provided to start writing a program that accepts a large file as input (given to you) and takes all of these numbers and enters them into an array. Once all of the numbers are in your array your job is to sort them. You must use either the insertion or selection sort to accomplish this. Input: Each line of input will be one item to be added to your array. Output: Your output will...

  • I have this program that works but not for the correct input file. I need the...

    I have this program that works but not for the correct input file. I need the program to detect the commas Input looks like: first_name,last_name,grade1,grade2,grade3,grade4,grade5 Dylan,Kelly,97,99,95,88,94 Tom,Brady,100,90,54,91,77 Adam,Sandler,90,87,78,66,55 Michael,Jordan,80,95,100,89,79 Elon,Musk,80,58,76,100,95 output needs to look like: Tom Brady -------------------------- Assignment 1: A Assignment 2: A Assignment 3: E Assignment 4: A Assignment 5: C Final Grade: 82.4 = B The current program: import java.io.File; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.util.Scanner; public class main {    public static void main(String[]...

  • (Reading & Writing Business Objects) I need to have my Classes be able to talk to...

    (Reading & Writing Business Objects) I need to have my Classes be able to talk to Files. How do I make it such that I can look in a File for an account number and I am able to pull up all the details? The file should be delimited by colons (":"). The Code for testing 'Select' that goes in main is: Account a1 = new Account(); a1.select(“90001”); a1.display(); Below is what it should look like for accounts 90000:3003:SAV:8855.90 &...

  • Trying to practice this assignment Argument list: the *yahoonews.txt Data file: yahoonews.txt Wr...

    Trying to practice this assignment Argument list: the *yahoonews.txt Data file: yahoonews.txt Write a program named WordCount.java, in this program, implement two static methods as specified below: public static int countWord(Sting word, String str) this method counts the number of occurrence of the word in the String (str) public static int countWord(String word, File file) This method counts the number of occurrence of the word in the file. Ignore case in the word. Possible punctuation and symbals in the file...

  • I need to write a program in java that reads a text file with a list...

    I need to write a program in java that reads a text file with a list of numbers and sorts them from least to greatest. This is the starter file. import java.util.*; import java.io.*; public class Lab3 { static final int INITIAL_CAPACITY = 5; public static void main( String args[] ) throws Exception { // ALWAYS TEST FOR REQUIRED INPUT FILE NAME ON THE COMMAND LINE if (args.length < 1 ) { System.out.println("\nusage: C:\\> java Lab3 L3input.txt\n"); System.exit(0); } //...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT