Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Assignment directions:Input will be provided in a text file called scores.txt. There will be one line for each game and
- the lines will be in the following form:
- 2010/11/08,UC Irvine,65,Illinois,79,0,0
- 2010/11/12,CSU Northridge,50,UCLA,83,0,0
- 2010/11/12,Truman St,59,Mo. Kansas City,71,0,0
- where the fields are date, away team, away team's score, home team, home team's score, a
- neutral court flag, and the number of overtimes. Fields are separated by commas. The neutral
- court flag will be 1 to indicate that the game was played on a neutral court (in which case home
- and away teams were determined by which team wore white) and the flag will be 0 otherwise
- (when the home team really was playing at home).
- Classes
- There should be at least three classes in your program.
- • One class will represent one individual game result (e.g., GameResult). Each object of
- this class will be created out of a single line entry in the game database text file.
- • A second class will hold a list of game result objects in an ArrayList (e.g., GameList).
- o There should be at least two methods in this second class, namely
- computeWinningPercentage (String team) which returns a double value and
- getOpponentList(String team) which returns an ArrayList of opponent teams.
- o The method “double computeWinningPercentage(String team)” will compute
- and return the winning percentage. Winning percentage will be computed by
- dividing the number of wins by the total number of games played a particular
- team. This method will return Double.NaN (look it up in Java API for Double) for
- teams that have not played any games.
- o The method ArrayList<String> getOpponentList(String team) will create and
- return a list of all of the opponents for the given team, where opponents that
- were played more than once should appear multiple times on the list.
- • Finally, you will design a third class (e.g., GamesDatabase) that will handle user input,
- including the name of the input file, what team to get information about, and which
- information to get about that team.
- o There needs to be a method in one of the classes that will compute the winning
- percentage of all the teams in the database and write them out in a text file
- called winning_pct.txt. When you exit the program by typing QUIT, your
- program will compute these percentages, write them to winning_pct.txt and
- then quit.
- First Class
- package project2;
- import java.util.Scanner;
- public class GameResult implements Comparable <GameResult>{
- protected String date;
- protected String awayTeam;
- protected int awayTeamScore;
- protected String homeTeam;
- protected int homeTeamScore;
- protected int neutralCourtFlag;
- protected int overtimes;
- /**
- * Constructor for my instance variables
- *
- *
- */
- public GameResult(String date, String awayTeam, int awayTeamScore, String homeTeam, int homeTeamScore, int neutralCourtFlag, int overtimes) {
- this.date = date;
- this.awayTeam = awayTeam;
- this.awayTeamScore = awayTeamScore;
- this.homeTeam = homeTeam;
- this.homeTeamScore = homeTeamScore;
- this.neutralCourtFlag = neutralCourtFlag;
- this.overtimes = overtimes;
- }
- /**
- * Getter methods for instance variables
- */
- public String getDate(){
- return date;
- }
- public String getAwayTeam() {
- return awayTeam;
- }
- public String getHomeTeam() {
- return homeTeam;
- }
- public int getAwayTeamScore() {
- return awayTeamScore;
- }
- public int getHomeTeamScore() {
- return homeTeamScore;
- }
- public int getNeutralCourtFlag() {
- return neutralCourtFlag;
- }
- public int getOvertimes()
- {
- return overtimes;
- }
- public int compareTo(GameResult arg0) {
- return 0;
- }
- /**
- * ToString method for a game's information
- */
- public String toString() {
- String gameStats;
- gameStats = "";
- gameStats = "Date: " + getDate() + " " + getAwayTeam() + ": " + getAwayTeamScore() + " " + getHomeTeam() + ": "
- + getHomeTeamScore() + " Neutral Court Flags: " + getNeutralCourtFlag() + " " + "Overtimes: " + getOvertimes() ;
- return gameStats;
- }
- }
- ***************************************************************************************************************************************
- 2nd Class
- package project2;
- import java.util.Scanner;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.StringTokenizer;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.FileReader;
- import java.io.FileNotFoundException;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.BufferedReader;
- import java.io.PrintWriter;
- import java.io.PrintStream;
- public class GameList {
- // neutral court flag, and the number of overtimes.
- private ArrayList<GameResult> _gameList;
- // Default Constructor
- public GameList() {
- _gameList = new ArrayList<GameResult>();
- }
- public void readFromTextFile() {
- String fileName = getInputFileName();
- GameResult objGameInfo;
- try {
- BufferedReader br;
- FileReader fr;
- fr = new FileReader("scores.txt");
- br = new BufferedReader(fr);
- String oneGameData;
- do {
- oneGameData = br.readLine();
- if (oneGameData == null) {
- break;
- }
- else {
- objGameInfo = processOneGameInfo(oneGameData);
- _gameList.add(objGameInfo);
- }
- while(oneGameData != null);
- br.close();
- //catching filenotfound exception
- }
- catch(FileNotFoundException fnfe) {
- System.out.println(fnfe.getMessage());
- //catching IOException
- } catch(IOException ioe) {
- System.out.println(ioe.getMessage());
- }
- }
- /** Asks user the name of the input file and returns it as a string
- *
- */
- private String getInputFileName() {
- Scanner scan = new Scanner(System.in);
- System.out.println("Please enter the name of the file to read from");
- return scan.nextLine();
- }
- /*
- * Asks user for the name of the output file and returns it as a string
- */
- private String getoutPutFileName() {
- Scanner scan = new Scanner(System.in);
- System.out.println("Please enter the name of the file to write output from");
- return scan.nextLine();
- }
- /**
- * Given string representation of one line of info about a basketball game
- * this method parses the info and puts them into the fields of a GameResult object
- *
- * These fields are separated by using StringTokenizer
- * @throws Exception
- *
- */
- public GameResult processOneGameInfo(String strGameInfo) throws Exception {
- GameResult gameInfo;
- StringTokenizer tokenizer = new StringTokenizer(strGameInfo);
- if (tokenizer.countTokens()!= 7) {
- System.out.println(strGameInfo);
- throw new Exception(
- "Unexpected number of fields in a record: " + tokenizer.countTokens());
- }
- String date = tokenizer.nextToken();
- String awayTeam = tokenizer.nextToken();
- int awayTeamScore = Integer.parseInt(tokenizer.nextToken());
- String homeTeam = tokenizer.nextToken();
- int homeTeamScore = Integer.parseInt(tokenizer.nextToken());
- int neutralCourtFlag = Integer.parseInt(tokenizer.nextToken());
- int overtimes = Integer.parseInt(tokenizer.nextToken());
- //constructing a GameResult object using the tokenized values
- gameInfo = new GameResult(date, awayTeam, awayTeamScore, homeTeam,
- homeTeamScore, neutralCourtFlag, overtimes);
- return gameInfo;
- }
- public void displayList() {
- for(GameResult gameInfo: _gameList) {
- System.out.println(gameInfo);
- }
- }
- public double computeWinningPercentage(String team) {
- int gameCount = 0;
- int winCount = 0;
- readFromTextFile();
- for(GameResult gameInfo: _gameList) {
- if(team.equals(gameInfo.getHomeTeam()) || team.equals(gameInfo.getAwayTeam())) {
- gameCount ++;
- if(team.equals(gameInfo.getHomeTeam())
- && gameInfo.getHomeTeamScore() > gameInfo.getAwayTeamScore() ){ //Wouldnt this return nothing since
- //my instance variables are empty?
- winCount ++;
- }
- else if(team.equals(gameInfo.getAwayTeam()) &&
- gameInfo.getAwayTeamScore() > gameInfo.getHomeTeamScore() ) {
- winCount ++;
- }
- }
- else
- System.out.println("That team does not exist in the data base.");
- return Double.NaN;
- }
- double winningPercent = (winCount/gameCount) * 100;
- return winningPercent;
- }
- public String getOpponentList(String team){
- ArrayList<String> opponentList;
- readFromTextFile();
- for(GameResult gameInfo: _gameList)
- {
- if (team.hasString(homeTeam))
- {
- opponentList.add(awayTeam); //the add functions don't work
- }
- else if (team.equals(getAwayTeam())) //this is not defined.. I didnt use my instance variables correctly I think
- {
- return (gameInfo.getHomeTeam())
- }
- else
- System.out.println("Team not found");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement