Question

In: Computer Science

Write the following java program: Desc Output the name and time of the runner who came...

Write the following java program:
Desc Output the name and time of the runner who came in first, as well as the name and time of
the runner who came in last in a marathon race (assuming there are no ties).
Input A text file named marathon.txt containing the name and time of each participant in the
following format (the file has at least 1 participant, name is just 1 word with no space, and name
and time are separated by tabs, blanks, and newlines):
John 2:40
Paul 3:20
Carl 2:10
Output The name and time of the runner who came in first, as well as the name and time of the
runner who came in last printed to the screen.

You must define a class called Runner. Here is the API of Runner:
A Runner object stores the name of a runner (String) and his raceTime (Time24).
Methods:
a. Usage: Runner()
Post: The Runner object initialized with name="unknown" and raceTime=0:0.
b. Usage: Runner (String s, Time24 t)
Post: The Runner object initialized with name=s and raceTime=t.
c. Usage: String getName()
Return: The name of the Runner object
d. Usage: Time24 getRaceTime()
Return: The raceTime of the Runner object.
e. Usage: void setName(String s)
Post: The Runner object's name set to s
f. Usage: void setRaceTime(Time24 t)
Post: The Runner object's raceTime set to t
g. Usage: void read(Scanner f)
Pre: f has a line in the following format ready to be read:

name hh:mm
where name is a String and hh, mm are integers. The token delimiters of f have
been set to white space characters and the colon by the caller.
Post: The line read in from f, the name and the time stored in the Runner object
h. Usage: int compareTo(Runner r)
Desc: Compare 2 Runner objects based on raceTime
Return: 1 if current object's raceTime > r's raceTime
0 if current object's raceTime == r's raceTime
-1 if current object's raceTime < r's raceTime

i. Usage: String toString()
Return: A String object in the form "name hh:mm"

Note:
 It is not necessary to save all the runners in an array or a vector (you only need to find the minimum
time and the maximum time).
Hand in:
 Marathon.java with 2 classes: Runner, and Marathon (use class Time24).

//Time24.java

import java.util.StringTokenizer;

import java.text.DecimalFormat;

/**

A data structure that stores integer values for hour (0..23) and minute (0..59) to represent the time of day in a 24-hour clock

*/

public class Time24

{

private int hour;

private int minute;

//Post: Sets the hour value in the range 0 to 23 and the minute value in the range 0 to 59

    private void normalizeTime()

    {

       int extraHours = minute / 60;

       minute %= 60;

       hour = (hour + extraHours) % 24;

    }

/**

Desc:Initializes this Time24 object

Post:hour and minute of this Time24 object both initialized to 0

*/

public Time24()

     {

         this(0,0); //calls the 2-argument constructor of class Time24

     }

/**

Desc:Initializes this Time24 object

Pre:h and m cannot be negative

Post:hour and minute of this Time24 object initialized to h and m

respectively. This operation will normalize the time if necessary (e.g.

9:75 is stored as 10:15).

Throw:IllegalArgumentException if h or m is negative

*/

public Time24(int h, int m)

    {

       setTime(h, m);

   }

/**

Desc:Sets the hour and minute of this Time24 object to a particular time

Pre:h and m cannot be negative

Post:hour and minute of this Time24 object set to h and m

respectively. This operation will normalize the time if necessary (e.g.

9:75 is stored as 10:15).

Throw:IllegalArgumentException if h or m is negative

*/

public void setTime(int h, int m)

    {

if (h < 0 || m < 0)

          throw new IllegalArgumentException("Time24.setTime: argument"

+ " must not be negative");

       this.hour = h;

      this.minute = m;

normalizeTime();

    }

/**

Desc:Adds minutes to this Time24 object

Pre:m cannot be negative

Post:This Time24 object set to m minutes later. This operation will

normalize the time if necessary (e.g. 9:75 is stored as 10:15).

Throw:IllegalArgumentException if m is negative

*/

public void addTime(int m)

