In: Computer Science
//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);
}
}
Add 1 method, readTime, to class Time24.
Usage: void readTime(Scanner f)
Desc: Read from f the hour and minute for this Time24 object
Pre: f is ready to be read, and the next line contains hh:mm where hh:mm gives a valid 24-
hour time
Post: The entire line hh:mm read in from f.
The hour and minute of this Time24 object set to hh and mm respectively.
The operation will normalize the time if necessary (e.g. 9:75 is stored as 10:15).
import java.util.Scanner;
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);
}
/**
* Desc: Read from f the hour and minute for this
Time24 object
* Pre: f is ready to be read, and the next line
contains hh:mm where hh:mm gives a valid 24-hour time
* Post: The entire line hh:mm read in from f.
* The hour and minute of this Time24 object set to hh
and mm respectively.
* The operation will normalize the time if necessary
(e.g. 9:75 is stored as 10:15).
*/
public void readTime(Scanner f)
{
// read the entire line from f and
set it to line
String line = f.nextLine();
Time24 obj = parseTime(line); //
parse the line using the method parseTime and set the resultant
object to obj
// update hour and minute to obj's
hour and minute
this.hour = obj.hour;
this.minute = obj.minute;
}
}