Very New to java, skeleton of program given

Reply

Join Date: Jun 2005
Posts: 1
Reputation: SunshineBusride is an unknown quantity at this point 
Solved Threads: 0
SunshineBusride SunshineBusride is offline Offline
Newbie Poster

Very New to java, skeleton of program given

 
0
  #1
Jun 8th, 2005
hi guys, im in my first few weeks of working with java, and am finding it hard to complete an excercise i've been given. We are working with a java program that works over 3 files, and we have to "fill in the blanks".

trouble is, i don't know what is really needed as far as methods and constructors go, and don't really understand the assignment.

The program is supposed to work processing sporting results. We are given a driver program which accepts the user input. this is it:

  1. (LeagueDriver.java)
  2.  
  3. import java.io.*;
  4.  
  5. public class LeagueDriver {
  6. private static final char OPTION_ADD = 'a';
  7. private static final char OPTION_PRINT = 'p';
  8. private static final char OPTION_SORT = 's';
  9. private static final char OPTION_FIND = 'f';
  10. private static final char OPTION_QUIT = 'q';
  11.  
  12. /**
  13.   The BufferedReader object is an instance variable, so can be used by any method requiring input from console.
  14.   It is necessary to do it this way, otherwise redirection of input (from a file) does not work
  15.   */
  16. private BufferedReader console;
  17.  
  18. /**
  19.   Holds all the data relevant to a League.
  20.   Note that the LeagueDriver class doesn't need to know anything about the Team class.
  21.   */
  22. private League theLeague;
  23.  
  24.  
  25. /**
  26.   Drives the program, namely displays the program's welcome message, and causes the leaugue data to be input,
  27.   a menu to be displayed and the users response to be processed.
  28.   */
  29. public static void main(String[] args) throws IOException {
  30. LeagueDriver program = new LeagueDriver(); // created so we can call non-static methods
  31. char response; // users selection from the menu
  32.  
  33. System.out.println("The League Table Program\n\n");
  34.  
  35. program.setupLeague();
  36. response = program.displayMenu();
  37.  
  38. // the logic of the loop is kept in main, and not 'hidden' in another method
  39. while (response != OPTION_QUIT) {
  40. program.processSelection(response);
  41. response = program.displayMenu();
  42. }
  43.  
  44. System.out.println("Goodbye");
  45. }
  46.  
  47. /**
  48.   The instance variable theLeague will be initialised in setupLeague().
  49.   */
  50. public LeagueDriver() {
  51. console = new BufferedReader(new InputStreamReader(System.in));
  52. }
  53.  
  54. /**
  55.   Gets data from the user so the League object can be instantiated.
  56.   */
  57. private void setupLeague() throws IOException {
  58. String leagueName;
  59. int premiershipPoints;
  60. int numberOfTeams;
  61. String[] namesOfTeams;
  62.  
  63. System.out.println("League setup\n"
  64. +"------------");
  65.  
  66. System.out.print("What is the name of this league: ");
  67. leagueName = console.readLine();
  68.  
  69. System.out.print("How many premiership points is a win worth: ");
  70. premiershipPoints = Integer.parseInt(console.readLine());
  71.  
  72. System.out.print("How many teams in this league: ");
  73. numberOfTeams = Integer.parseInt(console.readLine());
  74. namesOfTeams = new String[numberOfTeams];
  75.  
  76. System.out.println("\nPlease enter the team names");
  77.  
  78. for (int i = 0; i < namesOfTeams.length; i++) {
  79. System.out.print("\tTeam "+ (i+1) +": ");
  80. namesOfTeams[i] = console.readLine();
  81. }
  82.  
  83. // instantiate the League object
  84. theLeague = new League(leagueName, namesOfTeams, premiershipPoints);
  85.  
  86.  
  87. // these statements check that data is stored correctly
  88. System.out.println("The setup for " +theLeague.getName() +" is complete");
  89. System.out.println(leagueName +" has " +theLeague.numberOfTeams() +" teams.");
  90.  
  91. System.out.println("\nThe league table currently looks like this:");
  92. System.out.println(theLeague);
  93.  
  94. }
  95.  
  96. /**
  97.   Displays a textual menu
  98.   @return user's menu selection (as a lower case character)
  99.   */
  100. private char displayMenu() throws IOException {
  101. System.out.println("\n\nLeague Table Menu\n"
  102. +"-----------------");
  103. System.out.println(OPTION_ADD +" = add game stats");
  104. System.out.println(OPTION_PRINT +" = print table");
  105. System.out.println(OPTION_SORT +" = sort table");
  106. System.out.println(OPTION_FIND +" = find a team");
  107. System.out.println(OPTION_QUIT +" = program");
  108. System.out.print("Please enter an option: ");
  109.  
  110. return Character.toLowerCase(console.readLine().charAt(0));
  111. }
  112.  
  113. /**
  114.   Depending on the user's selection, invokes different League methods.
  115.   Notice how this class is handling all of the user input, and the input is sent to League.
  116.   @param selection user's menu selection
  117.   */
  118. private void processSelection(char selection) throws IOException {
  119. switch (selection) {
  120. case OPTION_ADD:
  121. // need more data from user, so go to a method in this class first
  122. addGameStats();
  123. break;
  124. case OPTION_PRINT:
  125. // prints the league ladder (automatically invokes League's toString())
  126. System.out.println(theLeague);
  127. break;
  128. case OPTION_SORT:
  129. // sort the ladder according to the rules dictated by League
  130. theLeague.sortLadder();
  131. break;
  132. case OPTION_FIND:
  133. // prompt & get a team name as a string, then display that team's data
  134. System.out.print("Please enter team name to find: ");
  135. System.out.println(theLeague.findTeam(console.readLine()));
  136. break;
  137. default:
  138. System.out.println("Error: invalid menu option.");
  139. break;
  140. }
  141. }
  142.  
  143. /**
  144.   Gather data for a match before invoking the League method
  145.   */
  146. private void addGameStats() throws IOException {
  147. String team1;
  148. String team2;
  149. int scoreFor;
  150. int scoreAgainst;
  151.  
  152. System.out.print("Team 1: ");
  153. team1 = console.readLine();
  154.  
  155. System.out.print("Team 2: ");
  156. team2 = console.readLine();
  157.  
  158. System.out.print(team1 +"'s score: ");
  159. scoreFor = Integer.parseInt(console.readLine());
  160.  
  161. System.out.print(team2 +"'s score: ");
  162. scoreAgainst = Integer.parseInt(console.readLine());
  163.  
  164. theLeague.addGameStats(team1, team2, scoreFor, scoreAgainst);
  165. }
  166. }


