/**
 * CS139 - Programming Fundamentals
 * Department of Computer Science
 * James Madison University
 * @version Spring 2016
 */

Objectives

Key Terms

instantiate
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 Seconds.java (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.
  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 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

$
  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. 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.
  3. Enter the command meld expected.txt output.txt  to compare the two files.
  4. 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.)
  5. Based on the differences between the expected output and your actual output, figure out what needs to be changed in your code.
  6. Edit and re-compile your code using jGRASP. Try testing your improved output using meld.
  7. Repeat the previous step until your solution matches 100%. Then submit your Seconds.java file via Web-CAT.