Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The file named world_series_winners.txt contains a chronological list of the wor

ID: 3838936 • Letter: T

Question

The file named world_series_winners.txt contains a chronological list of the world Series winning teams from 1903 through 2009. (The first line in the file is the name of the team that won in 1903, and the last line is the name of the team that won in 2009. Note that the World Series was not played in 1904 or 1994.) Write a program winnerFrequency.java that has a main method that lets the user enter the name of a team and then displays to the screen the number of times that team has won the World Series in the time period from 1903 through 2009. Your program calls a method countWins that takes a name of team and the filename as parameter and returns how many time this teams won. The method has the following signature: public static int countWins (String teamName, String fileName) Test your program with 3 different team names: 2 form the sample file provided world_series_ winners .txt 1 team name that is not in the file.

Explanation / Answer

import java.io.File;
import java.util.Scanner;

public class WinnerFrequency {

   /**
   * @param args
   */
   public static void main(String[] args) {
       // world_series_winners.txt
       System.out.println("count number of times team1 wins :"
               + countWins("team1", "world_series_winners.txt"));

       System.out.println("count number of times team5 wins :"
               + countWins("team5", "world_series_winners.txt"));

       System.out.println("count number of times team22 wins :"
               + countWins("team22", "world_series_winners.txt"));

   }

   /**
   * method to count the number of times team wins
   *
   * @param teamName
   * @param fileName
   * @return
   */
   public static int countWins(String teamName, String fileName) {

       int count = 0;

       Scanner scanner = null;
       try {
           scanner = new Scanner(new File(fileName));
           while (scanner.hasNext()) {
               String currentTeam = scanner.next();
               if (currentTeam.equals(teamName))
                   count++;
           }
       } catch (Exception e) {
           // TODO: handle exception
       } finally {
           if (scanner != null)
               scanner.close();

       }

       return count;
   }
}

world_series_winners.txt

team1
team2
team3
team4
team1
team6
team7
team8
team9
team10
team11
team12
team13
team14
team15
team16
team17
team18
team19
team20
team1
team2
team3
team4
team5
team6
team7
team8
team9
team10
team11
team12
team13
team14
team15
team16
team17
team18
team19
team20
team1
team2
team3
team4
team5
team6
team7
team8
team9
team10
team11
team12
team13
team14
team15
team16
team17
team18
team19
team20

OUTPUT:

count number of times team1 wins :4
count number of times team5 wins :2
count number of times team22 wins :0

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote