matthewentwistle

Untitled

Dec 8th, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.98 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. /**
  4. * Represent an event in the competition.
  5. */
  6. class Event {
  7. private String name;
  8. private int points;
  9.  
  10. /**
  11. * Construct a new event.
  12. *
  13. * @param name the name of the event
  14. * @param points the number of points awarded to the winner
  15. */
  16. public Event(String name, int points) {
  17. this.name = name;
  18. this.points = points;
  19. }
  20.  
  21. public String getName() { return name; }
  22. public int getPoints() { return points; }
  23. }
  24.  
  25. public class Competition {
  26. // Note: only use one scanner in the whole program
  27. private static Scanner scanner = new Scanner(System.in);
  28.  
  29. public static void main(String[] args) {
  30. Event[] events = getEvents();
  31. int[] scores = getScores(events, 2);
  32. printScores(scores);
  33. }
  34.  
  35. /**
  36. * Create the list of events in the competition
  37. *
  38. * @return an array of competition events
  39. */
  40. private static Event[] getEvents() {
  41. Event[] events = new Event[5];
  42.  
  43. events[0] = new Event("5k Run", 10);
  44. events[1] = new Event("Long Jump", 5);
  45. events[2] = new Event("Chess Game", 7);
  46. events[3] = new Event("Rubik's Cube", 3);
  47. events[4] = new Event("Victoria Sponge Baking", 10);
  48.  
  49. return events;
  50. }
  51.  
  52. /**
  53. * Prompt the user for the winner of each event
  54. *
  55. * @param events the list of competition events
  56. * @param numPlayers the number of players in the competition
  57. * @return an array giving the score of each player 0..numPlayers - 1
  58. */
  59. private static int[] getScores(Event[] events, int numPlayers) {
  60. int[] scores = new int[numPlayers];
  61.  
  62. for (int i = 0; i < events.length; i++) {
  63. int winner = getWinner(events[i], numPlayers);
  64. scores[winner] += events[i].getPoints();
  65. }
  66.  
  67. return scores;
  68. }
  69.  
  70. /**
  71. * Prompt the user for the winner of the given event
  72. *
  73. * @param event the event in question
  74. * @param numPlayers the number of players in the competition
  75. */
  76. private static int getWinner(Event event, int numPlayers) {
  77. System.out.println("Which player won the " + event.getName());
  78.  
  79. int winner = -1;
  80. while (winner < 0 || winner >= numPlayers) {
  81. System.out.println("Enter player number between 0 and " +
  82. (numPlayers - 1) +
  83. ": ");
  84. if (scanner.hasNextInt())
  85. winner = scanner.nextInt();
  86. else
  87. scanner.next();
  88. }
  89.  
  90. return winner;
  91. }
  92.  
  93. /**
  94. * Print the results of the competition to the screen
  95. *
  96. * @param scores an array of scores, one entry for each player
  97. */
  98. private static void printScores(int scores[]) {
  99. for (int i = 0; i < scores.length; i++) {
  100. System.out.println("Player " + i + " scored " + scores[i]);
  101. }
  102. }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment