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.

Page 2 of 3 FirstFirst 123 LastLast
Results 26 to 50 of 64

Thread: Time Class problem

  1. #26
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Time Class problem

    The code is missing the ending }s

    Where is the Time1 class code?

    Does the code you have compile and execute?
    If not, copy the full text of any error messages and paste them here if you need help.
    If you don't understand my answer, don't ignore it, ask a question.

  2. #27
    Member
    Join Date
    Apr 2020
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Time Class problem

    Quote Originally Posted by Norm View Post
    The code is missing the ending }s


    Does the code you have compile and execute?
    If not, copy the full text of any error messages and paste them here if you need help.
    it works but i dont recive the second output as 17:45 but as 00:00

  3. #28
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Time Class problem

    it works
    Sorry, I don't see how the posted code can work with the missing parts.
    Please post the complete code that will compile and execute and create some output.


    I'm done for tonight. Back tomorrow.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #29
    Member
    Join Date
    Apr 2020
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Time Class problem

    Quote Originally Posted by Norm View Post
    Sorry, I don't see how the posted code can work with the missing parts.
    Please post the complete code that will compile and execute and create some output.


    I'm done for tonight. Back tomorrow.
    package com.company;
    public class Time1StudentTester
    {
        public static void main(String [] args) {
     
            System.out.println("\n********** Test Time1 - Started **********\n");
            System.out.println("\n1. Testing Constructors and toString:");
            Time1 t1 = new Time1(17, 45);
            System.out.println("\tt1=" + t1);
            Time1 t2 = new Time1(t1);
            System.out.println("\tt2=" + t2);
            System.out.println("\n********** Test Time1 - Finished **********\n");
     
        }
    }
    this outputs the next
    1. Testing Constructors and toString:
    	t1=17:45
    	t2=00:00
    and i need it to be the same

  5. #30
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Time Class problem

    Where is the code for the Time1 class?
    If you don't understand my answer, don't ignore it, ask a question.

  6. #31
    Member
    Join Date
    Apr 2020
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Time Class problem

    Quote Originally Posted by Norm View Post
    Where is the code for the Time1 class?
    package com.company;
     
    public class Time1 {
        private  int _hour;
        private  int _minute;
     
        public Time1(int h, int m)
        {
            this._hour   = (((h >= 0) && (h < 23)) ? h : 0);
            this._minute = (((m >= 0) && (m < 59)) ? m : 0);
        }
        public String toString()
        {
            return String.format("%02d:%02d",_hour,_minute);
        }
    }

  7. #32
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Time Class problem

    That code does not compile without errors.
    The code must compile without errors to be able to execute it for testing.

    If you have code that compiles without errors, please post it.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #33
    Member
    Join Date
    Apr 2020
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Time Class problem

    Quote Originally Posted by Norm View Post
    That code does not compile without errors.
    The code must compile without errors to be able to execute it for testing.

    If you have code that compiles without errors, please post it.
    thats why i need help understand how to continue from here

  9. #34
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Time Class problem

    how to continue from here
    Please copy the full text of the error messages you get and paste it here if you need help fixing it.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #35
    Member
    Join Date
    Apr 2020
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Time Class problem

    Quote Originally Posted by Norm View Post
    Please copy the full text of the error messages you get and paste it here if you need help fixing it.
    Error:(11, 20) java: constructor Time1 in class com.company.Time1 cannot be applied to given types;
      required: int,int
      found: com.company.Time1
      reason: actual and formal argument lists differ in length

  11. #36
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Time Class problem

    Ok, that error says the Time1 class needs a constructor that takes a Time1 object as an argument.
    You posted some code in post#13 that declared a constructor but did not have any statements in it to get the needed data from the Time1 object that was passed to the constructor.
    If you don't understand my answer, don't ignore it, ask a question.

  12. #37
    Member
    Join Date
    Apr 2020
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Time Class problem

    Quote Originally Posted by Norm View Post
    Ok, that error says the Time1 class needs a constructor that takes a Time1 object as an argument.
    You posted some code in post#13 that declared a constructor but did not have any statements in it to get the needed data from the Time1 object that was passed to the constructor.
    so again same question how do i continue from this spot ?????

  13. #38
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Time Class problem

    The error message says you need a constructor that takes a Time1 object as its argument.
    The code in post#13 declares that constructor.
    Copy that code into the class's declaration.
    Add code to that constructor that copies the values from the Time1 object into the class's variables.
    If you don't understand my answer, don't ignore it, ask a question.

  14. #39
    Member
    Join Date
    Apr 2020
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Time Class problem

    Quote Originally Posted by Norm View Post
    The error message says you need a constructor that takes a Time1 object as its argument.
    The code in post#13 declares that constructor.
    Copy that code into the class's declaration.
    Add code to that constructor that copies the values from the Time1 object into the class's variables.
            return  String.format ("%02d:%02d",_hour,_minute);
    this line can be replaced and still get same result ??? i just figured it out that i cant use string.format

  15. #40
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Time Class problem

    this line can be replaced and still get same result
    Replaced with what? Post the new code that gives the same results.


    i just figured it out that i cant use string.format
    Why not? If you get errors, copy the full text and paste it here.
    If you don't understand my answer, don't ignore it, ask a question.

  16. #41
    Member
    Join Date
    Apr 2020
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Time Class problem

    Quote Originally Posted by Norm View Post
    Replaced with what? Post the new code that gives the same results.



    Why not? If you get errors, copy the full text and paste it here.
    i am asking if it can be replaced and if it can than with what ???

  17. #42
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Time Class problem

    Sorry, I have no idea where that code is located or what errors it is causing.
    Please post the full code that creates the error and the full text of the error messages you get.
    If you don't understand my answer, don't ignore it, ask a question.

  18. #43
    Member
    Join Date
    Apr 2020
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Time Class problem

    are you sure you know java ???? cause for now your not helping at all

  19. #44
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Time Class problem

    I am not able to look over your shoulder to see what you are doing. I need you to post the code that is giving the problem and the error messages it gives so I can help.


    cause for now your not helping at all
    You have been making progress and almost have it working. Don't give up now.

    Note: I do not write code that students can copy and paste into their programs. I try to lead a student to find the answer.

    are you sure you know java
    I have been posting on this forum since 2010 and have posted over 22K times.
    If you don't understand my answer, don't ignore it, ask a question.

  20. #45
    Member
    Join Date
    Apr 2020
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Time Class problem

    Quote Originally Posted by Norm View Post
    I am not able to look over your shoulder to see what you are doing. I need you to post the code that is giving the problem and the error messages it gives so I can help.


    I have been posting on this forum since 2010 and have posted over 22K times.
    i am posting over and over and over and one more time over
    package com.company;
    import com.company.Time1;
    public class Time1StudentTester
    {
        public static void main(String [] args) {
     
            System.out.println("\n1. Testing Constructors and toString:");
            Time1 t1 = new Time1(17, 45);
            System.out.println("\tt1=" + t1);
            Time1 t2 = new Time1(t1);
            System.out.println("\tt2=" + t2);
     
            System.out.println("\n2. Testing accessors and mutators:");
            t1.setHour(20);
            t1.setMinute(10);
            System.out.println("\tt1=" + t1);
            System.out.println("\tHour   of t1=" + t1.getHour());
            System.out.println("\tMinute of t1=" + t1.getMinute());
     
     
     
            System.out.println("\n4. Testing equals method:");
            Time1 t3=new Time1(12, 34);
            Time1 t4=new Time1(23, 45);
            System.out.println("\tt3=" + t3);
            System.out.println("\tt4=" + t4);
            if(t3.equals(t4))
                System.out.println("\tt3 is the same time as t4");
            else
                System.out.println("\tt3 isn't the same time as t4");
     
            System.out.println("\n5. Testing before method:");
     
     
            System.out.println("\n6. Testing after method:");
     
     
            System.out.println("\n7. Testing difference method:");
            t1=new Time1(11, 25);
            t2=new Time1(10, 15);
            System.out.println("\tt1=" + t1);
            System.out.println("\tt2=" + t2);
     
     
            System.out.println("\n8. Testing addMinutes method:");
            System.out.println("\tt1=" + t1);
     
     
            System.out.println("\n********** Test Time1 - Finished **********\n");
        }
    }
    this is the code test for the class file Time1.java
    now this is the final output i need to recive
    ********** Test Time1 - Started **********
     
     
    1. Testing Constructors and toString:
    	t1=17:45
    	t2=17:45
    	t33=00:00
     
    2. Testing accessors and mutators:
    	t1=20:10
    	Hour   of t1=20
    	Minute of t1=10
     
    2. Testing out of range setters:
    	t1=00:00
    	Hour   of t33=0
    	Minute of t33=0
     
    3. Testing minFromMidnight method:
    	t1=10:15
    	Minutes from midnight of t1=615
     
    3. Testing minFromMidnight method:
    	t33=00:00
    	Minutes from midnight of t33=0
     
    3. Testing minFromMidnight method:
    	t1=23:59
    	Minutes from midnight of t1=1439
     
    4. Testing equals method:
    	t3=12:34
    	t4=23:45
    	t33=23:45
    	t3=12:34 = t4=23:45 ? false
    	t33=23:45 = t4=23:45 ? true
    	t34=23:46 = t4=23:45 ? false
    	t35=22:45 = t4=23:45 ? false
     
    5. Testing before method:
    	t3=12:34 before t4=23:45 ? true
    	t33=23:45 before t4=23:45 ? false
    	t34=23:46 before t4=23:45 ? false
    	t35=22:45 before t4=23:45 ? true
    	t35=23:44 before t4=23:45 ? true
     
    6. Testing after method:
    	t3=12:34 after t4=23:45 ? false
    	t33=23:45 after t4=23:45 ? false
    	t34=23:46 after t4=23:45 ? true
    	t35=22:45 after t4=23:45 ? false
    	t35=23:44 after t4=23:45 ? false
     
    	t4=23:45 after t3=12:34 ? true
    	tt4=23:45 after t33=23:45 ? false
    	tt4=23:45 after t34=23:46 ? false
    	tt4=23:45 after t35=22:45 ? true
    	tt4=23:45 after t36=23:44 ? true
     
    7. Testing difference method:
    	t1=11:25
    	t2=10:15
    	The difference between 11:25 and 10:15 is : 70 min
    	The difference between 23:45 and 23:45 is : 0 min
    	The difference between 23:45 and 23:46 is : -1 min
    	The difference between 23:45 and 22:45 is : 60 min
    	The difference between 23:45 and 23:44 is : 1 min
     
    8. Testing addMinutes method:
    	t1=11:25
    	Adding 10 minutes to t1=11:35
    	Adding 20 minutes to 23:45= 00:05
    	Adding 48 hours and -5 min to 23:45= 23:40
    	Adding 48 hours and +5 min to 23:45= 23:50
    	Adding 48 hours to 23:45= 23:45
    	Adding -48 hours to 23:45= 23:45
    	Adding -48 hours to 23:45= 23:45
    	Adding -10 minutes to 00:05= 23:55
    	Adding -48 hours and -6 min to 00:05= 23:59
    	Adding -48 hours and +6 min to 00:05= 00:11
     
    ********** Test Time1 - Finished **********
    and now this is my time1 class file
    package com.company;
    public class Time1
    {
        private  int _hour;
        private  int _minute;
        public Time1(int h, int m)
        {
            this._hour   = (((h >= 0) && (h < 23)) ? h : 0);
            this._minute = (((m >= 0) && (m < 59)) ? m : 0);
        }
        public Time1(Time1 t2)
        {
            getHour ();
            getMinute ();
        }
     
        public int getHour()
        {
            return _hour;
        }
        public int getMinute()
        {
            return _minute;
        }
        public void setHour(int _hour)
        {
            this._hour = _hour;
        }
        public void setMinute(int _minute) {
            this._minute = _minute;
        }
    }
    from here how do i need to continue to eventually recive the final output

  21. #46
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Time Class problem

    the final output
    Where did all that code come from?

    This code does NOT get the time values from the argument that was passed to it:
       public Time1(Time1 t2)
        {
            getHour ();
            getMinute ();
        }
    The calls to the methods should use the reference to the passed object : t2.get.... to get the values
    and then those values need to be assigned to the local variables.



    What is the "final output" that you are looking for? What problems are you having writing the code for that output?
    If you don't understand my answer, don't ignore it, ask a question.

  22. #47
    Member
    Join Date
    Apr 2020
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Time Class problem

    ********** Test Time1 - Started **********
     
     
    1. Testing Constructors and toString:
    	t1=17:45
    	t2=17:45
    	t33=00:00
     
    2. Testing accessors and mutators:
    	t1=20:10
    	Hour   of t1=20
    	Minute of t1=10
     
    2. Testing out of range setters:
    	t1=00:00
    	Hour   of t33=0
    	Minute of t33=0
     
    3. Testing minFromMidnight method:
    	t1=10:15
    	Minutes from midnight of t1=615
     
    3. Testing minFromMidnight method:
    	t33=00:00
    	Minutes from midnight of t33=0
     
    3. Testing minFromMidnight method:
    	t1=23:59
    	Minutes from midnight of t1=1439
     
    4. Testing equals method:
    	t3=12:34
    	t4=23:45
    	t33=23:45
    	t3=12:34 = t4=23:45 ? false
    	t33=23:45 = t4=23:45 ? true
    	t34=23:46 = t4=23:45 ? false
    	t35=22:45 = t4=23:45 ? false
     
    5. Testing before method:
    	t3=12:34 before t4=23:45 ? true
    	t33=23:45 before t4=23:45 ? false
    	t34=23:46 before t4=23:45 ? false
    	t35=22:45 before t4=23:45 ? true
    	t35=23:44 before t4=23:45 ? true
     
    6. Testing after method:
    	t3=12:34 after t4=23:45 ? false
    	t33=23:45 after t4=23:45 ? false
    	t34=23:46 after t4=23:45 ? true
    	t35=22:45 after t4=23:45 ? false
    	t35=23:44 after t4=23:45 ? false
     
    	t4=23:45 after t3=12:34 ? true
    	tt4=23:45 after t33=23:45 ? false
    	tt4=23:45 after t34=23:46 ? false
    	tt4=23:45 after t35=22:45 ? true
    	tt4=23:45 after t36=23:44 ? true
     
    7. Testing difference method:
    	t1=11:25
    	t2=10:15
    	The difference between 11:25 and 10:15 is : 70 min
    	The difference between 23:45 and 23:45 is : 0 min
    	The difference between 23:45 and 23:46 is : -1 min
    	The difference between 23:45 and 22:45 is : 60 min
    	The difference between 23:45 and 23:44 is : 1 min
     
    8. Testing addMinutes method:
    	t1=11:25
    	Adding 10 minutes to t1=11:35
    	Adding 20 minutes to 23:45= 00:05
    	Adding 48 hours and -5 min to 23:45= 23:40
    	Adding 48 hours and +5 min to 23:45= 23:50
    	Adding 48 hours to 23:45= 23:45
    	Adding -48 hours to 23:45= 23:45
    	Adding -48 hours to 23:45= 23:45
    	Adding -10 minutes to 00:05= 23:55
    	Adding -48 hours and -6 min to 00:05= 23:59
    	Adding -48 hours and +6 min to 00:05= 00:11
     
    ********** Test Time1 - Finished **********
    and again this is the final output i need to recive i am brand new to java so help me please reach this output

  23. #48
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Time Class problem

    Ok, it is better to work on one problem at a time.
    What is the program's current output? Copy the program's output and paste it here.
    Add some comments to the print out showing where it needs to be changed.
    For example:
    1. Testing Constructors and toString:
    	t1=17:45
    	t2=17:45
    	t33=00:00   <<<<<<<<< THIS should be t33=11:22

    Did you see my comments about one of the Time1's constructors? It needs to be changed to get the values from the passed argument and saved in the current object.
    If you don't understand my answer, don't ignore it, ask a question.

  24. #49
    Member
    Join Date
    Apr 2020
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Time Class problem

    Quote Originally Posted by Norm View Post
    Ok, it is better to work on one problem at a time.
    What is the program's current output? Copy the program's output and paste it here.
    Add some comments to the print out showing where it needs to be changed.
    For example:
    1. Testing Constructors and toString:
    	t1=17:45
    	t2=17:45
    	t33=00:00   <<<<<<<<< THIS should be t33=11:22

    Did you see my comments about one of the Time1's constructors? It needs to be changed to get the values from the passed argument and saved in the current object.
    1. Testing Constructors and toString:
    	t1=com.company.Time1@1b6d3586
    	t2=com.company.Time1@4554617c
     
    2. Testing accessors and mutators:
    	t1=com.company.Time1@1b6d3586
    	Hour   of t1=20
    	Minute of t1=10
     
    4. Testing equals method:
    	t3=com.company.Time1@74a14482
    	t4=com.company.Time1@1540e19d
    	t3 isn't the same time as t4
     
    5. Testing before method:
     
    6. Testing after method:
     
    7. Testing difference method:
    	t1=com.company.Time1@677327b6
    	t2=com.company.Time1@14ae5a5
     
    8. Testing addMinutes method:
    	t1=com.company.Time1@677327b6
     
    ********** Test Time1 - Finished **********
    current output

  25. #50
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Time Class problem

    t1=com.company.Time1@1b6d3586
    I don't see any comments as I requested on this line. I assume that is not what is desired.
    The value after the = is the String returned by the Object class's default toString method. The Time1 method needs a toString method that returns the String you want to see.
    What happened to the toString method that was in earlier versions of the Time1 class? Copy it into the new version. See post#31
    If you don't understand my answer, don't ignore it, ask a question.

Page 2 of 3 FirstFirst 123 LastLast

Similar Threads

  1. current execution time of a class in java by running another class
    By priyap761 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 14th, 2014, 06:12 AM
  2. Time class - previousSecond()
    By kennylty in forum Object Oriented Programming
    Replies: 1
    Last Post: March 7th, 2014, 10:16 AM
  3. Problem in getting time of different time zone
    By anks.coder in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 13th, 2013, 05:44 PM
  4. Help!! Schedual class with time and date
    By passoa in forum What's Wrong With My Code?
    Replies: 7
    Last Post: August 30th, 2013, 07:36 AM
  5. Replies: 2
    Last Post: November 18th, 2012, 02:09 PM

Tags for this Thread