Site Navigation

Site Search

CS 110: Lab 6

Learning Objectives

After completion of this lab, you should be able to

Work collaboratively as a pair programming team

All labs in CS 110 will be done as pair programming teams. Your partner for today's lab is listed in the table below:

Hebeler 203
Grader: Vinh Tran; Assistant: Daniel Carpenter
Team 1
Aguilar Jr, Jaime
Field, Sarah
Team 2
Amezcua Gutierrez, Edson
Hansen, Mitchell
Team 3
Baird, Owen
Hastings, Jake
Team 4
Berman, Jake
Hogan, Martin
Team 5
Coudriet, Blake
Ling, Nathan
Team 6
Erickson, Joel
Nash, Chris
Team 7
Goeke, Max
Olivares, Scott
Team 8
Harris, Alexander
Shearer, Stetson
Team 9
Heflick, Liz
Wagster, Nathan
Team 10
McCauley, Rylee
Smith, Stephanie
Team 11
Millard, Ryan
Akana, Chris
Team 12
Olden, Greg
Awan, Samara
Team 13
Quayle, Weston
Barrett Wright, Matthew
Team 14
Straub-Walden, Andy
Bloom, Thomas
Team 15
Williamson, Sarah
Cuddington, Chris
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).

Use test-driven development for developing software programs

Begin by developing test cases for this program.

Test Cases
(in seconds)

Write your calculated test case values in the Lab assignment sheet. You will turn in this sheet at the end of lab for credit.

Use type casting to convert doubles to integers

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

   double d;
   int i;
   d = 336.219;  
   i = (int) d;  

In this case, i would be set to 336

To get at the decimal part of 336.219, we could use a combination of multiplication, casting, and the modulus operator

   double d = 336.219;
   int fractionalPart;
   fractionalPart = ((int) (d * 1000)) % 1000;  

In this case, fractionalPart would equal 219

Define a Java class to format double values as a string

  1. Create the Java class Seconds and save as
  2. Class Seconds needs a constructor method and has one instance method, format(), that returns a String formatted as minutes:seconds.milliseconds
public class Seconds
   // instance variable
   double time;
   // Constructor method
   public Seconds (double t)
      time = t;
   // returns time formatted as minutes:seconds.milliseconds
   public String format()
   // Declare local variables
      String timeString;
      int minutes;
      int seconds;
      int milliseconds;
   // Calculate minutes, seconds, milliseconds
   // Hint: use type casting to convert time from double to int
   // You figure out this part
   // Create the string minutes:seconds.milliseconds using concatenation
   // You figure out this part
      return timeString;

Test your class with a main method

A short main method will be used for testing purposes.

  1. Save the main method shown below to a new file named
  2. Compile and test your program, comparing the formatted time strings it produces with your hand-calculated values
   import java.util.Scanner;
   public class TestSeconds
      public static void main(String args[])
      // local variable
         double localTime;
      // Create a Scanner object
         Scanner keyboard = new Scanner(;
      // Get a time in seconds from the user
         System.out.print("Enter a time in seconds: ");
         localTime = keyboard.nextDouble();
      // Create a new Seconds object
         Seconds time = new Seconds(localTime);
      // Display the local time formatted as a string

Sample Session

Enter a time in seconds: 336.219

Use your class in a graphical user interface (GUI) Windows program

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.

  1. Download and save in your U:\labs\lab6 folder the following two Java files
    • file to implement the start and end times for a stopwatch
    • to create a graphical user interface for the stopwatch
    • Note: you are not expected to understand the code yet, creating GUI Java applications is a topic of CS 111
  2. Compile and run the StopWatchGui class and confirm that the stopwatch is working correctly
    • Note: you should not need to make any changes to your tested TimeFormat class code

startup with no buttons pressed start button pressed stop button pressed

Test a program as an independent tester

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.

  1. Test the stopwatch program as an independent tester and find the error(s)
    • You are testing the program by running it and looking at its output, not be carefully looking inside the source code for coding errors

Read and modify a program written by someone else

Examine the code that you downloaded and added to your lab6 folder. 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.

// Disable the stop button

To Receive Credit

You are allowed to drop one lab grade for the entire quarter.