Java代写:CS142 Contest

代写Java基础作业,解析比赛结果文件,并格式化输出。

Program Overview

Pretend that Skagit Valley College holds a Red-N-White Day competition each spring quarter. There are two events: a cow-chip toss and a one-legged race. The results of the 2017 contest is contained in a file named contest2017.txt. The data for each contestant is contained on a pair of lines in the file. The first line of each pair contains the contestant’s name. The second line of each pair contains the feet of toss distance and the seconds of race time. Here are a few of the line pairs in the file:

AKERS JONATHAN N
20.9 66.3
ALEXANDER COLTON J
22.1 52.9
...
WILLIAMS KEANDRE M
24.9 66.4
ZHENG YANG
23.4 54.3

I used our CS 142 class roster and a random-number generator to generate contest results, so don’t be insulted if you got a lousy score!

WARNING: Don’t rely on Notepad to view the file. It does not reliably recognize line breaks. Use jGRASP, Wordpad or some other text editor.

You will write a Java program in a file named ContestLab.java that will read the data from the file and write a contest report, both on the screen and into a file named report2017.txt. A sample I/O session, showing only a portion of the screen report, is below. User input is shown underlined only for easy identification, and is not part of the I/O session.

input file name? contest.dat
File not found. Try again.
input file name? contest2017
File not found. Try again.
input file name? contest2017.txt
                       CONTEST DATA
============================================================
  Contestants                     Distance         Time
============================================================
  AKERS JONATHAN N                20.90 ft      66.30 sec
  ALEXANDER COLTON J              22.10 ft      52.90 sec
  BARTLETT KATIE L                22.30 ft      66.00 sec
  CHAN HOIYIN                     19.30 ft      69.50 sec
  CHEN ZEKUN                      24.20 ft      51.70 sec
  ...
  SUPINEE PONGPOB                 15.10 ft      58.00 sec
  TIMLICK CHRISTOPHER J           19.10 ft      68.00 sec
  TRAMMELL REID M                 19.40 ft      49.20 sec
  VALENCIA RYAN S                 24.00 ft      56.70 sec
  WANG GUANGHAI                   16.80 ft      68.50 sec
  WILLIAMS KEANDRE M              24.90 ft      66.40 sec
  ZHENG YANG                      23.40 ft      54.30 sec
============================================================
Total number of contestants: 34
Average race time: 60.83 secs
Average toss distance: 20.52 ft
Best race time: 49.20 secs by TRAMMELL REID M
Best toss distance: 25.00 ft by SANTIAGO JENNIFER

Lab 6 Checkpoint

You cannot produce a report until you are able to successfully read the data from the contest2017.txt file into memory. In addition to defining and calling the boilerplate code for the getInput() method on lines 21-34 of the CountWords2 program at the end of section 6.4, write the minimal amount of code necessary to produce the I/O session shown below. User input is shown underlined only for easy identification.

input file name? contest.dat
File not found. Try again.
input file name? contest2017.txt
Contestant AKERS JONATHAN N has distance of 20.9 and time of 66.3
Contestant ALEXANDER COLTON J has distance of 22.1 and time of 52.9
Contestant BARTLETT KATIE L has distance of 22.3 and time of 66.0
Contestant CHAN HOIYIN has distance of 19.3 and time of 69.5
Contestant CHEN ZEKUN has distance of 24.2 and time of 51.7
Contestant COCHEBA SPENCER K has distance of 24.3 and time of 68.9
Contestant DAILEY JAKOB D has distance of 20.3 and time of 51.2
Contestant DIETERICH ROGER M III has distance of 20.2 and time of 54.6
Contestant FEHRINGER LOGAN M has distance of 23.4 and time of 57.5
Contestant HEIMER GRAHAM C has distance of 18.7 and time of 69.8
Contestant HEPPNER JORDON P has distance of 22.1 and time of 64.3
Contestant IRCINK NATHALIE C has distance of 19.9 and time of 63.0
Contestant LAGUA MARVIN G has distance of 22.3 and time of 53.2
Contestant LAMBETH JEREMIAH A has distance of 23.7 and time of 50.5
Contestant LIN SICHEN has distance of 16.3 and time of 68.3
Contestant MARTINEZ JOSHUA has distance of 22.4 and time of 57.0
Contestant NEW NATHAN T has distance of 19.0 and time of 55.0
Contestant NII YOHEI has distance of 18.3 and time of 65.5
Contestant NIX ETHAN V has distance of 16.3 and time of 69.2
Contestant PALMER MATTHEW J has distance of 23.3 and time of 54.6
Contestant PALUMBO ANTHONY V has distance of 20.3 and time of 58.0
Contestant PENG CHENGTING has distance of 19.9 and time of 67.5
Contestant PERRY KENDALL J has distance of 16.2 and time of 64.0
Contestant RAIGUEL DUSTIN J has distance of 21.4 and time of 63.3
Contestant RIPLEY RONNIE C has distance of 15.2 and time of 61.8
Contestant SANTIAGO JENNIFER has distance of 25.0 and time of 66.5
Contestant STEFFENS NATHANIEL R has distance of 17.6 and time of 57.0
Contestant SUPINEE PONGPOB has distance of 15.1 and time of 58.0
Contestant TIMLICK CHRISTOPHER J has distance of 19.1 and time of 68.0
Contestant TRAMMELL REID M has distance of 19.4 and time of 49.2
Contestant VALENCIA RYAN S has distance of 24.0 and time of 56.7
Contestant WANG GUANGHAI has distance of 16.8 and time of 68.5
Contestant WILLIAMS KEANDRE M has distance of 24.9 and time of 66.4
Contestant ZHENG YANG has distance of 23.4 and time of 54.3

