Please follow me here!

Sunday, November 18, 2012

JAVA Program: Military Time to Real Time

Hello, people. How you doing? I'm really busy with my college homework, but I am working so hard to pass my courses and continue in my studies to pursue my future career as a producer and director of computer games.

Want to get into the words of Namco's Time Crisis II? Here's another JAVA Program for you guys; this one was done last year in my Java Programming Level 1 class with Linda McGirr.

Luckily, this program, with the source file name MilitaryTimeDifferenceToRealTimeImproved.java, includes all the necessary comments to explain the process.

One thing before you read: A single line of comments is marked by two forward slashes, while a block of comments is marked as follows:

/*
*
*
*/

The program was written on Eclipse Indigo, a free IDE that provides the necessary tools and shortcuts to programming JAVA applications.

Enjoy reading the program! (This file is copied to this Blogger post as is.)


/*
*
*    Military Time Difference in Real Time Assignment
*
*    This program is designed to yield out the difference
*    of two independent military times, what's current
*    and what's due for, let's say, an estimation of
*    how long left before the Afghan Forces attack
*    a United States base in Kabul, in real time
*    (hours : minutes). When we enter the times of what's
*    current and what's due, this program will produce
*    the result in real-time format. This is helpful for
*    military personnel to interpret the difference in
*    real-time format to their relatives who can't
*    really convert from military time to real time
*    by themselves.
*
*    File Name: MilitaryTimeDifferenceToRealTimeImproved.java
*    Developed by Gregory Desrosiers, Student of Champlain College
*    St-Lambert, Quebec, Canada
*
*    Teacher : Linda McGirr
*
*    Assignment Record Dates
*
*    Psuedocode Start Date: Wednesday, September 21, 2011
*    Psuedocode End Date: Wednesday, September 21, 2011, 12:57 PM
*
*    Coding Start Date: Wednesday, September 21, 2011, 1:13 PM
*    Coding End Date: Saturday, September 24, 2011, 2:06 PM
*
*    The coding has been modified as of September 26, 2011 at 1:21 PM.
*
*    Comments are provided in each step of the process of this program.
*    The psuedocode is provided in the 'Assignment 2 - Psuedocode.docx'
*    file. You will need Microsoft Office Word to open it.
*
*    The psuedocode has been modified as of Wednesday, September 21,
*    2011, at 4:47 PM. This psuedocode, however, only covers the
*    logic of the original program where there were problems found
*    with the output, so there are logical errors.
*
*    No psuedocode is provided in this version because as of September 24,
*    I had way too much homework to make a psuedocode that matches the
*    style of this one, so the psuedocode is omitted.
*
*    To make the programming easier, this program has two 'if'
*    statements, a code that has not yet been covered by
*    Ms. McGirr in the Starting Out With Java textbook.
*
*    There are no if statements for correcting the entry of negative
*    numbers and decimals.
*
*    Part of the program was aided by Bruno Gosselin.
*/


// The following statement is needed to enable dialog boxes.

import javax.swing.JOptionPane;
/*
     The following four lines are important for the program to
     work where the two sets of braces connect the statements to
     the method and the method to the class file, similarly, the
     binary file in the Java language.
*/

