Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.56 KB | None | 0 0
  1. /*********************************************************************
  2. * * Find Fastest
  3. * copyright 2017-2018
  4. * class P1
  5. *
  6. * Find the fastest runner from a list
  7. * *
  8. * Input: name (space) time
  9. * *
  10. * Parameters parse in: A file called "runner.txt"
  11. * *
  12. * Output:
  13. * *
  14. * a file called "result" with the fastest runner and possible second fastest runner
  15. *
  16. *********************************************************************/
  17.  
  18.  
  19. package com.company.cs;
  20.  
  21. import java.io.File;
  22. import java.io.IOException;
  23. import java.io.PrintWriter;
  24. import java.util.*;
  25.  
  26. public class FindFastest {
  27.  
  28. public static void main(String[] args) throws IOException {
  29. displayCopyRight("Michael Luo", "20180112", "1", "2017-09-20", "2017-09-29", "Find the Fastest Runner within the list");
  30. // Read the File
  31. Scanner reader = new Scanner(new File("runner.txt"));
  32. ArrayList<Player> list = new ArrayList<>();
  33.  
  34. // Read in the first Line
  35. String beginning = reader.nextLine();
  36.  
  37. // If file is less than a line
  38. if (beginning == null) {
  39. System.out.print("The number of runners must be larger than 1 !");
  40. System.exit(1);
  41. }
  42.  
  43. // Read in the data
  44. while (reader.hasNextLine()) {
  45. String[] parts = beginning.split(" ");
  46. Player NewPlayer = new Player(parts[0], Double.parseDouble(parts[1]));
  47. list.add(NewPlayer);
  48. beginning = reader.nextLine();
  49. }
  50.  
  51. // Sort
  52. Collections.sort(list);
  53.  
  54. // Output with condition: if there is only one runner.
  55. PrintWriter writer = new PrintWriter("result.txt");
  56. if (list.size() > 1) {
  57. writer.println("The fastest runner named " + list.get(0).getName() + " takes " + list.get(0).getTime() + " minute");
  58. writer.println("The second fastest runner named " + list.get(1).getName() + " takes " + list.get(1).getTime() + " minute");
  59. } else if (list.size() > 0) {
  60. writer.println("The fastest runner named " + list.get(0).getName() + " takes " + list.get(0).getTime() + " minute");
  61. }
  62.  
  63. writer.close();
  64. System.out.print("Result has printed in file result.txt");
  65. }
  66.  
  67. /*********************************************************************
  68. * * displayCopyRight
  69. *
  70. * * Purpose: show copyright information in the console
  71. * *
  72. * Input: Name, ID, Period, StartDate, DueDate, Purpose
  73. * *
  74. * Parameters parse in:
  75. * *
  76. * Output:
  77. * * void
  78. *
  79. *
  80. *********************************************************************/
  81. public static void displayCopyRight(String Name, String ID, String Period, String StartDate, String DueDate, String Purpose) {
  82. System.out.print("/********************************************************************* \n" +
  83. " * * \n" +
  84. " * " + Name + " StudentID: " + ID + " \n" +
  85. " * * \n" +
  86. " * AP Computer Science Java Period ? " + Period + " \n" +
  87. " * * \n" +
  88. " * Starting Date:" + StartDate + " Due Date: " + DueDate + " \n" +
  89. " * * \n" +
  90. " * " + Purpose + " \n" +
  91. " *********************************************************************/\n");
  92. }
  93.  
  94. }
  95.  
  96. class Player implements Comparable<Player> {
  97.  
  98. private String name;
  99. private Double time;
  100.  
  101. Player(String name, double time) {
  102. this.name = name;
  103. this.time = time;
  104. }
  105.  
  106. public int compareTo(Player player1) {
  107. return time.compareTo(player1.time);
  108. }
  109.  
  110. /*********************************************************************
  111. * * getName
  112. *
  113. * * Purpose: get private object attribute name
  114. * *
  115. * Input: null
  116. * *
  117. * Parameters parse in:
  118. * *
  119. * Output:
  120. * * String name
  121. *
  122. *
  123. *********************************************************************/
  124. public String getName() {
  125. return this.name;
  126. }
  127.  
  128. /*********************************************************************
  129. * * getTime
  130. *
  131. * * Purpose: get private object attribute time
  132. * *
  133. * Input: null
  134. * *
  135. * Parameters parse in:
  136. * *
  137. * Output:
  138. * * double time
  139. *
  140. *
  141. *********************************************************************/
  142. public Double getTime() {
  143. return this.time;
  144. }
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement