24-hour Calculator
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 19

Thread: 24-hour Calculator

  1. #1
    Join Date
    Jan 2012
    Posts
    27

    24-hour Calculator

    Hello, im new to programming, and im learning how, but i wanted to try to make some sort of program that will tell you how long you are at a place, such as work or school.

    I call it the "School Calculator"

    So it works like this:

    You enter when your school starts, lets say 4
    You enter when your school ends, lets say 530
    You enter weather this is AM or PM, lets say there both PM
    It will then tell you:

    "You school is 1 hour, 30 minutes long."

    Now i coded this almost perfuctly, it will tell you this, but it will put everything as just a regular number, like "Your school is 130 hours long"

    So I relised im going to need some sort of 24-hour clock, so that i can brake it into minutes.

    Can anyone tell me how to:

    Make it treat every number as "time" and once it reaches 60 minutes, it turns into 1 hour, after it will display the 60 minutes section (the hours) into a diffrent catagory, so it will say in the code "your school is" + hours + " and " + minutes + "long"

    Here is my code for this school calculator:

    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.io.IOException;
    import java.util.Random;

    public class MultiPro
    {
    public static void write (String text)
    {
    System.out.print (text);
    }


    public static void writeLine (String text)
    {
    System.out.println (text);
    }


    public static void write (int number)
    {
    System.out.print (number);
    }


    public static void writeLine (int number)
    {
    System.out.println (number);
    }


    public static void write (char character)
    {
    System.out.print (character);
    }


    public static void writeLine (char character)
    {
    System.out.println (character);
    }


    public static String readString ()
    {
    BufferedReader br = new BufferedReader (new InputStreamReader (System.in));
    String var = "";
    try
    {
    var = br.readLine ();
    }
    catch (IOException ioe)
    {
    System.out.println (ioe.toString ());
    }
    return var;
    }


    public static int readInt ()
    {
    BufferedReader br = new BufferedReader (new InputStreamReader (System.in));
    String tempVar = null;
    try
    {
    tempVar = br.readLine ();
    }
    catch (IOException ioe)
    {
    System.out.println (ioe.toString ());
    }
    int var = Integer.parseInt (tempVar);
    return var;
    }


    public static char readChar ()
    {
    BufferedReader br = new BufferedReader (new InputStreamReader (System.in));
    char var = 0;
    try
    {
    var = (char) br.read ();
    }
    catch (IOException ioe)
    {
    System.out.println (ioe.toString ());
    }
    return var;
    }


    public static int randomInt ()
    {
    Random rnd = new Random ();
    int number = rnd.nextInt (1) * 3;
    return number;
    }


    public static char randomChar ()
    {
    Random rnd = new Random ();
    char character = (char) (rnd.nextInt (1) * 3);
    return character;
    }

    public static void main (String args[])
    {
    int num1;
    int num2;
    int time1=0;
    int time2;
    String name = null;
    char op = 0;
    int op1;
    int age;
    int sum;
    int result;
    int result2;
    char pass = 0;
    char next = 0;
    int answer;
    int total;
    char timeframe;
    char no = 0;
    int soc1=0;
    int soc2=0;
    int soc3=0;
    int soc4=0;
    int soc5=0;
    int soc6=0;
    int grade;
    char rank;
    int minlength = 10;

    // Option 4

    if (op1 == 4)
    {
    writeLine ("School Calculator selected!");
    writeLine ("");
    write ("When does your school start: ");
    time1 = readInt ();
    writeLine ("");
    writeLine ("AM or PM?");
    timeframe = readChar ();
    if(time1>59){
    time1++;
    }
    write ("When does your school end: ");
    time2 = readInt ();
    answer = time2 - time1;
    writeLine ("AM or PM?");
    timeframe = readChar ();
    writeLine ("Your school is: " + answer + " hours long.");
    writeLine ("*This program is in debug mode, and is still being worked on*");
    writeLine ("");
    writeLine ("Back to menu? (Y|N)");
    writeLine ("");
    op = readChar ();
    }

  2. #2
    Join Date
    Jan 2012
    Posts
    27

    Re: 24-hour Calculator

    Also, im working on a password generator, i got it to work but, it will do the numbers 1 by one, how do i hide this and keep the digets the same? like, if i remove the line that stops it from showing, it will just do somthing like GGGGGGGGGG, instead of randomizing it like Gh5fTn

    heres my code:

    // Option 3
    if (op1 == 3)
    {
    writeLine ("Password Generator selected!");
    writeLine ("");
    writeLine ("This is a random password generator.");
    writeLine ("");
    writeLine ("Type 1 for a new password!");
    next = readChar ();
    if (next == '1')
    {
    final String chars = "abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    String word = "";
    while(word.length() < minlength){
    Random rand = new Random();
    int x = rand.nextInt(62 - 1) +1 ;
    word = word + chars.charAt(x);
    System.out.println(word.toUpperCase());
    }
    //pass = randomChar ();
    writeLine ("Password Generater came up with: " + word);
    writeLine ("");
    writeLine ("");
    writeLine ("Back to menu? (Y|N)");
    writeLine ("");
    op = readChar ();
    while (op == 'N' || op == 'n')
    {
    writeLine ("This is a random password generator.");
    writeLine ("");
    writeLine ("Type 1 for a new password!");
    next = readChar ();
    if (next == '1')
    {

    while(word.length() < minlength){
    Random rand = new Random();
    int x = rand.nextInt(62 - 1) +1 ;
    word = word + chars.charAt(x);
    }
    pass = randomChar ();
    writeLine ("Password Generater came up with: " + pass);
    writeLine ("");
    writeLine ("");
    writeLine ("Back to menu? (Y|N)");
    writeLine ("");
    op = readChar ();


    }

    }
    }

    }
    --------------------------------
    When you run the program, it will look like this:

    Password Generator selected!

    This is a random password generator.

    Type 1 for a new password!
    1
    H
    HK
    HKY
    HKYD
    HKYDE
    HKYDEC
    HKYDECT
    HKYDECTA
    HKYDECTAI
    HKYDECTAIZ
    Password Generater came up with: hkydectAiz


    Back to menu? (Y|N)


    I want it to hide
    H
    HK
    HKY
    HKYD
    HKYDE
    HKYDEC
    HKYDECT
    HKYDECTA
    HKYDECTAI
    HKYDECTAIZ part, but still show the
    Password Generater came up with: hkydectAiz part, thank you in advance for anyone who can help with any of these 2 problems.

  3. #3
    Join Date
    May 2006
    Location
    UK
    Posts
    4,474

    Re: 24-hour Calculator

    Don't ask 2 totally unrelated questions in the same thread, please start a new thread for the second question and leave this thread for the first question or it will get incredibly confusing.

    When posting code please use code tags.

    When dealing with time you need to use the java.util.Date class to hold time values and the java.text.DateFormat class to convert Date objects to and from Strings. Or better still download Joda time and use that library instead.

    BTW Your program doesn't take any notice of the AM/PM tags.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  4. #4
    Join Date
    Jan 2012
    Posts
    27

    Re: 24-hour Calculator

    Quote Originally Posted by keang View Post
    Don't ask 2 totally unrelated questions in the same thread, please start a new thread for the second question and leave this thread for the first question or it will get incredibly confusing.

    When posting code please use code tags.

    When dealing with time you need to use the java.util.Date class to hold time values and the java.text.DateFormat class to convert Date objects to and from Strings. Or better still download Joda time and use that library instead.

    BTW Your program doesn't take any notice of the AM/PM tags.
    Ok.
    Im new to this site, so I dont know how to use "Code tags"

    java.util.date, could you explain this more for me? Like give me an example?

    And yes, I dont have any tags for AM/PM, because when I was going through this, I did not need it, becasue it doesnt need to be reconized other then typed out/desplayed to the user of the program.

  5. #5
    Join Date
    May 2006
    Location
    UK
    Posts
    4,474

    Re: 24-hour Calculator

    Im new to this site, so I dont know how to use "Code tags"
    At the bottom of each of my posts, in blue text, it shows how to use code tags.

    java.util.date, could you explain this more for me? Like give me an example?
    It's not java.util.date, it is java.util.Date - Java is case sensitive.

    Google for something like "Java using DateFormat".
    And yes, I dont have any tags for AM/PM, because when I was going through this, I did not need it, becasue it doesnt need to be reconized other then typed out/desplayed to the user of the program.
    AM & PM make a big difference to the calculation and if you ask a user to supply information then the user will expect the program to use that information in a sensible way. If they told the program they arrived at school at 8 AM and left at 9 PM they would rightly expect the program to say they have been at school for 13 hours and not 1 hour.

    But don't worry, if you use DateFormat with the correct format string it will handle AM/PM for you.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  6. #6
    Join Date
    Jan 2012
    Posts
    27

    Re: 24-hour Calculator

    Quote Originally Posted by keang View Post
    At the bottom of each of my posts, in blue text, it shows how to use code tags.

    It's not java.util.date, it is java.util.Date - Java is case sensitive.

    Google for something like "Java using DateFormat".
    AM & PM make a big difference to the calculation and if you ask a user to supply information then the user will expect the program to use that information in a sensible way. If they told the program they arrived at school at 8 AM and left at 9 PM they would rightly expect the program to say they have been at school for 13 hours and not 1 hour.

    But don't worry, if you use DateFormat with the correct format string it will handle AM/PM for you.
    Thanks for your help man

  7. #7
    Join Date
    May 2006
    Location
    UK
    Posts
    4,474

    Re: 24-hour Calculator

    Are you going to post your second question in a new thread?
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  8. #8
    Join Date
    Jan 2012
    Posts
    27

    Re: 24-hour Calculator

    K so it didint really work out too well... that time thing is 5 hours ahead of my time (its 2:59PM now, it would say its 6:59PM)

    Although I did find an alternitive solution to my code, and sorta got it working, theres still 1 problem though when I was debugging it, how would I do my operation backwards for AM?
    Like if they enterd PM to start (Because mabey you have a night school or your working a little longer for your shift), this is what happens when the user types PM first (AM first works fine)

    School Calculator selected!

    When does your school start: 5

    AM or PM?
    pm
    When does your school end: 2
    AM or PM?
    am
    Your school is: 9 hours long.
    ---------------------------------------
    AM to PM is also a little buggy (for example, if you put 5 am - 10pm, it will say its 7 hours (answer is 17)) Any idea how to resolve this issue?
    Here is my code:

    // Option 4

    if (op1 == 4)
    {
    writeLine ("School Calculator selected!");
    writeLine ("");
    write ("When does your school start: ");
    time1 = readInt ();
    writeLine ("");
    writeLine ("AM or PM?");
    timeframe = readChar ();
    //timeframe2 = readChar ();
    //if (timeframe=='p'){
    //time1+=12;
    //}
    if(time1>59){
    time1++;
    }
    write ("When does your school end: ");
    time2 = readInt ();
    writeLine ("AM or PM?");
    timeframe2 = readChar ();
    //if (timeframe2=='p'){
    //time2+=12;
    //}
    if(timeframe != timeframe2){
    if(timeframe == 'a'){
    time2=+12;
    }

    else{
    time1+=12;
    time2+=24;
    }
    }
    if(timeframe == 'p'){
    time2-=time1;
    time2-=4;
    time2+=time1;
    }
    answer = time2 - time1;
    writeLine ("Your school is: " + answer + " hours long.");
    writeLine ("");
    writeLine ("Back to menu? (Y|N)");
    writeLine ("");
    op = readChar ();
    }

  9. #9
    Join Date
    May 2006
    Location
    UK
    Posts
    4,474

    Re: 24-hour Calculator

    K so it didint really work out too well... that time thing is 5 hours ahead of my time (its 2:59PM now, it would say its 6:59PM)
    That's because you aren't using the correct time zone. But it doesn't really matter as you are only interested in the difference between the times and not what the times are. Once you have parsed the input and created the Date objects you can call the getTime() methods to get the number of milliseconds. Subtract one from the other and you have your time difference in milliseconds.

    Although I did find an alternitive solution to my code, and sorta got it working, theres still 1 problem though when I was debugging it, how would I do my operation backwards for AM?
    AM to PM is also a little buggy
    That's 2 problems, not 1.

    I've told you how to do it, if you don't want to take my advice that's your choice. I've also asked you to post code in code tags which you have also ignored, but this now effects my ability and willingness to help you as unformatted code in hard to read. So why should I help further if you are just going to ignore all the advice/instructions I give you?
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  10. #10
    Join Date
    Jan 2012
    Posts
    27

    Re: 24-hour Calculator

    Quote Originally Posted by keang View Post
    That's because you aren't using the correct time zone. But it doesn't really matter as you are only interested in the difference between the times and not what the times are. Once you have parsed the input and created the Date objects you can call the getTime() methods to get the number of milliseconds. Subtract one from the other and you have your time difference in milliseconds.

    That's 2 problems, not 1.

    I've told you how to do it, if you don't want to take my advice that's your choice. I've also asked you to post code in code tags which you have also ignored, but this now effects my ability and willingness to help you as unformatted code in hard to read. So why should I help further if you are just going to ignore all the advice/instructions I give you?
    You did not tell me how to put code tags... All you told me was it was at the bottom of your comment... It wasnt...

    Yes I relise its 2, but basicly there both related to the same topic at hand...

    Im sorry man... Im really new to coding...

  11. #11
    Join Date
    May 2006
    Location
    UK
    Posts
    4,474

    Re: 24-hour Calculator

    All you told me was it was at the bottom of your comment... It wasnt...
    Yes it is, it's at the bottom of every post I make. Look for the blue text.

    If you don't understand an instruction or advice you are given you should say so. If you just ignore it, it comes over as being rude.

    Did you try to implement the suggestion I gave you, if so show the code and explain where you are stuck.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  12. #12
    Join Date
    Jan 2012
    Posts
    27

    Re: 24-hour Calculator

    Quote Originally Posted by keang View Post
    Yes it is, it's at the bottom of every post I make. Look for the blue text.

    If you don't understand an instruction or advice you are given you should say so. If you just ignore it, it comes over as being rude.

    Did you try to implement the suggestion I gave you, if so show the code and explain where you are stuck.
    That is all I see... I dont see any blue text... Im sorry if I came off rude before...

  13. #13
    Join Date
    May 2006
    Location
    UK
    Posts
    4,474

    Re: 24-hour Calculator

    Are you saying when you view the posts your screen doesn't look like the attached image?
    Attached Images Attached Images  
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  14. #14
    Join Date
    Jan 2012
    Posts
    27

    Re: 24-hour Calculator

    No, it does not look like that, this is what I see:
    Yesterday, 10:52 PM
    keang
    Elite Member Join Date: May 2006
    Location: UK
    Posts: 3,959


    Re: 24-hour Calculator

    --------------------------------------------------------------------------------

    Are you saying when you view the posts your screen doesn't look like the attached image?
    Attached Images cd.gif (23.6 KB, 8 views)

  15. #15
    Join Date
    May 2006
    Location
    UK
    Posts
    4,474

    Re: 24-hour Calculator

    What platform and browser are you using?
    Can you post a screen shot showing what you see.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

Page 1 of 2 12 LastLast

Tags for this Thread

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center