Lab 6 Testing via Command Line (Seconds lab)


  • Perform integer division and modulus.

  • Display output with specific formatting.

  • Compile and run from the command line.

  • Redirect input and output using text files.

Key Terms

create a new instance of a class (i.e., make an object)
import statement
make classes available from Java's standard library
division (/)
integer operation that produces the quotient
modulus (%)
integer operation that produces the remainder

Part 1: Preparation

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.

Part 2: Implementation

Create a new program (note the capital S) in jGRASP with the following structure.

  1. Write a one-sentence Javadoc comment about your class, along with @author and @version tags and Honor Code statement.
  2. Add an import statement at the top of the file to make Scanner available to your program.
  3. Add all required variable declarations at the start of the main method.
  4. Declare and instantiate a Scanner variable after the other declarations.
  5. Display the prompt "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.
  6. Calculate the equivalent hours, minutes, and seconds using your algorithm from Part 1.
  7. Finally, output your result. The format must be EXACTLY as shown, including whitespace.
    (blank line)
    _____ seconds = _____ hours, _____ minutes, and _____ seconds
    (blank line)
    For example:
    1000 seconds = 0 hours, 16 minutes, and 40 seconds
  8. Test your program with several input values, and make corrections if the results are not correct.

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.

Part 3: Diff Testing

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 DiffMerge, a visual diff and merge tool that runs on Linux. (There are similar free tools Meld  on Linux, 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
$ java Seconds
Enter the number of seconds: 1000

1000 seconds = 0 hours, 16 minutes, and 40 seconds

  1. The file expected.txt contains an example of the desired input and output for our program. Download this file and examine it in a text editor.
  2. Execute your own program in the terminal, providing 1000 as the input value.
  3. Now download the input.txt file and run this command in the terminal: java Seconds < input.txt  and see what happens.
  4. Enter the command  java Seconds < input.txt > output.txt  and see what happens.
  5. You should now have a file named output.txt in that same directory. What is in the file?
  6. Enter the command meld expected.txt output.txt  to compare the two files.
  7. If there are no differences, congratulations on getting your program right the first time!
    (But go back and make a mistake in your Java code to see how this part of the lab works.)
  8. Based on the differences between the expected output and your actual output, figure out what needs to be changed in your code.
  9. Edit and re-compile your code using jGRASP. Try testing your improved output using meld.
  10. Repeat the previous step until your solution matches 100%. Then submit your file via Autolab.

Back to Top