Results 1 to 12 of 12
  1. #1
    sk8rsam77 is offline Member
    Join Date
    Nov 2008
    Posts
    1
    Rep Power
    0

    Default Java, Military Format using "/" and "%" Operator!!

    Write a program that reads two times in military format (0900, 1730) and prints the number of hours and minutes between two times. Supply your own test program. Here are two sample runs. User inputs are the numbers.

    Please enter the first time: 0900
    Please enter the second time: 1730
    8 hours 30 minutes

    Please enter the first time: 1730
    Please enter the second time: 0900
    15 hours 30 minutes

    This is what i have so far...


    import java.util.Scanner;

    public class Main
    {



    public static void main(String[] args)
    {
    Scanner scanner = new Scanner(System.in) ;
    TimeInterval timeInterval = new TimeInterval(time1, time2);

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

    int time1 = scanner.nextInt() ;
    int time2 = scanner.nextInt() ;






    System.out.println("The number of hours is.:");
    System.out.println(timeInterval.getHours(time1, time2));
    System.out.println("The number of minutes is.:");
    System.out.println(timeInterval.getMinutes(time1, time2));

    }
    }








    public class TimeInterval
    {

    int time1 = scanner.nextInt() ;
    int time2 = scanner.nextInt() ;
    private int hr;
    private int min;




    hr = ((time2 - time1)/100)-(((time2 - time1)/100) % 100);




    public int getHours(){

    return hr;
    }

    public int getMinutes(){

    return min;
    }
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Cool. Thanks for sharing that with us.

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  4. #4
    serjant's Avatar
    serjant is offline Senior Member
    Join Date
    Jun 2008
    Location
    Ukraine,Zaporozhye
    Posts
    487
    Rep Power
    7

    Default

    Quote Originally Posted by Fubarable View Post
    Cool. Thanks for sharing that with us.
    I liked the answer,i am on the floor,LOL :D

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Can you tell me what are you guys talking about here. I didn't see any helpful code segment here...

  6. #6
    Join Date
    Mar 2009
    Posts
    1
    Rep Power
    0

    Talking The answer to your question

    I made a "rough cut" program. This question is asked in java concepts book and it should be solved without an if statement because the problem was in book before we even know what an "if" statement is. The key to this is using the Math.max method call. Well here is the code below. You will have to a litle translation becuase I am to lazy to fix it. It took me a couple of days to solve becuase i had no clue how to atttack it without an if statement, but I guess the max method might actually use one. Any ways here is the answer and ive test it and it works. Good luck. Oh and you have to put in actually valid military time, i didnt put any boundary conditions

    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */

    package book;
    import java.util.Scanner;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    /**
    *
    * @author Owner
    */
    public class MilitaryFormat {


    public static void main ( String[] args )
    {

    Logger.global.setLevel(Level.OFF);
    Scanner scanner = new Scanner(System.in);

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

    String time1 = scanner.nextLine();
    String time2 = scanner.nextLine();

    double x = Double.parseDouble(time1.substring(0,2));
    double xx = Double.parseDouble(time1.substring(2,4))/60;
    double y = Double.parseDouble(time2.substring(0,2));
    double yy = Double.parseDouble(time2.substring(2,4))/60;

    Logger.global.info("x: "+x);
    Logger.global.info("xx: "+xx);
    Logger.global.info("y: "+y);
    Logger.global.info("yy: "+yy);

    double xxx = x+xx;
    double yyy = y + yy;

    Logger.global.info("xxx: "+xxx);
    Logger.global.info("yyy: "+yyy);

    double z = (yyy-xxx)+((Math.max(xxx,yyy)-yyy)/(xxx-yyy))*24;

    Logger.global.info(""+z);

    int hours = (int)z;
    int minutes = (int)Math.round(((z - (int)z)*60));

    System.out.println("The difference in times is "+hours+" hours and "+minutes+" minutes");




    }
    }

  7. #7
    HPcompaq256 is offline Member
    Join Date
    Feb 2010
    Posts
    12
    Rep Power
    0

    Default

    Ok the given code above doesn't seem to compile. Can anyone tell me what could be possibly wrong with the code above? Error in bluej(compiler) comes out to be class, interface, or enum expected.

  8. #8
    HPcompaq256 is offline Member
    Join Date
    Feb 2010
    Posts
    12
    Rep Power
    0

    Default

    I'm talking about the first code given in this thread. Using the main class and the TimeInterval class.

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    What are you trying to do? Complete a homework assignment? If so, you may wish to post the assignment and then your best attempt at the solution (not the code above, but your code if at all possible), and then any error messages or what not, and I'll bet we can help you. Oh, and don't forget code tags. Best of luck!

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by dsohi1821 View Post
    Ok the given code above doesn't seem to compile. Can anyone tell me what could be possibly wrong with the code above? Error in bluej(compiler) comes out to be class, interface, or enum expected.
    Edit: I've looked at the code in the first post and I'm sorry, but it's complete rubbish. Don't even think of getting an idea from it or try to correct it. Instead, if you have the same assignment, start over with your code, and then if you run into problems, again, post your code (with code tags) and your error messages. Also, since this is now your question, do start a new thread for it.

    Again, good luck.

  11. #11
    HPcompaq256 is offline Member
    Join Date
    Feb 2010
    Posts
    12
    Rep Power
    0

    Default

    Ok I'll try my best to generate my own code but can someone tell me what
    int time1 = scanner.nextInt() ; means. What does scanner stand for and what exactly does it do?

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by dsohi1821 View Post
    Ok I'll try my best to generate my own code but can someone tell me what int time1 = scanner.nextInt() ; means. What does scanner stand for and what exactly does it do?
    Sun/Oracle can: Scanning

Similar Threads

  1. Hwlp with "Open", "Save", "Save as..."
    By trill in forum New To Java
    Replies: 3
    Last Post: 11-02-2010, 09:26 AM
  2. Replies: 1
    Last Post: 10-20-2008, 07:35 AM
  3. <core:forEach var="" begin="+<%=j%>+">???
    By freddieMaize in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 09-27-2008, 01:20 AM
  4. Struts 1.x <bean:write format="0.00%"> help?
    By prabhurangan in forum Web Frameworks
    Replies: 0
    Last Post: 07-02-2008, 12:59 PM
  5. "Jumble" or "Scramble" Program
    By Shadow22202 in forum Java Applets
    Replies: 8
    Last Post: 04-30-2008, 03:42 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
  •