-
Complete the program as follows:
- immediately AFTER the "define conversion constants"
commment declare a conversion constant SECONDS_PER_DAY and in the
initialization have the computer do the arithmetic (in other words you
shouldn't get out your calculator and figure out how many seconds are
in a day -- write the expression so the computer has to do the work)
- define other constants and/or variables as needed
(assume there are 30 days in a month and 365 days in a year) - note
that some variables have already been declared
- calculate and print, appropriately labeled, the total number of
seconds
(place your code where indicated by the comments!!)
Test your program. For example, a person who is 18 years, 3 months, and
21 days old has lived 577,238,400 seconds (under the assumptions of the program).
A person who is 21 years, 0 months, and 0 days has lived 662,256,000 seconds.
- How many seconds has a person who is 68 years,
6 months, and 12 days old lived? Run your program to find out.
You should have gotten a strange answer to the last question!
Why did your program produce such an answer? Hint: Java uses
a 32-bit two's complement representation for type int.
In pre-lab you
computed the largest int value that Java can represent (it is
also in the textbook on page 72).
Now look at the other ages in seconds you have computed, e.g., 21 years.
68 years is a little over triple that value; is this consistent with your
strange answer? (No need to write this answer.)
- Fix your program as follows:
- Change the type of one variable from int to
long, which is a 64-bit integer representation.
Think about which variable is going to contain a number
too big for 32 bits - don't just randomly change! It should
work to change only ONE variable (unless you
declared lots of other variables and did several intermediate
calculations).
Your program should
still work exclusively with integers, not floating point numbers.
- The above change will not completely solve the problem (run your
program and see). You need to make sure the expression is computed using
the larger data type - a carefully placed cast operator should do
the trick! (NOTE: You should be able to correct this with just one
variable type change and one cast however, the order in which you
do calculations may require more.)
Test the program again and make sure it works. A person who has lived
68 years, 6 months, and 12 days is 2,161,036,800 seconds old.
Also try 99 years, 9 months, and 9 days and make sure the answer makes
sense.
- Print your final program.