    {

       if (m < 0)

          throw new IllegalArgumentException("Time24.addTime: argument"

+ " must not be negative");

       minute += m;

       normalizeTime();

    }

/**

Desc:Measures the interval from this Time24 object to another time

Return:The interval from this Time24 object to t as a Time24

*/

public Time24 interval(Time24 t)

    {

int currTime = hour * 60 + minute;

       int tTime = t.hour * 60 + t.minute;

if (tTime < currTime) tTime += 24 * 60;

return new Time24(0, tTime-currTime);

    }

/**

Desc:Gets the hour value of this Time24 object

Return:The hour value of this Time24 object

*/

public int getHour()

    {

return hour;

}

/**

Desc:Gets the minute value of this Time24 object

Return:The minute value of this Time24 object

*/

public int getMinute()

    {

return minute;

}

/**

Desc:Converts this Time24 object to a string

Return:This Time24 object as a String in the form "hh:mm"

*/

public String toString()

    {

DecimalFormat f = new DecimalFormat("00");

return hour + ":" + f.format(minute);

    }

/**

Desc:Convert a String to a Time24

Pre:s must be in the form "hh:mm" where hh and mm are positive integers

Return:A Time24 object that corresponds to s

*/

public static Time24 parseTime(String s)

    {

StringTokenizer t = new StringTokenizer(s, ":");

int h = Integer.parseInt(t.nextToken());

int m = Integer.parseInt(t.nextToken());

return new Time24(h, m);

}

}


Hint:
class Runner
{
private String name;
private Time24 raceTime;
public Runner()
{
name="unknown";
raceTime=new Time24(0,0);

}
public Runner(String s, Time24 t)
{
name=s;

raceTime=new Time24(t.getHour(), t.getMinute());

}
public String getName()
{

return name;
}
public Time24 getRaceTime()
{

return new Time24(raceTime.getHour(), raceTime.getMinute());
}
……
}
class Marathon
{
public static void main(String[] args) throws FileNotFoundException
{
…….
}
}

Solutions

Expert Solution

Runner Class:

import java.util.Scanner;
public class Runner
{
    private String name;
    private Time24 raceTime;
    public Runner()
    {
        name="unknown";
        raceTime=new Time24(0,0);
    }
    
    public Runner(String s, Time24 t)
    {
        name=s;
        raceTime=new Time24(t.getHour(), t.getMinute());
    }
    
    public String getName()
    {
        return name;
    }
    
    public Time24 getRaceTime()
    {
        return new Time24(raceTime.getHour(), raceTime.getMinute());
    }
    public void read(Scanner f)
    {
        String s = f.nextLine();
        String words[] = s.split(" ");
        name = words[0];
        raceTime = Time24.parseTime(words[1]);
    }
    public int compareTo(Runner r){
        if(raceTime.getHour()>r.getRaceTime().getHour()){
            return 1;
        }else if(raceTime.getHour()<r.getRaceTime().getHour()){
            return -1;
        }else{
            if(raceTime.getMinute()>r.getRaceTime().getMinute()){
                return 1;
            }else if(raceTime.getMinute()<r.getRaceTime().getMinute()){
                return -1;
            }else{
                return 0;
            }
        }
    }
    public String toString(){
        return name + " "+raceTime.toString();
    }
}

Marathon class:

import java.io.*;  
import java.util.Scanner;
public class Marathon
{
        public static void main(String[] args) {
            Runner first=null;
            Runner last=null;
            try{
            FileInputStream fis=new FileInputStream("marathon.txt");       
            Scanner sc=new Scanner(fis); 
            while(sc.hasNextLine())  
            {  
                if(first ==null){
                    first = new Runner();
                    first.read(sc);
                    last = new Runner(first.getName(),first.getRaceTime());
                }else{
                    Runner temp = new Runner();
                    temp.read(sc);
                    if (temp.compareTo(first)==-1){
                        first = temp;
                    }else if(temp.compareTo(last)==1){
                        last = temp;
                    }
                }
                
            }  
            sc.close(); 
            System.out.println("first runner:");
            System.out.println(first.toString());
            System.out.println("last runner:");
            System.out.println(last.toString());
            }
            catch(IOException e)  
        {  
            e.printStackTrace();  
        }  
        }
}

