CSCI 161 - Lab 2: "Give Me a Second!"
Overview
This lab will have you developing a program that asks the user to input a time period as a number of seconds and in turn
calculates the whole number of days, hours and minutes that are contained within the entered time period.
Refer to the "Output Format" below for an example of what your program must do.
The objectives of this lab are to reinforce and help teach:
- proper Java program class construction, purpose of the main method and use of other methods that each perform a smaller part of the overall problem
- use of different data types, in particular long
- methods with parameters
- methods that return values
- handling of user input (use of the Scanner class)
- conditional statements
- indefinite loops
Instructions
Following are the instructions and you should follow them carefully:
- Using Eclipse create a new Java Project and name it: Lab2
- As with all your labs, add a class of the same name, Lab2, and include in that class a main method.
- The main method and therefore your program should use an indefinite loop and within that loop:
- Prompt the user to enter a whole number of seconds or 0 to exit the program.
- Get input from the user as a single long integer.
- Check if 0 was entered and if so then...
- ...Print a departing message and terminate the loop and the program
- else if 0 not entered then....
- ...Print out the number of seconds entered and, ...
- ...Calculate and print out the total whole number of days contained within said seconds and, ...
- ...Calculate and print out the total whole number of hours contained within said seconds and, ...
- ...Calculate and print out the total whole number of minutes contained within said seconds and, ...
- ...Lastly, calculate and print out the days, hours, minutes and seconds within said seconds in the "digital clock" format of D:HH:MM:SS (see Output Format section, below). Note that calculating and outputting this line is probably the most difficult part of this program.
- Your program should output according to the "Output Format" below.
- The program must break down the task at hand into small operations, each one performed by a different method. You should be thinking of developing a diferent method for each computation and print, with each method accepting an integer input parameter (the number of seconds
input) and printing that value out. For example:
public static void printDays(int seconds) {
// your code will calculate numbner of days in the seconds
// passed in and then print that value out formatted with text
}
- Iteratively develop the lab (get one method working, test it, fix syntax errors, continue on to the next).
- When the lab is complete and working submit your Lab2.java file via AutoLab.
Things to Consider & Remember
- Utilize a commenting style like the one shown in class (here).
- Method names and variable names should have the proper camelCase.
- Use proper indentation for your curly braces, conditional statements, loops.
- You should be able to find some code to re-use (with some modifications required) in the last lecture code sample (here.)
- Your program should store the seconds entered by the user in a variable of the long data type.
- Your program should have many methods, ideally one method for each of the different feature steps about (approximately 8 in total for i. through x. above).
- Test with many different seconds values (a few digits, many digits, 0, ...).
Output Format
Following is an example of the desired output format:
Enter a number of seconds as a whole number or 0 to quit: 3600
Number of seconds entered is 3600 and that equates to:
0 days
1 hours
60 minutes
0:01:00:00 days, hours, minutes and seconds (D:HH:MM:SS:)
Enter a number of seconds as a whole number or 0 to quit: 123456
Number of seconds entered is 123456 and that equates to:
1 days
34 hours
2057 minutes
1:10:17:36 days, hours, minutes and seconds (D:HH:MM:SS:)
Enter a number of seconds as a whole number or enter 0 to exit: 723672
Number of seconds entered is 723672 and that equates to:
8 days
201 hours
12061 minutes
8:09:01:12 days, hours, minutes and seconds (D:HH:MM:SS:)
Enter a number of seconds as a whole number or 0 to quit: 0
0 entered. Goodbye!
Deadline
This lab must be AutoLab submitted to the instructor by 11:59pm on the Monday a week after it was assigned. Check AutoLab for the specific due date.