javadocof 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.javaThis file was used in previos assignments. (Do not modify).
TestUTC.javaThis is the test drvver for this assignment. (do not modify.)
interfacedefines the methods you will implement. (Do not modify.)
MyUTC.javaThis 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.javafile 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
publicso that they many be tested independently.
MyUTC class contains only two instance variables,
secondsSince1970. and a boolean
A value of
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
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 ....
-1on 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.
cumulDaysand then put the values in the instance variable.
MyUTC.fitsIntInt()and check it using the
MyUTC(long secondsSince1970). This is easy once you have written the previous method.
MyUTC.isLeapYear()and check it using the
MyUTC.isValidHour()and check it using the
MyUTC.isValidMinOrSec()and check it using the
MyUTC.isValidMonth()and check it using the
MyUTC.isValidMonthDay()and check it using the
MyUTC.isValidDayOfYear()and check it using the
MyUTC.getMonth()and check it using the
MyUTC.getDay()and check it using the
MyUTC.daysUntilMonth()and check it using the
MyUTC.dayOfYear(int, int, boolean)and check it using the
MyUTC.getYearFromStr()and check it using the
MyUTC.hmsStrToSec()and check it using the
MyUTC.daysSince1970()and check it using the
MyUTC.dayOfYear(String, boolean)and check it using the
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
inputfile to test them.
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
Before you complete the code this, simply make the
toString(format) method return the
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
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
javadoc. When you have completely scanned the
format, you return the value you have built up.
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
inputthis file and it will automatically run every test case. To check your work, you may use the linux
datecommand to test your code. It provides all the functionality this class requires and more. To find out how to use the command type
man dateand 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
1296473415. and the day of the year is
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
file to test them.
MyUTC.javausing the Checkin tab of the course website. Alteratively, at a terminal type:
~csXXX/bin/checkin XXX MyUTC.java