Note: Since main() will instigate the opening of a file, it must account for the possibility of throwing a checked FileNotFoundException. This was discussed in 6.1.

WARNING: Be prepared for your first attempt to blow up after reading and displaying the information about the first student in the file. This is because you are probably using a mix of token-based input using nextDouble(), and line-based processing using nextLine().

HINT: You must consume the \n following the time score, so that when you go to read the next name you don’t end up with the empty string. This happens because the nextLine() method reads all characters from the current input cursor position up to the \n. If the input cursor happens to be right under \n (as it would be after using nextDouble() to input the time), it just moves past the \n and returns “” (the empty string).

Suggested Algorithm for Processing the File

  1. Use the getInput() method to create a Scanner object connected to the contest file.
  2. While the contest file still has a line of data to be read
    • a. Read the entire name from the next line in the file
    • b. Read the distance from the file
    • c. Read the time from the file
    • d. If there is still a line of data to read, consume \n following the time
    • e. Display the contestant data just read in the checkpoint format shown above

End of Lab 6 Checkpoint Specifications

Final Lab 6 Specifications

As with previous labs, your ContestLab.java program will be graded on functionality, implementation and style.

Functionality Requirements

  • The program allows the user to input the name of the file to be processed. It repeatedly asks for the file name until it can successfully open the file named by the user.
  • You may assume that the file contains only valid data in the format described above in the program overview.
  • The program generates a screen report with the following properties:
    • The report is 60 characters wide.
    • All scores are displayed with two digits to the right of the decimal point followed by the unit ft or sec as shown in the sample I/O session
    • The names are displayed left-justified in a field of 30 spaces on the screen
    • The report begins with a 4-line heading as shown in the sample I/O session
    • The CONTEST DATA heading is centered over the report.
    • At the end of the detail report is the =========== separator followed by a report summary.
    • The report summary is formatted as shown in the sample I/O session
    • If the names and scores and number of contestants in the file were to change, the report should change correspondingly. That is, you should not assume there will always be 34 contestants in the file.
  • The program generates a report in a file named report2017.txt. The file report is formatted the same as the screen report.

Implementation/Language Usage Requirements

  • All counters, accumulators, Scanner objects and PrintStream objects should be declared and initialized in main(), EXCEPT
    • you may declare the Scanner object connected to System.in at the class level so you don’t have to pass it as a parameter to methods that perform keyboard input.
  • Define and call the boilerplate code for the getInput() method on lines 21-34 of the CountWords2 program at the end of section 6.4
  • Your program should contain a public static method with the following header:
    void displayReportHeading(PrintStream output)
    This method is responsible for displaying the first four (4) lines of the report to whatever PrintStream object is passed as a parameter. This allows the same method to be used to display the headings to System.out or to a file. The FixSpacing program in 6.4 does this by calling the echoFixed() method, once with the output PrintStream object, and again with the System.out object.
  • Your program should contain a public static method with the following header:
    String raceWinner(String currentBest, double newTime, String newName)
    • The currentBest parameter contains the best race score so far, concatenated with a space, followed by the name of the contestant who earned that score.
    • If the newTime is better than the currentBest time, return the newTime concatenated with a space, followed by the newName;
    • otherwise return the currentBest.
    • REMINDER: The fastest time (shortest time) is the best time.
      This clever technique of returning two values from a method by putting them together in a string object was used in the find() method of the 6.5 Case Study. REMINDER: The fastest time is the best time.
  • Your program should contain a public static method with the following header:
    String tossWinner(String currentBest, double newDistance, String newName)
    • The currentBest parameter contains the best toss score so far, concatenated with a space, followed by the name of the contestant who earned that score.
    • If the newDistance is better than the currentBest distance, return the newDistance concatenated with a space, followed by the newName;
    • otherwise return the currentBest.
    • REMINDER: The furthest toss (longest toss) is the best toss.
  • Your program should contain a method for displaying the summary at the end of the report. It should accept a PrintStream parameter so that it can display the summary to the screen or a file.
    • You must determine the other parameters that this method will need to be passed when it is called.
  • You are welcome to define and call other methods that you may find helpful to reduce redundancy or simplify the logic in your other methods.

Style Requirements

Your programs should incorporate the following style elements for this lab:

  • comments at the top of the file include the following
    • name of programmer and purpose of program
    • total hours spent on assignment including class time, reading time, video watching, quizzes, etc.
  • appropriate and consistent spacing and blank lines to enhance readability
  • appropriate and consistent indentation of class bodies, method bodies, if/else bodies and loop bodies
  • meaningful, self-documenting names for variables, constants and methods
  • named class constants, if applicable, that make code more readable
  • comments strategically placed throughout the code to highlight major logic
  • procedural decomposition: No method (including main()) has more than 20 lines of code, not counting comments, blank lines and lines containing single { or } characters. If a method gets too long, subdivide it.
  • Except for a class-level Scanner object connected to System.in, all other variables and objects should be declared at the most limited scope possible.