Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 14 of 14

Thread: Need Help ASAP with my code. should be an easy one for you guys!

  1. #1
    Junior Member
    Join Date
    Dec 2011
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need Help with my code. should be an easy one for you guys!

    Hello all.
    I have an assignment due soon and I'm struggling with this code.
    a little background of what I needed to do:
    create a class that will receive input from user representing time (H for hour M for minute and S for second)
    this program however only has one variable which is: _secFromMid.
    The program then stores whatever the user inputted in the form of seconds only.
    the class is supposed to then be able to retrieve all of the original parameters from _secFromMid.

    Here is the code for the class I have written:
    *Please ignore the mistakes in the API as I know the changes needed to be done there..


    public class Time2 {
     
        private long _secFromMid;
     
        //constructors:
        /**
        * creates a new Time object
        * @param _hour the hour in the time(0-23)
        * @param _minute the minute in the time(0-59)
        * @param _second the second inthe time (0-59)
        */
     
        public Time2(int h, int m, int s) {
            if(h<0 || h>23)
            h=0;
            if(m<0 || m>59)
            m=0;
            if(s<0 || s>59)
            s=0;
            _secFromMid=h*3600+m*60+s;
        }
        public Time2 (Time2 other){
            _secFromMid=other._secFromMid;
        }
        /** gets the year */
        public int getHour(){
            return (int)((_secFromMid-_secFromMid%3600)/3600);
        }
     
        /** gets the month */
        public int getMinute(){
            return (int)((_secFromMid%3600)/60);
        }
     
        /** gets the Day */
        public int getSecond(){
            return (int)(_secFromMid-((_secFromMid-_secFromMid%3600)+(_secFromMid%3600)));
        }
     
        /** sets the year
         * @param yearToSet the value to be set
         */
        public void setHour(int num){
           if(num<0 || num>23)
               _secFromMid=_secFromMid;
               else
               _secFromMid=num*3600+(_secFromMid-(_secFromMid-_secFromMid%3600));
     
        }
     
        /** set the month
         * @param monthToSet the value to be set
         */
        public void setMinute(int num){
            if(num<0 || num>59)
               _secFromMid=_secFromMid;
               else
               _secFromMid=num*60+(_secFromMid-_secFromMid%3600);
        }
        /** sets the day 
         *  @param dayToSet the value to be set
         */
     
        public void setSecond (int num){
           if(num<0 || num>59)
              _secFromMid=_secFromMid;
              else
              _secFromMid=num+(_secFromMid-((_secFromMid-_secFromMid%3600)/3600)+(_secFromMid%3600));
        }
        public String toString(){
            String res="";
            if(((_secFromMid-_secFromMid%3600)/3600)<10)
               res=res+"0";
            res=res+((_secFromMid-_secFromMid%3600)/3600)+":";
            if(((_secFromMid%3600)/60)<10)
               res=res+"0";
            res=res+((_secFromMid%3600)/60)+":";
            if((_secFromMid-(((_secFromMid-_secFromMid%3600)/3600)+(_secFromMid%3600)/60))<10)
               res=res+"0";
            res=res+(_secFromMid-(((_secFromMid-_secFromMid%3600)/3600)+(_secFromMid%3600)/60));
            return res;
        }
        public boolean equals (Time2 other){
            if(_secFromMid==other._secFromMid)
               return true;
            return false; 
        }
        public int difference (Time2 other){
            return (int)_secFromMid-(int)other._secFromMid;
        }
        /**
        * checks if this date comes before a given date
        * @param date2 the given date
        * @retun true iff this date comes before date2
        */
     
        public boolean before(Time2 other) {
            if (_secFromMid < other._secFromMid)
                return true;
            return false;
        }
        public boolean after(Time2 other) {
              return other.before(this);
            }
     
    }





    Here is the tester given to me I need to run this code with:

    *This tester is not to be tempered with



    public class Time2Driver {
     
        public static void main(String[] args) {
            Time2 firstTime = new Time2(18, 33, 59);
            Time2 secondTime = new Time2(firstTime);
     
            System.out.println("The hour of first Time2 object is: " + firstTime.getHour());
            System.out.println("The minute of first Time2 object is: " + firstTime.getMinute());
            System.out.println("The second of first Time2 object is: " + firstTime.getSecond());
     
            secondTime.setHour(16);
            secondTime.setMinute(55);
            secondTime.setSecond(21);
     
            System.out.println("The hour of second Time2 object is: " + secondTime.getHour());
            System.out.println("The minute of second Time2 object is: " + secondTime.getMinute());
            System.out.println("The second of second Time2 object is: " + secondTime.getSecond());
     
            System.out.println("Is the first time equal to the second time? " + firstTime.equals(secondTime));
     
            System.out.println("Is the first time before the second time? " + firstTime.before(secondTime));
     
            System.out.println("Is the first time after the second time? " + firstTime.after(secondTime));
     
            System.out.println("The difference in seconds between the first and second time is: " + firstTime.difference(secondTime));
     
            System.out.println("The string representation of the first time is: " + firstTime.toString());
        }
    }



    The output I need to write my class by is as follows:


    Hour of first time is: 18
    Minute of first time is: 33
    Second of first time is: 59
    Hour of second time is: 16
    Minute of second time is: 55
    Second of second time is: 21
    Is the first time equal to the second time? false
    Is the first time before the second time? false
    Is the first time after the second time? true
    The difference in seconds between the first and second time is: 5918
    The string representation of the first time is: 18:33:59





    I seem to be struggling with the output of the seconds as everything else seems to follow properly.
    there must be something wrong in this line of code I wrote that keeps outputting the value 0 instead of 59:
    return (int)(_secFromMid-((_secFromMid-_secFromMid%3600)+(_secFromMid%3600)));



    Would appreciate some help! thanks so much!
    Last edited by GalBenH; December 4th, 2011 at 09:33 PM. Reason: I guess title was too offiensive?


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Need Help ASAP with my code. should be an easy one for you guys!

    ASAP

    What makes you more important than anyone else asking for help? I'll get back to when I feel like it not when you demand it.
    Improving the world one idiot at a time!

  3. #3
    Junior Member
    Join Date
    Dec 2011
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need Help ASAP with my code. should be an easy one for you guys!

    I'm not asking to get faster responses than people who have asked questions before me and have yet gotten their answers.
    But I'm asking that if from reading this post,someone holds the answer for me and has no other answers for other posts in this forum, a prompt response would be very greatly appreciated as this is a time sensitive matter for me.
    Thanks in advance for any help with this. and I did not mean to cause any inconvenience or anger with anyone. my apologies if I did so.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need Help ASAP with my code. should be an easy one for you guys!

    Please edit your post and add code tags around your code to preserve its formatting. Unformatted code is hard to read and understand.
    See: BB Code List - Java Programming Forums - The Java Community

    this line of code I wrote that keeps outputting the value 0
    Break the statement up into parts and print out the values of all the parts to see why the results is zero.
    Last edited by Norm; December 4th, 2011 at 09:32 PM.

  5. #5
    Junior Member
    Join Date
    Dec 2011
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need Help ASAP with my code. should be an easy one for you guys!

    I was afraid of that and was not aware of the code. will look into it now . thanks

  6. #6
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Need Help ASAP with my code. should be an easy one for you guys!

    Quote Originally Posted by GalBenH View Post
    I'm not asking to get faster responses
    What other purpose was marking your post as ASAP? The only conclusion people can make is that you want an answer now.
    Improving the world one idiot at a time!

  7. #7
    Junior Member
    Join Date
    Dec 2011
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need Help ASAP with my code. should be an easy one for you guys!

    I meant faster responses than others who posted before me and have yet gotten their answers.
    I just know that some people tend to look at threads and while they may have the answer to them. they tend to not answer right away but get back to it later.

    lets drop it guys. it's not getting anywhere.
    I again, apologize if I offended anyone. english is not my first language and things may have been interpreted in a manner I certainly did not want them to.
    I tried to edit the thread's title but had no success doing so.
    Lets please move one.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need Help ASAP with my code. should be an easy one for you guys!

    this line of code I wrote that keeps outputting the value 0
    Break the statement up into parts and print out the values of all the parts to see why the results is zero

  9. #9
    Junior Member
    Join Date
    Dec 2011
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need Help ASAP with my code. should be an easy one for you guys!

    Thanks for your reply.
    I tried doing so but I still can't figure it out.
    my logic says that the seconds should be

    seconds = total number of seconds - (hour represented by seconds + minutes represented by seconds)

    which is what my code represents, no?
    It did after all manage to print out the hours and minutes properly so the equations for those were correct.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need Help ASAP with my code. should be an easy one for you guys!

    You do not show what prints out when you do as I suggested. That will tell you what you code is doing.
    The formulas you just posted are what you WANT the code to do. But you need to see what the code is DOING.

  11. #11
    Junior Member
    Join Date
    Dec 2011
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need Help ASAP with my code. should be an easy one for you guys!

    You're going to have to forgive my ignorance because I'm not sure what you mean by see what the code is doing. examining the code on paper seems right to me.
    Is there a way to translate it into what the program is actually doing and how it sees the variables in my equation?
    I've only been using Java for a week and a half now and I have not been taught that yet.
    btw I'm using BlueJ if it helps. I tried playing around with it using the debugger but it didn't seem to shed much light on the topic.
    Thanks again for your help, I really appreciate it

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need Help ASAP with my code. should be an easy one for you guys!

    Break the equation up into pieces and print out each piece:
    System.out.println("secFromMid-secFromMid%3600="+ (secFromMid-secFromMid%3600));

    For ease of testing, I suggest you work with numbers that are easy to work with in your head:
    Time2 firstTime = new Time2(1, 1, 59);

    Doing it on paper is one way, but that does NOT show you what the computer will do with the code. You must have the computer evaluate the expressions and show you what it is doing.

  13. #13
    Junior Member
    Join Date
    Dec 2011
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need Help ASAP with my code. should be an easy one for you guys!

    Got it, and fixed!
    Many thanks Norm!

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need Help ASAP with my code. should be an easy one for you guys!

    Let println be your friend.

Similar Threads

  1. [SOLVED] Can someone help me with this code asap
    By Darkcore123 in forum What's Wrong With My Code?
    Replies: 12
    Last Post: November 14th, 2011, 11:55 AM
  2. new to java need help with code asap
    By stark007 in forum Object Oriented Programming
    Replies: 1
    Last Post: November 13th, 2011, 01:25 PM
  3. Need some help with this code - Asap
    By Th3T3chGuy in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 10th, 2011, 07:17 AM
  4. Array code problem Please help fairly easy
    By LOPEZR in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 28th, 2011, 10:51 AM
  5. [SOLVED] Begginer Question please help ASAP! (Easy to Answer)
    By Bagzli in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 11th, 2010, 10:00 PM