Question
In java, fill in the remaining code ( methods) to sort the world championship array list ( there are two classes, mainclass and winner. The
In java, fill in the remaining code ( methods) to sort the world championship array list ( there are two classes, mainclass and winner. The methods that need to be filled in are in mainclass.
import java.io.File;
import java.util.Scanner;
public class MainClass {
public static Winner [] listOfWinners;
public static void loadFromFile()
{
try{
//Create instance of Scanner and provide instance of File pointing to the txt file
Scanner input = new Scanner(new File("WorldSeriesWinners.txt"));
//Get the number of teams
int years = input.nextInt();
input.nextLine();//move to the next line
//Create the array
listOfWinners = new Winner[years];
//for every year in the text file
for(int index = 0; index { //Get the year int year = input.nextInt(); input.skip(" "); //Get the team String team = input.nextLine(); //Create an instance of Winner and add it to the next spot in the array listOfWinners[index] = new Winner(team,year); } }catch(Exception e) { System.out.println("Hey Meatbag, I'm Bender, and something wrong in the loadFromFile method happened!"); System.out.println(e.toString()); System.exit(0); } } public static void sortByTeamName() { //Fill In Code } public static void sortByYear() { //Fill In Code } public static void printArray() { //Print the array for(int index=0; index { System.out.println(listOfWinners[index].getYear()+" Winners " + listOfWinners[index].getTeam()); } } public static void searchForWinnerByYear(int year) { //Fill In Code } public static void searchForYearsATeamWon(String team) { //Fill In Code } public static void main(String[] args) { int choice; Scanner keyboard = new Scanner(System.in); System.out.println("World Series Program"); //Load textfile loadFromFile(); do{ System.out.println("1.........Print out the Winner List sorted by team name"); System.out.println("2.........Print out the Winner List sorted by years"); System.out.println("3.........Print out the Winner of a particular year"); System.out.println("4.........Print out the years a particular team won"); System.out.println("5.........Exit the Program"); System.out.println("Which Choice Would You Like?"); choice = keyboard.nextInt(); switch(choice) { case 1: //Option 1, sort array by name and print out. sortByTeamName(); printArray(); break; case 2: //Option 2, sort array by year and print out. sortByYear(); printArray(); break; case 3: //Option 3 Search for winner by year System.out.println("What year would you like to find the winner?"); int year = keyboard.nextInt(); searchForWinnerByYear(year); break; case 4: //Option 4 Search for years a team won System.out.println("What team would you like to check for years won?"); keyboard.nextLine(); String team = keyboard.nextLine(); searchForYearsATeamWon(team); break; case 5://Exit break; default: System.out.println("Invalid Choice"); } }while(choice != 5); System.out.println("Goodbye!"); } } public class Winner { private String team; private int year; private Winner() { /* * Make default constructor private so programers who use this * class are forced to use the constructor that requires them * to provide both the team name and year */ } public Winner(String team, int year) { this.team = team; this.year = year; } public String getTeam() { return team; } public int getYear() { return year; } } worldserieswinners.txt ( file input) : WorldSeriesWinners.txt: 112 1903 Boston Americans 1904 No World Series 1905 New York Giants 1906 Chicago White Sox 1907 Chicago Cubs 1908 Chicago Cubs 1909 Pittsburgh Pirates 1910 Philadelphia Athletics 1911 Philadelphia Athletics 1912 Boston Red Sox 1913 Philadelphia Athletics 1914 Boston Braves 1915 Boston Red Sox 1916 Boston Red Sox 1917 Chicago White Sox 1918 Boston Red Sox 1919 Cincinnati Reds 1920 Cleveland Indians 1921 New York Giants 1922 New York Giants 1923 New York Yankees 1924 Washington Senators 1925 Pittsburgh Pirates 1926 St. Louis Cardinals 1927 New York Yankees 1928 New York Yankees 1929 Philadelphia Athletics 1930 Philadelphia Athletics 1931 St. Louis Cardinals 1932 New York Yankees 1933 New York Giants 1934 St. Louis Cardinals 1935 Detroit Tigers 1936 New York Yankees 1937 New York Yankees 1938 New York Yankees 1939 New York Yankees 1940 Cincinnati Reds 1941 New York Yankees 1942 St. Louis Cardinals 1943 New York Yankees 1944 St. Louis Cardinals 1945 Detroit Tigers 1946 St. Louis Cardinals 1947 New York Yankees 1948 Cleveland Indians 1949 New York Yankees 1950 New York Yankees 1951 New York Yankees 1952 New York Yankees 1953 New York Yankees 1954 New York Giants 1955 Brooklyn Dodgers 1956 New York Yankees 1957 Milwaukee Braves 1958 New York Yankees 1959 Los Angeles Dodgers 1960 Pittsburgh Pirates 1961 New York Yankees 1962 New York Yankees 1963 Los Angeles Dodgers 1964 St. Louis Cardinals 1965 Los Angeles Dodgers 1966 Baltimore Orioles 1967 St. Louis Cardinals 1968 Detroit Tigers 1969 New York Mets 1970 Baltimore Orioles 1971 Pittsburgh Pirates 1972 Oakland Athletics 1973 Oakland Athletics 1974 Oakland Athletics 1975 Cincinnati Reds 1976 Cincinnati Reds 1977 New York Yankees 1978 New York Yankees 1979 Pittsburgh Pirates 1980 Philadelphia Phillies 1981 Los Angeles Dodgers 1982 St. Louis Cardinals 1983 Baltimore Orioles 1984 Detroit Tigers 1985 Kansas City Royals 1986 New York Mets 1987 Minnesota Twins 1988 Los Angeles Dodgers 1989 Oakland Athletics 1990 Cincinnati Reds 1991 Minnesota Twins 1992 Toronto Blue Jays 1993 Toronto Blue Jays 1994 No World Series 1995 Atlanta Braves 1996 New York Yankees 1997 Florida Marlins 1998 New York Yankees 1999 New York Yankees 2000 New York Yankees 2001 Arizona Diamondbacks 2002 Anaheim Angels 2003 Florida Marlins 2004 Boston Red Sox 2005 Chicago White Sox 2006 St. Louis Cardinals 2007 Boston Red Sox 2008 Philadelphia Phillies 2009 New York Yankees 2010 San Francisco Giants 2011 St. Louis Cardinals 2012 San Francisco Giants 2013 Boston Red Sox 2014 San Francisco Giants 1. Method sortByTeamName (15 pts) The method you will finish, it will sort (in ascending order) the listOfWinners array by the name of the teams. (Implement your own sort suggest selection sort) 2. Method sortByYear (15 pts) The method you will finish, it will sort (in ascending order) the listOfWinners array by the name of the years. 3. Method searchForWinnerByYear(10 pts) The method will search through listOfWinners array to find a match to year and print out the name of the team that won that year. 4. Method searchForYearsATeamWon(10 pts) The method will search through listOfWinners array to find all matches to team and print out all years that team won the World Series.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started