public class MilitaryTimeDifferenceToRealTimeImproved
{
    public static void main(String[] args)
    {
       
        // Here are the variables declared with comments explaining
        // their use and their meaning.

       
        String inputString;
                /*
                 * This holds the input for the current military time
                 * and the due military time from two dialog boxes.
                 * They will then be used in the parse methods to
                 * convert the characters inputted into integers.
                 */

       
        int currentMilitaryTime,
                /*
                 * This holds the natural number of the current
                 * military time. Natural, because time numbers
                 * cannot be negatives.
                 */

       
            dueMilitaryTime,
                /*
                 * This holds the natural number of the due
                 * military time.
                 */

           
            dueHours,
                /*
                 * This holds the first digit in the
                 * dueMilitaryTime variable.
                 */

           
            dueMinutes,
                /*
                 * This holds the last two digits in the
                 * dueMilitaryTime variable.
                 */

           
            currentHours,
                /*
                 * This holds the first digit in the
                 * currentMilitaryTime variable.
                 */

           
            currentMinutes,
                /*
                 * This holds the last two digits in the
                 * currentMilitaryTime variable.
                 */

           
            outputHours,
                /*
                 * This holds the result for the hours that will
                 * be displayed later on in the program.
                 */

           
            outputMinutes;
                /*
                 * This holds the result for the minutes that
                 * will be displayed later on in the program.
                 */

       
        /*
         * The two statements shown below are for the input of the current
         * military time and converting the input into a number because
         * the entry will only be a string of characters.
         *
         * This is where we see the first dialog box.
         *
         * For both dialog boxes, the programming style is not met,
         * but this is for the purpose of printing the code on paper only.
         */

       
        inputString =
            JOptionPane.showInputDialog("What is the current military time?");
        currentMilitaryTime = Integer.parseInt(inputString);
       
        /*
         * The next two statements follow the same process as the current
         * military time, except this time, it's for the due military time.
         */

       
        inputString =
            JOptionPane.showInputDialog("What is the due military time?");
        dueMilitaryTime = Integer.parseInt(inputString);
       
       
        /*
         * Now, we seperate the hours and minutes from each inputted
         * values. To do this, we use both the division and modulus
         * operators where an hour in military time is set at every
         * interval of 100. Technically, there are 60 minutes in
         * one hour, but the hours fit into 100, so we use the division
         * of 100 for hours and the modulus to get the remainder
         * of the hour division, which is then assigned to the
         * variable representing minutes.
         */

        currentHours = currentMilitaryTime / 100;
       
        currentMinutes = currentMilitaryTime % 100;
       
        dueHours = dueMilitaryTime / 100;
       
        dueMinutes = dueMilitaryTime % 100;
       
        /*
         * This is where we find the difference in time where the
         * two following statements are effective when the
         * due military time is later on in the same day
         * than the time it is right now.
         */

       
        outputHours = dueHours - currentHours;
       
        outputMinutes = dueMinutes - currentMinutes;
       
        /*
         * Now, we do include the most crucial process, but there
         * are a couple of catches.
         *
         * One is what if the variable 'dueMinutes' is smaller
         * than 'currentMinutes' in inputs where one example
         * would be that the current time is 0945 hours and
         * the due is 1030 hours? The result will be a negative
         * number and plus one on the 'outputHours' variable,
         * which doesn't make sense.
         *
         * To solve the first catch, we use a conditional
         * statement where if the result of minutes is negative,
         * it will subtract one from 'outputHours' and add
         * 60, which gives the complementary value of
         * 'outputMinutes' and stores the value in that
         * variable. Braces are used because we are executing
         * two statements at the same time.
         */

       
        if (outputMinutes < 0)
        {
            outputHours -= 1;
            outputMinutes += 60;
        }
       
        /*
         * Another catch is, what if the current time is before
         * midnight and the due is after midnight? We do get
         * the minutes right, but not the hours. It will be
         * negative. To compensate it, we add 24 to
         * 'outputHours' where it gives us the complementary
         * value and stores the value in it. There are no
         * braces because we're only executing one
         * statement.
         */

       
        if (outputHours < 0)
            outputHours += 24;
       
        // This is where we display the result.
       
        JOptionPane.showMessageDialog(null, "Current time in military format: "
                                    + currentMilitaryTime + " hours." +
                                    "\nDue time in military format: " +
                                    dueMilitaryTime + " hours." +
                                    "\n\nThe difference between those times is "
                                    + outputHours + " hours and " +
                                    outputMinutes + " minutes.");
       
        /* The next statement is for the last dialog box.
         * This is the last output we will see before the
         * program terminates (closes).
         */

       
        JOptionPane.showMessageDialog(null, "Program Complete.");
       
        /* This is the last statement in the program.
         * It will stop dialog boxes from appearing and
         * closes the program.
         */

       
        System.exit(0);
    }
}

Google+ Followers

Popular Posts by Gregory