/** * CS139 - Programming Fundamentals * Department of Computer Science * James Madison University * @version Spring 2016 */
Perform integer division and modulus.
Display output with specific formatting.
Compile and run from the command line.
Redirect input and output using text files.
import
statement/
)%
)Develop (on paper) an algorithm for converting a total number of seconds into hours, minutes, and seconds. For example:
Input | Output | ||
---|---|---|---|
Total Seconds | Hours | Minutes | Seconds |
5000 | 1 | 23 | 20 |
3625 | 1 | 0 | 25 |
1000 | 0 | 16 | 40 |
Before writing any code, think about solving this problem in general. Make sure you know how to do this task on paper! Only then can you describe the process algorithmically. Be sure to indicate the names of all variables, and list all required steps in the conversion.
Create a new program Seconds.java (note the capital S) in jGRASP with the following structure.
import
statement at the top of the file to make Scanner available to your program."Enter the number of seconds: "
exactly as shown. Use the print
method (rather than println
) so that the cursor remains on the same line as the prompt.(blank line) _____ seconds = _____ hours, _____ minutes, and _____ seconds (blank line)For example:
1000 seconds = 0 hours, 16 minutes, and 40 seconds
Make sure to put spaces around each of the values. There should be
exactly one space between elements on the output line, and no leading
or trailing spaces. Be sure to include the right number of blank
lines in the output (use the newline
character, \n
). Incorrect spacing or anything other
than three lines of output (each with one newline character at
the end) will cause the output to be flagged as incorrect.
The requirements above specify exactly how the prompt and output should be formatted. It is easy to make small formatting errors like extra spaces, mispelled words, etc. Fortunately there are software tools that can compare the contents of two files to automate the process of checking for differences. If we have an example of the desired output we can use these tools to compare our output to the example.
Today we will use Meld, a visual diff and merge tool that runs on Linux. (There are similar free tools like WinMerge on Windows or FileMerge on Mac, the latter of which comes with Xcode.)
If your program is working correctly, executing it in the terminal should look something like the following:
$ javac Seconds.java $ java Seconds Enter the number of seconds: 1000 1000 seconds = 0 hours, 16 minutes, and 40 seconds $
1000
as the input value. Use a text editor to create a new text file
named output.txt
and copy your program's output into that
file. (You can copy from the terminal by selecting some text,
right-clicking in the terminal and selecting "Copy".) Save the file
into the same folder where you saved expected.txt
. Make
sure to copy all of your output, including the trailing empty line.
meld expected.txt output.txt
to compare the two files.