Related Solutions

We have a list of runner and their running time, write a program in Java to...
We have a list of runner and their running time, write a program in Java to show the fastest runner and their time.
Write a program to perform the following actions: the language is Java – Open an output...
Write a program to perform the following actions: the language is Java – Open an output file named “Assign14Numbers.txt” – Using a do-while loop, prompt the user for and input a count of the number of random integers to produce, make sure the value is between 35 and 150, inclusive. – After obtaining a good count, use a for-loop to generate the random integers in the range 0 ... 99, inclusive, and output each to the file as it is...
Write a machine language program to output your name on the output device. The name you...
Write a machine language program to output your name on the output device. The name you output must be longer than two characters. Write it in a format suitable for the loader and execute it on the Pep/9 simulator. my name is Kevin
Write a Java program for a restaurant with the following features: ◦ Customer: Name, Surname, ID...
Write a Java program for a restaurant with the following features: ◦ Customer: Name, Surname, ID (incremental ID by 1 for each new customer), Email, Phone, Address. ◦ Service: ID (incremental ID by1 for each group),CustomerID, Priority (High, Medium, Low, Other), ResolutionTimeFrame (Measured in Man hours), AssignedUser, Status(resolved or not), Fee. ◦ User (simple user of system): ID, Name, Surname, Username and Password (insert from code five fixed users), Address , PhoneNumber ◦ Manager: Name, Surname, Username and Password (insert...
Write a complete Java program to print out the name bob
Write a complete Java program to print out the name bob
Write a Java program thatdoes the following jobs.Step1. Creates a Schooltable with name, score, rating, and...
Write a Java program thatdoes the following jobs.Step1. Creates a Schooltable with name, score, rating, and district.Step2. Reads data from theCSVfile(elementrayschool.csv)and insert the schoolsinto the table created in Step 1.Step3. Interact with the user, wheretheusercan select one of the following actions.-(Q) Quit: quit the program-(A) Add a school-(C) Calculate average rating-(S) Print a subset of the schools based on score-(D) Print a subset of the schools based on districtThe user can choose one of these actions bytyping Q,A,C, S, or...
Using jGRASP, write a Java program named LastnameFirstname10.java, using your last name and your first name,...
Using jGRASP, write a Java program named LastnameFirstname10.java, using your last name and your first name, that does the following: Create two arrays that will hold related information. You can choose any information to store, but here are some examples: an array that holds a person's name and an array that hold's their phone number an array that holds a pet's name and an array that holds what type of animal that pet is an array that holds a student's...
Using jGRASP, write a Java program named LastnameFirstname09.java, using your last name and your first name,...
Using jGRASP, write a Java program named LastnameFirstname09.java, using your last name and your first name, that does the following: Declare an array reference variable called myFavoriteSnacks for an array of String type. Create the array so that it is able to hold 10 elements - no more, no less. Fill the array by having each array element contain a string stating one of your favorite foods/snacks. Note: Only write the name of the snack, NO numbers (i.e. Do not...
Write a JAVA program that prompts the user to enter a single name. Use a for...
Write a JAVA program that prompts the user to enter a single name. Use a for loop to determine if the name entered by the user contains at least 1 uppercase and 3 lowercase letters. If the name meets this policy, output that the name has been accepted. Otherwise, output that the name is invalid.
Write a Java program that allows the user to specify a file name on the command...
Write a Java program that allows the user to specify a file name on the command line and prints the number of characters, words, lines, average number of words per line, and average number of characters per word in that file. If the user does not specify any file name, then prompt the user for the name.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT