After completion of this lab, you should be able to
classto format double values as a string
All labs in CS 110 will be done as pair programming teams. Your partner for today's lab is listed in the table below:
Aguilar Jr, Jaime
Amezcua Gutierrez, Edson
Barrett Wright, Matthew
|Fill in: Millard, Mike|
Note: Partners will change every week.
You may wish to review basic pair programming guidelines before you begin.
You should change roles every 10 to 15 minutes.
You will write a Java class called
Seconds that converts a time value represented in seconds (double) to its equivalent time value as a String
For example, the time
69.752 (in seconds) would be represented as the String "1:9.752" or 1 minute, 9 seconds, and 752 milliseconds (in minutes:seconds.milliseconds).
Begin by developing test cases for this program.
Write your calculated test case values in the Lab assignment sheet. You will turn in this sheet at the end of lab for credit.
In the test cases, time (in seconds) is represented as a double value. The part to the left of the decimal represents the total seconds, the part to the right of the decimal represents the milliseconds.
Converting a double to an integer value in Java is done with the cast operator. For example, to convert the double variable
d to the integer
i, we could write
In this case,
i would be set to
To get at the decimal part of 336.219, we could use a combination of multiplication, casting, and the modulus operator
In this case, fractionalPart would equal
classSeconds and save as
format(), that returns a
Stringformatted as minutes:seconds.milliseconds
A short main method will be used for testing purposes.
Enter a time in seconds:
Once your class is tested and determined to be working correctly, we will use it to format time (as a double) into a strings to display in a graphical user interface (GUI) Windows application acting as a stopwatch.
U:\labs\lab6folder the following two Java files
StopWatchGuiclass and confirm that the stopwatch is working correctly
Java programs need to be thoroughly tested before being sold or released to the public.
The best testers are usually independent testers, or those who did not write the source code. It has been shown that developer of the source code overlook many bugs while testing because they are not thorough or they make false assumptions about using the program that independent testers often catch as errors.
Even though the user interface to the stopwatch program is very simple, it has subtle errors.
Examine the code that you downloaded and added to your lab6 folder.
StopWatchGui.java contains the code that creates the graphical user interface that displays as a window application.
A problem occurs with the stopwatch program when the user clicks on the stop button without clicking the start button first. (Try it if you did not discover this problem in the previous activity)
One way to fix the problem (as a programmer) is to design the user interface to initially disable the Stop button to prevent the user for clicking it before they clicked the Start button. To disable or enable a button, use the button's
setEnabled() method, passing a true or false value as the method argument.
StopWatchGui.javacode and find where the Stop button is created.
StopWatchGui.javacode find where the method called when the user clicks on the Start button
labfolder to the partner's USB thumb drive (or email it to them if they do not have a thumb drive).
U:\labs\lab6folder and turn in the Lab 6 assignment sheet at the start of the next lab
You are allowed to drop one lab grade for the entire quarter.