Results 1 to 2 of 2

Thread: Military Time

  1. #1
    ustar is offline Member
    Join Date
    Feb 2010
    Posts
    7
    Rep Power
    0

    Default Military Time

    I have a program that is suppose to read the difference between two times but my output always comes out to be 00. Please let me know whats wrong with my program. My program is divided into two parts. TimeInterval class and TimeInterval.














    public class TimeInterval
    {
    private int time1;
    private int time2;
    private int hours;
    private int minutes;

    public TimeInterval()
    {




    }
    public int getHours()
    {
    hours = (time2 - time1)/100;
    return hours;

    }

    public int getMinutes()
    {
    minutes=(time1-time2)%100;



    return minutes;
    }

    }














    import java.util.Scanner;


    public class ElapsedTimePrinter
    {



    public static void main(String[] args)
    {

    Scanner in = new Scanner(System.in) ;



    TimeInterval a= new TimeInterval();

    System.out.println("Enter two times in military format, using 4 digits.");

    System.out.println("Please Enter time one Military Format:");
    int time1=in.nextInt();

    System.out.println("Please Enter time two in Military Format:");
    int time2=in.nextInt();
    System.out.println("The time between the give times is:" + a.getHours() +a.getMinutes());

    }}

  2. #2
    rdtindsm is offline Member
    Join Date
    Feb 2009
    Posts
    92
    Rep Power
    0

    Default

    In your class, you have time1 and time2 as private members of TimeInterval.

    The time1 and time2 variables you declare in main are local to main. They are not the same fields you create in the class definition.

    Nor can you reference them as a.time1. They are private and not visible to main. You already seem to have the understanding of how to access the fields with getHours(). Do the same thing to set these fields.

    public void setTime1(int someTime){
    time1 = someTime;
    }

    This is a very simple setter. In the real world, you might check that int 0000 <= someTime <= 2400 and other relevant rules. One of these rules might check that the last two digits of your number are in the range 0 -> 59.

    While your code may simply be trying to develop the structure of your program, let me point out that (0220 - 0110) % 100 != 70

Similar Threads

  1. Write a Program that reads two times in military format
    By HPcompaq256 in forum New To Java
    Replies: 10
    Last Post: 02-26-2010, 04:38 AM
  2. Java, Military Format using "/" and "%" Operator!!
    By sk8rsam77 in forum New To Java
    Replies: 11
    Last Post: 02-26-2010, 03:03 AM
  3. Class Time - represents time of day
    By verbazon in forum New To Java
    Replies: 1
    Last Post: 04-13-2009, 01:06 AM
  4. Simple program involving military time
    By busdude in forum New To Java
    Replies: 4
    Last Post: 10-08-2008, 06:03 PM
  5. Replies: 21
    Last Post: 08-01-2008, 11:13 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •