Decomposition, constructors and more

Essentials

Due:
Key:


Overview

This assignment deals with the respresentation of a point in time. There are a variety of ways of representing time. This assignment will follow the Linux model. Much of the information about this assignment is contained in the 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:

Here is the documentation for the classes. Study the documentation for details on how to comlete your implementation.


The MyUTC class

A skeleton for this class is provided in the MyUTC.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.


Getting started

There is a lot to do in this assignment, but if you do the work logically and incrementally, you will save time. Complete and test the routines one at a time. Various routines will call other routines to complete their work. Here are a few suggestions to get you going.

The toString(format) method

The purpose of this method is to convert the object to a human readable form.

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):

Lets see how you might start. Given the number of seconds, you can easily convert this to an integer number of days and the remaining seconds. Since the value may be negative, you may find the 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.


The constructor MyUTC(String)

This method is the "opposite" of the 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 .

Testing

As you complete a routine, create one or more test cases to verify that it works. If you put all these in a file, you can 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.


Submitting Your Assignment

Check in the single file MyUTC.java using the Checkin tab of the course website. Alteratively, at a terminal type:

  ~csXXX/bin/checkin XXX MyUTC.java

(c) Fritz sieker 2010-2015