Lab: Experience with the Programming Process - Before Writing Code
Instructions:
Answer the following questions one at a time. After answering each question,
check your answer (by clicking on the check-mark icon if it is available)
before proceeding to the next question.
1. Understanding the Problem Domain:
This part of the lab will help you realize how important it is to
understand the problem domain at the start of the programming process
(and long before you start writing code).
-
Read and understand the document describing
different techniques for representing the time of day.
-
What character is used to delimit the hours, minutes, and seconds
in a time of day?
A colon (i.e., the : character).
-
How is "midnight" represented in military time and civilian time?
00:00:00 in military time and 12:00:00AM in civilian time.
-
How is "noon" represented in military time and civilian time?
12:00:00 in military time and 12:00:00PM in civilian time.
-
What is the last time of day in military time and civilian time?
23:59:59 in military time and 11:59:59PM in civilian time.
-
What important difference is there in the way the 1-digit hours
are presented in military time and civilian time?
Though they both are presented in a 2-character field, the leading character
in military time is a 0 whereas it is a space in civilian time.
2. Decompose the Problem into Parts:
This part of the lab will help you understand why it is important to decompose
a problem into parts and give you some experience doing so.
-
Read and understand the
Software Requirements Specification (SRS) for TimeOut
-
What are the important functions that TimeOut must perform?
1. It must determine the current time.
2. It must print the current time using a civilian representation.
3. It must print the current time using a military representation.
4. It must convert String
command-line parameters to
int
values.
5. It must print the command-line parameters.
6. It must calculate the time in the future/past.
7. It must print the future/past time using a civilian representation.
8. It must print the future/past time using a military representation.
-
Break the problem of formatting a time into subproblems.
1. It must determine if the hour/minute/second has 1 or 2 digits.
2. It must pad 1-digit numbers with either a leading space or a
leading 0.
3. Solve Each Part of the Problem by Hand:
This part of the lab will help you understand why it is important to
solve each part of the problem by hand before writing code in a particular
language.
-
Suppose the current time is 11:05:28AM. What is the time
+22min in the past/future.
This one is pretty easy: 11:27:28AM.
-
Suppose the current time is 11:05:28AM. What is the time
+57min in the past/future.
This one is a little harder. Proceeding as in the previous example
yields 11:62:28AM. But, we know that the minutes must be in the
interval [0, 59] so we have to "carry" into the hour. 62/60 is 1 and
62%60 is 2, so we must increase the hour by 1 and set the minute to
2. But, increasing the hour by 1 also changes AM to PM. So, the answer
is 12:02:28PM.
-
Suppose the current time is 11:05:28AM. What is the time
+1hr, -6min, +5sec in the past/future.
This one is harder still. Blindly adding/subtracting yields 12:-01:33PM which
doesn't make sense and I'm a little worried about using the / and % operators
with a negative number. However, it looks like it should be 11:59:33AM.
-
Could you come up with even more complicated examples?
Yes, have the signs be different, and the absolute value of the hours, minutes, and seconds be be more than 24, 60 and 60 respectively.
-
Should you start writing code in a programming language yet?
No, it would be a disaster. If you can't solve the problem by hand you can't
even give instructions to another person, let alone to a computer.
-
How should you proceed at this point?
There are two possibilities. You can either work harder on this
approach or you can consider a different approach. We're going to try
a different approach.
-
Rather than working with times represented as hours, minutes and
seconds, we're instead going to work with times represented as
seconds since midnight. With that in mind, how many seconds since midnight
is 11:05:28AM?
11hr * 3600sec/hr + 5min * 60sec/min + 28sec = 39928
-
Working backwards, what hour, minute and second of the day (in
military time) correspond to 39928 seconds since midnight?
The hour is 39928sec / 3600sec/hr = 11hr
For the minute we first must calculate the seconds remaining after
this division
(i.e., 39928sec % 3600sec/hr = 328sec) and then divide this by 60sec/min.
Hence, the minute is 328sec / 60sec/min = 5min.
For the second we need the seconds remaining after this division.
Hence, the second is 328sec % 60sec/min = 28sec.
-
How many seconds are in +1hr, -6min, +5sec?
This is easy: 1hr * 3600sec/hr - 6min * 60sec/min + 5sec = 3245.
-
Using the previous answers, how many seconds since midnight
is 11:05:28AM changed by +1hr, -6min, +5sec?
39928 + 3245 = 43173
-
Working backwards, what hour, minute and second of the day (in
military time) correspond to your answer to the previous question?
43173sec / 3600sec/hr = 11
(43173sec % 3600sec/hr) / 60sec/min = 3573sec / 60sec/min = 59
3573sec % 60sec/min = 33
-
Which approach seems easier and less error-prone?
The second approach, so that's what we'll use.
-
Is it time to start writing Java code?
No (we're not even close to ready).
-
Consider the civilian time 11:05:28AM. What is the equivalent
military time?
This one is easy: 11:05:28
-
Consider the civilian time 3:05:28PM. What is the equivalent
military time?
We have to add 12 to the hour, so 15:05:28
-
Consider the military time 09:05:28. What is the equivalent
civilian time?
Since the hour is greater than 12 we know that the time is before noon
(i.e., AM). So, the answer is 9:05:28AM.
-
Consider the military time 16:05:28. What is the equivalent
civilian time?
Since the hour is greater than 12 we know that the time is after noon
(i.e., PM) and we have to subtract 12 from the hour. So, the answer is
4:05:28PM.
-
Consider the military time 00:05:28. What is the equivalent
civilian time?
Since the hour is less than 12 we know that the time is before noon
(i.e., AM). However, since the hour is exactly 0 we know that we are in the hour immediately after midnight. So, the answer is 12:05:28AM.
-
In general, when the military hour is less than 12, what is the
civilian hour? Is it during the AM or PM half of the day?
The civilian hour is the same as the military hour. It is during the
AM half of the day.
-
There is one exception to the answer to the previous question. What is it?
When the military hour is 0 then the civilian hour is 12 (still in the
AM half of the day).
-
In general, when the military hour is greater than or equal to 12,
what is the civilian hour? Is it during the AM or PM half of the day?
The civilian hour is the military hour minus 12. It is during the
PM half of the day.
-
There is one exception to the answer to the previous question. What is it?
When the military hour is 12 then the civilian hour is also 12 (still
in the PM half of the day).
-
Is it time to start writing Java code?
No. Look back at the different problems we have to solve and you'll see that
there are two we haven't considered yet.
-
How many digits are in the number -11, -6, 0, 6, 11, and 22 respectively?
2, 1, 1, 1, 2 and 2.
-
Mathematically, what is true of numbers with exactly one digit?
Their absolute value is in the interval [0, 9].
-
Mathematically, what is true of numbers with exactly
two digits?
Their absolute value is in the interval [10, 99].
-
Is it time to start writing Java code?
If you were proficient Java programmers you could start writing code now.
However, since you're not, its important that you first understand the
language features that you will be making use of.
4. Understand the Relevant Features of the Language:
This part of the lab will help you review the relevant features of the
Java programming language that you will need to understand when you
start writing code.
-
What are the two kinds of members in a class?
Attributes and methods.
-
What does it mean when we say that a member of a class is
static
?
It means it belongs to the class, not to individual objects in that
class. Hence, it can be referred to using the class name and not the
identifier for a particular object.
-
When should an attribute be declared
final
?
When it can't be the left-side operand of an assignment operator more than
once.
-
What is the advantage of declaring an attribute
final
?
The compiler will prevent you from mistakenly assigning a value to the attribute.
-
Why is it better to include a method in a class and call it
from multiple locations rather than duplicate the code
in each of those locations?
Code often contains defects. So, duplicate code often duplicates
defects. Then, even if you correct a defect in one copy of the code,
you're likely to forget about the existence of the the other copies,
and the defects in those copies will remain.
Also, smaller components are easier to understand than larger components,
and methods result in smaller components.
-
What does
11 / 3
evaluate to?
3
-
What does
11 % 3
evaluate to?
2 (because 11 / 3 is 3 with a remainder of 2).
-
Are you overconfident?
Probably!
-
What does
-4 / 3
evaluate to? Why?
-1 because Java rounds to 0 (i.e., the result is closer to 0 than the
quotient). This is sometimes referred to as truncation.
-
What does
4 / -3
evaluate to? Why?
-1 because Java rounds to 0 (i.e., the result is closer to 0 than the
quotient).
-
What does
-4 / -3
evaluate to? Why?
1 because Java rounds to 0 (i.e., the result is closer to 0 than the
quotient).
-
What does
-4 % 3
evaluate to? Why?
-1 because -4 / 3
evaluates to -1 and the quotient is
-1 1/3 (meaning there is negative one 3s remaining).
-
What does
4 % -3
evaluate to? Why?
1 because 4 / -3
evaluates to -1 and the quotient is
-1 1/3 (meaning there is one -3s remaining).
-
What does
-4 % -3
evaluate to? Why?
-1 because -4 / -3
evaluates to 1 and the quotient is
1 1/3 (meaning there is negative one -3s remaining).
-
Suppose the current time is exactly 11:00:00. What time will it be
15 hours from now using the seconds since midnight representation?
11:00:00 corresponds to 11hr * 3600sec/hr = 39600sec since midnight.
15 hours forward corresponds to 15hr * 3600sec/hr = 54000sec. Adding yields
93600 which is, unfortunately, bigger than the 86400 seconds we know
are in a day.
We could subtract, but what happens if we then add 30hr or 60hr? So, instead,
we should use the % operator. Specifically, the number of seconds
since midnight is (39600 + 54000) % 86400 = 93600 % 86400 = 7200 (i.e., 02:00:00).
-
Suppose the current time is exactly 11:00:00. What time was it
15 hours ago using the seconds since midnight representation?
11:00:00 corresponds to 11hr * 3600sec/hr = 39600sec since midnight.
15 hours backward corresponds to -15hr * 3600sec/hr = -54000sec.
Using the same algorithm as in the previous question,
the number of seconds since midnight is (39600 - 54000) % 86400 =
-14400 % 86400 = -14400. In other words, 14400 seconds before midnight.
So, to figure out the number of seconds since midnight we must add
86400 to get 72000 (i.e., 20:00:00).
-
Look at the documentation for
the
Calendar
class in the
package java.util
. Is the
method getInstance()
static
or not?
static
-
Look at the documentation for the
Calendar
class in the
package java.util
. Is the method get()
static
or not?
It is not static
.
-
Using what you now know, how can you find the current time in Java?
Calendar today;
int hour, minute, second;
today = Calendar.getInstance();
hour = today.get(Calendar.HOUR_OF_DAY);
minute = today.get(Calendar.MINUTE);
second = today.get(Calendar.SECOND);
-
How can you convert
String
representations of
integers to int
values?
Using the static
method named parseInt()
in the Integer
class.
-
How are command line parameters passed into the
main()
method of an application?
As a String[]
.
-
How can you print an unformatted
String
?
There are many ways. The easiest is to use the static
attribute named out
in the System
class and
call it's print()
or println()
methods.
5. Understanding any Existing Designs and Components:
This part of the lab will help you appreciate the importance/value
of UML and why you need to understand existing designs and
components before you start writing code.
-
Read and understand the
high-level design for TimeOut.
-
How many classes are included in the design and what are they?
Three, the TimeOut
, TimeOfDay
,
and Numerical
classes.
-
In UML, what does it mean when a member is underlined?
It is static
.
-
All of the methods in
Numerical
are underlined. What term
is often used to describe such a class?
Classes that have only static methods are often called utility classes.
-
What tasks are going to be performed by the
main()
method in the TimeOut
class?
Input and output.
-
What tasks are going to be performed by the methods in
the
Numerical
class?
Tasks related to formatting the one-digit and two-digit numbers.
-
What tasks are going to be performed by
TimeOfDay
objects.
TimeOfDay
objects will know how to construct themselves, how
to change themselves, and how to create String
representations of
themselves.
-
Though it isn't made explicit, you should be able to infer the
purpose of the
boolean
parameter of
the toString()
method by its name. How should the
returned String
be formatted when the actual parameter
is false
?
When it is true
it should use the civilian format so, when it is
false
it should use the military format (since those are
only two options).
-
Have you been given any existing components for TimeOut?
No. But, if we had, now would be the time to understand them.
-
Is it time to start writing Java code?
No (but we're getting closer).
6. Create a Detailed Design:
This part of the lab will help you understand how you go from software
requirements specifications and a high-level design to a detailed design.
-
Using the "seconds since midnight" approach, what attribute should
the
TimeOfDay
class have?
A private int
named elapsed
.
-
What "constants" should the
TimeOfDay
class have?
It should certainly have the int
"constants"
SECONDS_PER_MINUTE
, MINUTES_PER_HOUR
, and
HOURS_PER_DAY
. It may also need the String
constants AM
and PM
.
-
Should the "constants" be
static
as well as
final
?
Yes because they are not attributes of individual objects.
-
Before actually constructing the
String
, what calculations
will the toString()
method in the TimeOfDay
class need to perform?
It will need to calculate the hour, minute, and second from
the elapsed
attribute.
-
What two methods could be included in the
TimeOfDay
class
that would simplify the toString()
method?
Methods named formatAsMilitary()
and formatAsCivilian()
, both of which are passed an hour,
minute, and second and return a
String
.
-
Should those two methods be
static
or not?
They should be static
because they only use parameters
and local variables (no attributes of TimeOfDay
objects).
-
Should those two methods be
private
or public
?
This is a harder question. They might be useful to other classes, which would
argue for making them
public
. However, if they are
public
they will need to validate the parameters and handle
invalid parameters somehow.
Let's make them public and, just for the purposes of this lab, not worry
about validating the parameters.
-
Draw the UML class diagram look like for this detailed design.
7. Conclusion:
This part of the lab will, hopefully, help you learn the most important
lesson of this lab.
-
You still haven't written a single line of code. Would you be "farther
along" if you had started writing code sooner?
No. You've probably only devoted about an hour to this lab and the
understanding that you have gained will save you far more than an hour when
you start writing code.
-
In the past, have you been told that you were wasting a lot of time on
programming assignments?
Yes. I've spent 8-10 hours on assignments that I've been told should take
half that time or less. (I've never believed those claims, though.)
-
Given that you were unable to solve the examples above by hand,
do you really think that you could have completed TimeOut
without any help?
I do, but I probably couldn't convince anybody else.
-
In the future, should you think before you start writing code?
I should.
-
In the future, should you break problems up into pieces
before you start writing code?
I should. Small problems are much easier to solve.
-
In the future, should you think about multiple ways to
solve the problem before you start writing code?
I should. In this case, I probably would have used the first approach
(i.e., working with the hour, minute and second) but the second
approach (i.e., using the elapsed seconds since midnight) is much
better.
-
In the future, will you you think about these things before you start writing code?
Only time will tell.