Digital Clock

1. Introduction

This lab will create a digital clock based on nested FOR loops for hours, minutes, and seconds, and a WHILE loop for doing over and over so that your clock works for all year long without interruption.

Note: If a group of students is interested, we could create a real digital wall-mounted clock to be placed in our classroom, so I can keep track of the time during my lectures. The actual clock would be based on the LED Strip used by Emma a couple of lectures ago. I would help you guys all the way through. Let me know if any of you would be interested in taking this challenge but fun project. See example of what I am talking about here.

2. Program Functional Requirements

2.1. Input Requirements

Your program shall request five inputs at startup:

  • Current hour
  • Current minute
  • Current seconds
  • Period of the day: “am” or “pm”
  • Day of the week: “Monday, Tuesday, …, Sunday”

2.1.1. Input Validation

Your program shall validate all the inputs as described below:

  • Valid hours: from 1 to 12
  • Valid minutes: from 0 to 59
  • Valid seconds: from 0 to 59
  • Valid periods: only “am” and “pm”, nothing else
  • Valid days of the week: well, you know what is valid here, right? (the user can enter upper case, lower case, or a mix of cases and your program should accept them without any problem (ex.: Sunday, sunday, and SUNDAY are valid inputs)

To simply your program, the validation can be performed after the user enter al the inputs. If any of the inputs above is not valid, your program shall:

  • Display a dialog box letting the person know that the given input (show the invalid input) is not valid and the valid values for the given input are (show the allowed values for that given input).
  • Request a new number

2.2. Program Logic Requirements

Only after the four inputs have been entered and validated your program should the following steps.

2.2.1. Display the Time

The following statement should be displayed in the terminal window every past second:

Day of the week hh:mm:ss am/pm

Example: 

Saturday 11:50:04 pm 

Where the first line displayed by your program should be the actual time that the user has entered in Section 2.1 above. (Pay attention that for values less than 10, your program must put a zero (0) at the front of the number, such as 04 in the example above).

Hint: you do not need to initialize the FOR loop with a hard-code value of 0. Instead you can initialize that variable with some other variable. See Example below:

for (int banana = 0; banana < 1000; banana++)

for (int banana = variable1; banana < 1000; banana++)

This will help to get your digital watch to display the right time as the program starts. However you must do some trick later on so that when the seconds (or minutes, or hours) goes to its maximum value (i.e., 59 seconds) the loop starts again from 0 and not “initial” value of variable1 (hum, you may change the value of variable1 when you reach 59 seconds to zero!)

2.2.2. Pause the program execution for 1 second

Use Thread.sleep(1000) to make your program wait for one thousand milliseconds (what is equivalent to 1 second) before printing another time stamp in the terminal window.

Please not that Java Virtual Machine takes some time (few microseconds) itself to process the for loops, and the printout statements, so you should really use a number slightly less than 1000 milliseconds to compensate this extra time that JVM spends doing other things. By How much? You can check how long JVM took from one print statement to the next one and then figure out how many milliseconds you need to subtract from 1000.

How would you possibly know how to do it????  Talking to your best programming friend: the internet. I talked to it and “he” gave me the following suggestion:

import java.time.*
Instant before = Instant.now();
// do stuff
Instant after = Instant.now();
long delta = Duration.between(before, after).toMillis();

Try to adapt the code above into your own program. If you get stuck, ask the lab TA or your tall, young, handsome instructor…

2.2.3. Endless program

Remember your program when reaches midnight on Sunday it should keep going to the next week and so on.

3. Test Your Program

Even though the time you will be testing is not close to midnight, test your program giving the following initial inputs:

  • Sunday
  • 11 hours
  • 58 minutes
  • 15 seconds
  • pm

and wait until it reaches midnight and see if it goes to Monday, 12:00:00 am. Repeat this with 12h, 58m, and 15s and see if it goes from 12h to 1h when the hour is up.

4. Java Topics Used In this Lab

  • variable creation (int, boolean, etc.)
  • Nested FOR Loops
  • IF-ELSE statements
  • SWITCH Statements
  • Logical operators (AND, OR, and NOT)
  • WHILE loops
  • JOptionPane Input and Message dialog boxes
  • Input validation
  • Java API Thread
  • Java API DecimalFormat
  • Handling Exception
  • Java Imports