this doesn't need changing, but it (probably) will help you guys decipher this. Here are the remaining files.

  1.  
  2. (League.java)
  3.  
  4. import java.util.Arrays;
  5.  
  6. /**
  7.   This class does not do any user input!
  8.  
  9.   @version 2.0 5 May 2005
  10.   @author YOUR NAME
  11.  */
  12. public class League {
  13. private final int WIN_POINTS;
  14. private String leagueName;
  15. private Team[] teams;
  16.  
  17. /**
  18.   Constructor instantiates the Team array <code>teams</code>, and for each
  19.   array element it instantiates a Team object (using the appropriate value
  20.   from <code>teamNames</code>).
  21.   @param leagueName name of this league
  22.   @param teamNames an array of team names
  23.   @param pointsForWin number of premiership points a win is worth (assumes a draw is pointsForWin/2)
  24.   */
  25. public League(String leagueName, String[] teamNames, int pointsForWin) {
  26. this.leagueName = leagueName;
  27.  
  28. teams = new Team[teamNames.length];
  29.  
  30. // PUT A LOOP HERE TO CREATE A NEW TEAM, 1 FOR EACH NAME IN teamNames
  31.  
  32.  
  33.  
  34.  
  35. WIN_POINTS = pointsForWin;
  36.  
  37. }
  38.  
  39. public String getName() {
  40. return leagueName;
  41. }
  42.  
  43. public int numberOfTeams() {
  44. return teams.length;
  45. }
  46.  
  47.  
  48. /**
  49.   Adds the new game stats to the correct 2 teams.
  50.   It must update the points for and against to both teams
  51.  
  52.   @param team1 the home team
  53.   @param team2 the away team
  54.   @param pointsTeam1 the points scored by the home team
  55.   @param pointsTeam2 the points scored by the away team
  56.   @see #findTeamIndex(java.lang.String)
  57.   */
  58. public void addGameStats(String team1, String team2, int pointsTeam1, int pointsTeam2) {
  59. // YOU MUST COMPLETE THIS METHOD
  60. // hint: make use of the method findTeamIndex
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73. }
  74.  
  75. /**
  76.   Sorts the ladder according to the "natural order" of Teams.
  77.   Disaplys a message if the sorting is not performed (e.g. you haven't implemented this method).
  78.   Refer to the API for the pre-defined class Arrays.
  79.   */
  80. public void sortLadder() {
  81. // YOU MUST COMPLETE THIS METHOD
  82. // hint: it's one short statement
  83.  
  84. }
  85.  
  86.  
  87. /**
  88.   Prints the ladder to the console. Obviously prints info for each team in the array.
  89.   Must take advantage of a Team object's <code>toString</code> method.
  90.  
  91.   @see Team#toString
  92.   */
  93. public String toString() {
  94. String str = "\n";
  95. String spacer1 = " ";
  96.  
  97. str += leagueName.toUpperCase() +" LADDER\n";
  98. str += "\t" + spacer1 + "P\tW\tL\tD\tF\tA\t%\tTotal\n";
  99.  
  100. for (int i = 0; i < teams.length; i++) {
  101. str += (i+1) + "\t";
  102.  
  103. // YOU MUST COMPLETE THIS METHOD
  104.  
  105.  
  106.  
  107. }
  108.  
  109. return str;
  110. }
  111.  
  112. /**
  113.   Each win is worth WIN_POINTS and each draw is worth WIN_POINTS/2
  114.  
  115.   */
  116. private int calculateTotalPremiershipPoints(Team team) {
  117. return team.getWins() * WIN_POINTS + team.getDraws() * WIN_POINTS/2;
  118. }
  119.  
  120. public String findTeam(String teamName) {
  121. String str = "";
  122. int index = findTeamIndex(teamName);
  123. if (index < teams.length)
  124. str = teams[index].toString();
  125. else
  126. str = "Error: no team found with the name " +teamName;
  127.  
  128. return str;
  129. }
  130.  
  131. /**
  132.   Find where in the array the Team with <code>teamName</code> is. Creates a temporary Team to
  133.   represent the team we're trying to find so that we can use Team's equals method.
  134.   @param teamName the String value used to find the correct <code>teams</code> subscript
  135.   @return the array subscript of the <code>teams</code> element that refers to
  136.   the Team object which has <code>teamName</code> as an attribute.
  137.   If no match, then returns teams.length
  138.   */
  139. private int findTeamIndex(String teamName) {
  140. Team temp = new Team(teamName);
  141. boolean found = false;
  142. int i = 0;
  143.  
  144. while (i < teams.length && !found) {
  145. if (teams[i].equals(temp)) {
  146. found = true;
  147. }
  148. else
  149. i++;
  150. }
  151.  
  152. return i;
  153. }
  154. }

