Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*********************************************************************
- * * Find Fastest
- * copyright 2017-2018
- * class P1
- *
- * Find the fastest runner from a list
- * *
- * Input: name (space) time
- * *
- * Parameters parse in: A file called "runner.txt"
- * *
- * Output:
- * *
- * a file called "result" with the fastest runner and possible second fastest runner
- *
- *********************************************************************/
- package com.company.cs;
- import java.io.File;
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.util.*;
- public class FindFastest {
- public static void main(String[] args) throws IOException {
- displayCopyRight("Michael Luo", "20180112", "1", "2017-09-20", "2017-09-29", "Find the Fastest Runner within the list");
- // Read the File
- Scanner reader = new Scanner(new File("runner.txt"));
- ArrayList<Player> list = new ArrayList<>();
- // Read in the first Line
- String beginning = reader.nextLine();
- // If file is less than a line
- if (beginning == null) {
- System.out.print("The number of runners must be larger than 1 !");
- System.exit(1);
- }
- // Read in the data
- while (reader.hasNextLine()) {
- String[] parts = beginning.split(" ");
- Player NewPlayer = new Player(parts[0], Double.parseDouble(parts[1]));
- list.add(NewPlayer);
- beginning = reader.nextLine();
- }
- // Sort
- Collections.sort(list);
- // Output with condition: if there is only one runner.
- PrintWriter writer = new PrintWriter("result.txt");
- if (list.size() > 1) {
- writer.println("The fastest runner named " + list.get(0).getName() + " takes " + list.get(0).getTime() + " minute");
- writer.println("The second fastest runner named " + list.get(1).getName() + " takes " + list.get(1).getTime() + " minute");
- } else if (list.size() > 0) {
- writer.println("The fastest runner named " + list.get(0).getName() + " takes " + list.get(0).getTime() + " minute");
- }
- writer.close();
- System.out.print("Result has printed in file result.txt");
- }
- /*********************************************************************
- * * displayCopyRight
- *
- * * Purpose: show copyright information in the console
- * *
- * Input: Name, ID, Period, StartDate, DueDate, Purpose
- * *
- * Parameters parse in:
- * *
- * Output:
- * * void
- *
- *
- *********************************************************************/
- public static void displayCopyRight(String Name, String ID, String Period, String StartDate, String DueDate, String Purpose) {
- System.out.print("/********************************************************************* \n" +
- " * * \n" +
- " * " + Name + " StudentID: " + ID + " \n" +
- " * * \n" +
- " * AP Computer Science Java Period ? " + Period + " \n" +
- " * * \n" +
- " * Starting Date:" + StartDate + " Due Date: " + DueDate + " \n" +
- " * * \n" +
- " * " + Purpose + " \n" +
- " *********************************************************************/\n");
- }
- }
- class Player implements Comparable<Player> {
- private String name;
- private Double time;
- Player(String name, double time) {
- this.name = name;
- this.time = time;
- }
- public int compareTo(Player player1) {
- return time.compareTo(player1.time);
- }
- /*********************************************************************
- * * getName
- *
- * * Purpose: get private object attribute name
- * *
- * Input: null
- * *
- * Parameters parse in:
- * *
- * Output:
- * * String name
- *
- *
- *********************************************************************/
- public String getName() {
- return this.name;
- }
- /*********************************************************************
- * * getTime
- *
- * * Purpose: get private object attribute time
- * *
- * Input: null
- * *
- * Parameters parse in:
- * *
- * Output:
- * * double time
- *
- *
- *********************************************************************/
- public Double getTime() {
- return this.time;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement