Site Navigation

Site Search

CS 110: Lab 3 main content

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
Barrett Wright, Matthew
Team 2
Amezcua Gutierrez, Edson
Bloom, Thomas
Team 3
Baird II, Owen
Cuddington, Christopher
Team 4
Berman, Jake
Field, Sarah
Team 5
Coudriet, Blake
Hansen, Mitchell
Team 6
Erickson, Joel
Hastings, Jake
Team 7
Goeke, Maxwell
Hogan, Martin
Team 8
Harris, Alexander
Ling, Nathan
Team 9
Heflick, Elizabeth
Nash, Christopher
Team 10
McCauley, Rylee
Olivares, Scott
Team 11
Millard, Ryan
Shearer, Stetson
Team 12
Olden, Gregory
Wagster, Nathan
Team 13
Quayle, Weston
Smith, Stephanie
Team 14
Straub-Walden, Andrew
Akana, Christopher
Team 15
Williamson, Sarah
Millard, Mikel
Fill in: Awan, Samara

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.

Introduction

Write a Java program that can serve as a ending time calculator. The user enters the starting time in hours and minutes, a duration in total minutes, and your program will calculate and display the ending time (as hours:minutes).

For example, if an event starts at 2 30 and lasts 125 minutes, it will end at 4 35

Note: To simplify the problem, assume military time (0..23) rather than standard time, in which you would need to worry about a.m. and p.m.

Use test-driven development for developing software programs

Begin by developing test cases for this program. Ending time should be expressed in military time (0..23)
For example: 1:30 p.m. is expressed as 13:30 military time

Test Cases
All times are in military time
Start HoursStart MinutesDuration
(in minutes)
Ending Hour
(military time 0..23)
Ending Minute
(0..59)
130120  
114560  
124090  
100240  

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

Use integer multiplication, division, and modulus operators

The algorithm to solve this problem on a computer is not straight-forward. Here is a solution:

  1. Convert the starting time in hours and minutes to the equivalent total minutes
    • For example: 2 hours and 30 minutes would be 150 total minutes
    • For example: 5 hours and 15 minutes would be 315 total minutes
    • Hint: use multiplication and addition

On the Lab 3 assignment sheet, write down the math formula needed to calculate the starting time (in total minutes) given the start hours and start minutes.

startingTime (in total minutes) = write out the formula
  1. Add the duration (in minutes) to this starting time (in total minutes), giving you the ending time in total minutes
    • For example: a start time of 150 (in total minutes) plus 125 minutes duration duration gives you an ending time of 275 total minutes
    • For example: a start time of 315 (in total minutes) plus 10 minutes duration gives you an ending time of 325 total minutes

On the Lab 3 assignment sheet, write down the math formula needed to calculate the ending time (in total minutes) given the starting time (in total minutes) and the duration (in minutes).

endingTime (in total minutes) = write out the formula
  1. Convert the ending time in total minutes back to hours and minutes with / and % integer operators and the fact that one hour is 60 minutes
    • For example, 63 minutes is 1 hour and 3 minutes
    • For example, 125 minutes is 2 hours and 5 minutes
    • For example, 325 minutes is 5 hours and 25 minutes

On the Lab 3 assignment sheet, write down the math formula needed to calculate the ending hour and minutes given the ending time (in total minutes).

Note: at this point, assume simple military time for the end hours. So 12:50 plus 30 minutes would be 13:20

endingHour = write out the formula
endingMinutes = write out the formula

Use jGRASP to create the program EndingTime.java

Use jGRASP to write the Java program EndingTime.java that calculates the ending time (in hours and minutes), given a starting time (in hours and minutes) and a duration (in minutes).

The skeleton class code for EndingTime is provided below

import java.util.Scanner;

public class EndingTime
{
   public static void main(String [] args)
   {      
   // Variable declarations
   // Hint: All variables need to be declared as integers
   
   // Create a Scanner object to read from the keyboard
      Scanner keyboard = new Scanner(System.in);
     
   // Get the starting time in hours and minutes
   
   // Get the duration time in minutes
   
   // Calculate the ending time  
   
   // Display the output
     
   }
}

Sample Session

Enter the starting time (in hours and minutes): 2 30
Enter the duration (in minutes): 125
Ending hour is 4 
Ending minute is 35
  1. Copy and paste the skeleton class code into EndingTime.java to get you started
  2. Type in your program code
  3. Compile your program and correct all the syntax errors
  4. Run your program and compare your results to the test case values you hand calculated (in military hours)

Change Pair Partner Roles

Change pair partner roles. The driver should become the navigator, the navigator should become the driver.

Convert military time to standard

Modify your program so that it converts the hours in military time (0..23) to hours in standard time (1..12).

Doing the modulus 12 conversion is not as straightforward as it may at first seem. Dividing by 12 will always returns a remainder value between 0 .. 11. Standard time hours are expressed as 1 .. 12.

A formula to perform the conversion is

h = ( ( h + 11 ) mod 12 ) + 1 where h is military time hours and mod is the modulus operator %
  1. Modify your program using the formula given to produce ending times using standard time
  2. Compile your program and find and correct all the compiler syntax errors identified by the compiler
  3. Run your program and compare your results to the test case values you hand calculated (regular hours)

To Receive Credit

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