That's the second file, i have added all the lines that i know will work, but my attempts at method and constructor making are embarrassingly wrong.

  1. (Team.java)
  2.  
  3. public class Team implements Comparable {
  4. // implement this class according to the published class diagram solution
  5. // deviating from the design (either fewer or more attributes/methods) will
  6. // result in loss of marks
  7.  
  8.  
  9.  
  10.  
  11.  
  12.  
  13.  
  14.  
  15. public Team(String name) {
  16. // COMPLETE THIS CONSTRUCTOR
  17.  
  18.  
  19.  
  20.  
  21.  
  22.  
  23. }
  24.  
  25.  
  26. // ADD ACCESSOR METHODS AND THE 1 MUTATOR METHOD
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35. /**
  36.   Updates this team's total points for, total points against,
  37.   and this team's wins, loses, or draws, as appropriate.
  38.  
  39.   @param pointsFor the points this team scored in a game
  40.   @param pointsAgainst the points the opposing team scored against this team
  41.   */
  42. public void updateStats(int pointsFor, int pointsAgainst) {
  43. // YOU MUST COMPLETE THIS METHOD
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53. }
  54.  
  55. /**
  56.   Calculates the ratio of points for to points against, as a percentage of points against.
  57.   @return the percentage of points scored against this team, rounded to 2 decimal points.
  58.   */
  59. public double calculatePercentage() {
  60. double percentage;
  61. percentage = (double)getTotalPointsFor() / (double)getTotalPointsAgainst();
  62. percentage *= 10000.0;
  63. percentage = (int)percentage;
  64. percentage /= 100.0;
  65.  
  66. return percentage;
  67. }
  68.  
  69. /**
  70.   Creates a description of this team. The description consists of
  71.   <ul>
  72.   <li>team name
  73.   <li>number of games played
  74.   <li>wins
  75.   <li>loses
  76.   <li>draws
  77.   <li>total points for
  78.   <li>total points against
  79.   </ul>
  80.   Note that it does not concatenate the percentage to this description.<p>
  81.   There are always 20 characters from the first letter of the team name,
  82.   to the games played digit. Two examples:
  83.   <pre>
  84.   Adelaide 1 0 1 0 123 132
  85.   Port Adelaide 1 1 0 0 132 123
  86.   </pre>
  87.   */
  88. public String toString() {
  89. String spacer1 = " ";
  90.  
  91. return getName() + spacer1.substring(getName().length())
  92. + (getWins() + getLoses() + getDraws())
  93. + "\t" + getWins()
  94. + "\t" + getLoses()
  95. + "\t" + getDraws()
  96. + "\t" + getTotalPointsFor()
  97. + "\t" + getTotalPointsAgainst();
  98. }
  99.  
  100. /**
  101.   2 Team objects are considered equal if they have the same name.
  102.   */
  103. public boolean equals(Object o) {
  104. boolean result = false;
  105.  
  106. if (o instanceof Team) {
  107. Team other = (Team)o;
  108.  
  109. result = this.name.equalsIgnoreCase(other.getName());
  110. }
  111.  
  112. return result;
  113. }
  114.  
  115.  
  116. /**
  117.   The "natural order" for a team is descending order of wins, percentage, points for, then ascending name.
  118.   */
  119. public int compareTo(Object o) {
  120. int result;
  121. Team other = (Team)o;
  122.  
  123. result = (other.getWins()+other.getDraws()) - (this.wins+this.draws);
  124.  
  125. if (result == 0) {
  126. result = other.getWins() - this.wins;
  127.  
  128. if (result == 0 ) {
  129. result = (int)(other.calculatePercentage()*100) - (int)(this.calculatePercentage()*100);
  130.  
  131. if (result == 0) {
  132. result = other.getTotalPointsFor() - this.totalPointsFor;
  133.  
  134. if (result == 0)
  135. result = this.name.compareTo(other.getName());
  136. }
  137. }
  138. }
  139.  
  140. return result;
  141. }
  142. }



Any pieces of code i try and put in just get me nowhere, because the very Key elements of the program are missing, i am very lost. Please Help?

Thanking you all in advance....

SunshineBusride.[I]
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC