Question: I'm learning Object-Oriented-Programming and using Java language. I've already created 4 class: Team, Game, BadmintonGame and SoccerGame. class Team public class Team { protected String

I'm learning Object-Oriented-Programming and using Java language. I've already created 4 class: Team, Game, BadmintonGame and SoccerGame.

class Team

public class Team { protected String nation; // Nation name protected int points; // Points of that nation // Initialise nation name public Team(String nation) { this.nation = nation; } // Returns string containing team information public String toString() { return "Nation: "+nation+" Points: "+points; } }

class Game

public abstract class Game { // Names of nations competing private String nation1; private String nation2; // Initialise names Game(String nation1, String nation2) { this.nation1 = nation1; this.nation2 = nation2; } // Sets names of both nations public void setNames(String nation1, String nation2){ this.nation1 = nation1; this.nation2 = nation2; } // Accessor methods public String Nation1(){ return nation1; } public String Nation2(){ return nation2; } // Returns a string containing names of both nations public String toString() { return "Nation 1: "+nation1+" Nation2: "+nation2; } // abstract method points public abstract int points(String nation); }

class BadmintonGame

public class BadmintonGame extends Game { private String set1, set2, set3; private String score; // Constructor to initialise nations BadmintonGame(String nation1, String nation2) { super(nation1, nation2); } // Constructor to initialise nations, sets and score BadmintonGame(String nation1, String nation2, String set1, String set2, String set3, String score) { super(nation1, nation2); this.set1 = set1; this.set2 = set2; this.set3 = set3; this.score = score; } // Accessor methods public String getSet1() { return set1; } public String getSet2() { return set2; } public String getSet3() { return set3; } public String getScore() { return score; } // Mutator methods public void setSet1(String set) { set1 = set; } public void setSet2(String set) { set2 = set; } public void setSet3(String set) { set3 = set; } public void setScore(String value) { score = value; } // Returns a string containing information about this game public String toString() { return super.toString()+" "+"Set 1: "+set1+" Set 2: "+set2+" Set 3: "+set3+" Score: "+score; } // Points method calculates the points of the given nation public int points(String nation) { int val1 = Integer.parseInt(score.substring(0,1)); int val2 = Integer.parseInt(score.substring(2)); if(nation.equals(super.Nation1())) { // If nation 1 wins if(val1 > val2) return 2; else return 0; } else { // If nation 2 wins if(val1 < val2) return 2; else return 0; } } }

class SoccerGame

public class SoccerGame extends Game { private String score; // Constructor to initialise nations SoccerGame(String nation1, String nation2) { super(nation1, nation2); } // Constructor to initialise nations and score SoccerGame(String nation1, String nation2, String score) { super(nation1, nation2); this.score = score; } // Accessor method public String getScore() { return score; } // Mutator method public void setScore(String value) { score = value; } // Returns a string containing information about this game public String toString() { return super.toString()+" "+"Score: "+score; } // Calculates points for a given nation public int points(String nation) { // for score format 6-3 i.e. goals int idx = score.indexOf('-'); int val1 = Integer.parseInt(score.substring(0, idx)); int val2 = Integer.parseInt(score.substring(idx+1)); if(val1 == val2) return 1; if(nation.equals(super.Nation1())) { // If nation 1 wins if(val1 > val2) return 3; else return 0; } else { // If nation 2 wins if(val1 < val2) return 3; else return 0; } } }

MY QUESTION IS:

Please create GameTest class that uses text input-output files.

input filename : games.txt output filenames : badminton.txt, soccer.txt

Note that the actual input-output filenames may be different, so you need to prompt for the user filenames and not hardcoded all the filenames in your program.

1st line of games.txt will be the overall number of countries participating. 2nd line if games.txt will show the names of all the countries participating. The rest of the input file consist of the games and their results. (Note that each line can differ because it depends on the particular sport.)

Next create 2 arrays of Team to hold the data for these countries.

Team[] badmintonTeams = new Team[n]; Team[] soccerTeams = new Team[n];

Then create an array of Game to hold the games data for both sports. From there, create objects of either BadmintonGame or SoccerGame and calculate and total the points for each team in each sport.

badminton.txt file will store the results of each badminton game and print the winner of the badminton sport. (the country and the total points scored)

soccer.txt file will store the results of each soccer game and print the winner of the soccer sport. (the country and the total points scored)

sample input file: game.txt

4

Malaysia;Japan;Belgium;USA

Malaysia;Japan;21-19;21-8;21-17

Malaysia;USA;10-21;21-19;21-11

Malaysia;Belgium;21-9;21-9;21-0

Japan;Belgium;21-10;21-5;19-21

Japan;USA;16-21;21-11;21-18

USA;Belgium;1-21;13-21;16-21

Malaysia;Japan;0-4

Malaysia;USA;1-1

Malaysia;Belgium;2-11

Japan;Belgium;2-3

Japan;USA;4-2 USA;

Belgium;4-5

sample output file: badminton.txt

nation1 nation2 score set1 score set 2 score set3

Malaysia Japan 21-19 21-8 21-17

Malaysia USA 10-21 21-19 21-11

Malaysia Belgium 21-9 21-9 21-0

The winner is Malaysia team with total points 60.

Note that this coding need to implement round-robin format in order to know no of games based on no of teams.txt

Declare this array of int in the GameTest class. Once you read in the first line the number of teams - let that value be n teams, then we can easily determine the number of games by a simple assignment: //1st line: read n, the number of teams: //declare n soccer and badminton teams Team[] badmintonTeams = new Team[n]; Team[] soccerTeams = new Team[n]; // round-robin number of games int[] numGames = {0, 0, 1, 3, 6, 10, 15, 21, 28, 36, 45}; //assign the number of games based on the value of n. int m = numGames[n]; Game[] game = new Game[m]; Then read each game data from lines 3 onwards, which can be either badminton or soccer games, whereby each of these two sports have different score format, create that sports object and assign to the game array.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!