In: Computer Science
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
{
…….
}
}
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();
}
}
}