javadoc
of the provided code. Be sure and read this documentation
carefully.
Warning: Although you are only implementing a single class, there is much more to do than in assignment 1. Start early and ask questions!
You will learn how to decompose a problem into a set of smaller problems. You will then solve the smaller problems one at a time. You will code a smaller problem and then test it. When one is complete, you will move on to the next one. When all of the smaller problems are complete, the main problems can be expressed in terms of the smaller ones.
The assignment consists of of four files. The four files are:
Shell.java
This file was used
in previos assignments. (Do not modify).TestUTC.java
This is the
test drvver for this assignment. (do not modify.)UTCintf.java
This
interface
defines the methods you will implement.
(Do not modify.)MyUTC.java
This is the file
you will complete.Here is the documentation for the classes. Study the documentation for details on how to comlete your implementation.
MyUTC
classMyUTC.java
file given above.
This class will provide an implementation of an object that represents a date
and time. The class contains multiple constructors and multiple public methods
It contains various many (15+) helper methods. They are declared
public
so that they many be tested independently.
The MyUTC
class contains only two instance variables,
an integer secondsSince1970
. and a boolean valid
.
A value of 0
for secondsSince1970
means
the date
Jan 1, 1970 0:00:00
. The date Jan 1,1970 0:01:00
would have an intance variable value of 60
and means one minute
after midnight. Similarly, the date Dec 31, 1969 23:59:00
would
have an instance variable of -60
.
For practice, assume you were born in 1970. Take the month and day of your birthday and compute the day of the year of your birthday. You can then compute the number of seconds by multiplication. Now assume you were born in 1972 (the first leap year after 1970). Two complete years have gone by and some number of days in 1972. Again, you can compute the seconds by multiplication.
Next assume you were born in 1969. Figure out the seconds to the beginning of 1969 (a negative number) and then add back the seconds until your birthday. Then assume you were born in 1968, the first leap year before 1970), and do the same computation. Try to understand how this works in general. One you do, you will understand how to write code for this assignment.
Save Target As ...
.
int
or -1
on an error, set up a local variable
and initialize it to -1
. When all tests are passed, assign
it its final value. The last line of the method may be a return of the
variable.
cumulDays
and then
put the values in the instance variable.defaultFormat
.MyUTC.fitsIntInt()
and check it using the
fits
command.MyUTC(long secondsSince1970)
. This
is easy once you have written the previous method.MyUTC.isLeapYear()
and check it using the
leap
command.MyUTC.isValidHour()
and check it using the
hour
command.MyUTC.isValidMinOrSec()
and check it using the
min
command.MyUTC.isValidMonth()
and check it using the
month
command.MyUTC.isValidMonthDay()
and check it using the
valid
command.MyUTC.isValidDayOfYear()
and check it using the
valid
command.MyUTC.getMonth()
and check it using the
month
command.MyUTC.getDay()
and check it using the
day
command.MyUTC.daysUntilMonth()
and check it using the
dum
command.MyUTC.dayOfYear(int, int, boolean)
and check it
using the doy
command.MyUTC.getYearFromStr()
and check it using the
year
command.MyUTC.hmsStrToSec()
and check it using the
hms
command.MyUTC.daysSince1970()
and check it using the
duy
command.MyUTC.dayOfYear(String, boolean)
and check it using
the doy
command.
MyUCT(String)
- the constructor which takes a string and
determines the number of seconds in time that represents this date.
There is a section below on the constructor.toString(format)
which decomposes the seconds back int a
month, day, ... depending on what the user has asked for via the
format
. There is a section below on the method.
You may tackle these in either order. Since you may construct a
MyUTC
object using a number you can obtain from the linux
date
command, it may be easier to start with the
toString(format)
method.
MyUTC.newUTC()
. MyUTC.diff()
,
MyUTC.equals()
and MyUTC.setFormat().
input
file to test them.toString(format)
method
You must also implement a toString()
that takes no parameters.
You should understand why this is necessary. You may write this method as
a single line of code that uses the toString(format)
method.
Before you complete the code this, simply make the
toString(format)
method return the secondsSince1970
as a string. You may then tell if your
constructor is computing the correct value.
As a start, you will want to determine the following (althought not necessarily
in this order):
Math.floor()
useful.
The remaining seconds can be converted to hours, minute and second.
Just try to do this and test it.
Now you can use the days to find the year and once the year is found, you can compute the day of the year. Then the day of the year can be broken down into a month and day.
Most all of this can be done with helper methods you already completed.
The last part is to scan thought the format
string and build
the return value. When you look at a character in the format
,
if it is not an %
just add it to the return value. If it is an
%
you look at the next character to determine what piece of
information to add to threturn string. All the characters are defined in
the javadoc
. When you have completely scanned the
format
, you return the value you have built up.
MyUTC(String)
toString()
method. You
begin with a string value and extract from it various values such as
month, day, year, ... The allowable formats are defined in the
javadoc
. Once you have extracted the values, you may combine
them numerically to produce the instance variable input
this file
and it will automatically run every test case.
To check your work, you may use the linux
date
command to test your
code. It provides all the functionality this class requires and more. To find
out how to use the command type man date
and read the instructions.
As an example, enter (in a terminal):
date -u -d"1/31/2011 11:30:15 GMT" "+%a totalSec: %s dayOfYear: %j"
and you should see:
Mon totalSec: 1296473415 dayOfYear: 031
This means that the day of the week is Mon
and that the
secondsSince1970 is 1296473415
. and the day of the year is
31
.
Another example:
date -d"11/22/1963 18:59 GMT" "+%a totalSec: %s dayOfYear: %j"
Fri totalSec: -192776460 dayOfYear: 326
If you wrote the helper methods, you may use this input
file to test them.
MyUTC.java
using the Checkin tab of the
course website. Alteratively, at a terminal type:
~csXXX/bin/checkin XXX MyUTC.java