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 1 of 3 123 LastLast
Results 1 to 25 of 64

Thread: Time Class problem

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

    Default Time Class problem

    hi guys please help i got a problem with time programming
    in my code there is a main class named Time1
    than it supposed to contain 2 instance variables int _hour - representing the time (0-23)
    and int _minute - representing the minute(0-59)
    than in this class there is a constructors one named public Time1(int h,int m)
    how do i begin Please Help me

  2. #2
    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: Time Class problem

    Take a look at the tutorial re classes: https://docs.oracle.com/javase/tutor...O/classes.html
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    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
    Take a look at the tutorial re classes: https://docs.oracle.com/javase/tutor...O/classes.html
    i looked every where the problem is not with creating class but working with time package et'c

  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: Time Class problem

    working with time package
    Ok, so you can create the needed classes.
    Can you explain what the problem is? What do you mean by a time package? What problems are there working with it?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    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, so you can create the needed classes.
    Can you explain what the problem is? What do you mean by a time package? What problems are there working with it?
    i got no idea how to work with the time library
    this is the final output i need to recive
    ********** Test Time1 - Started **********
     
     
    1. Testing Constructors and toString:
    	t1=17:45
    	t2=17:45
     
    2. Testing accessors and mutators:
    	t1=20:10
    	Hour   of t1=20
    	Minute of t1=10
     
    3. Testing minFromMidnight method:
    	t1=10:15
    	Minutes from midnight of t1=615
     
    4. Testing equals method:
    	t3=12:34
    	t4=23:45
    	t3 isn't the same time as t4
     
    5. Testing before method:
    	t3 is before t4
     
    6. Testing after method:
    	t4 is after t3
     
    7. Testing difference method:
    	t1=11:25
    	t2=10:15
    	The difference in minutes between times t1 and t2 is : 70
     
    8. Testing addMinutes method:
    	t1=11:25
    	Adding 10 minutes to t1=11:35
     
    ********** Test Time1 - Finished **********
    Last edited by CrashOverride417; April 16th, 2020 at 05:42 PM.

  6. #6
    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: Time Class problem

    how to work with the time library
    What is in the time library? Do you have good documentation on how to use it?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    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
    What is in the time library? Do you have good documentation on how to use it?
    take a look at the final output i added maybe this will tell you what is my strugle

  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: Time Class problem

    I missing where java programming is involved in your problem. Can you ask something more specific java questions?

    output i added maybe this will tell you what is my strugle
    Please explain how you plan to use java.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Time Class problem

    package com.company;
    public class Time1 {
        private final int _hour;
        private final int _minute;
     
        public Time1(int h, int m) {
            _hour = ((h >= 0 && h < 23) ? h : 0);
            _minute = ((m >= 0 && m < 59) ? m : 0);
        }
        public String toString() {
            return String.format("%02d:%02d", _hour, _minute);
        }
    }
    i managed to begin but now i need to out put this
    Time1 t2 = new Time1(t1);
            System.out.println("\tt2=" + t2);
    what can i do ???
    Last edited by CrashOverride417; April 16th, 2020 at 05:43 PM.

  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: Time Class problem

    i need to out put this
    Time1 t2 = new Time1(t1);
    System.out.println("\tt2=" + t2);
    what can i do ???
    Write a testing class and method that executes those two lines.


    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    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
    Write a testing class and method that executes those two lines.


    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    how can i clone the first output to the second out put t1 and t2 should output same

  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: Time Class problem

    If t1 and t2 are instances of the same class, then the String returned by the toString() method should be formatted the same way.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    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
    If t1 and t2 are instances of the same class, then the String returned by the toString() method should be formatted the same way.
    public Time1(Time1 t2)
        {
            return ;
        }
    is this ok ?

  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: Time Class problem

    is this ok ?
    What is that code supposed to do?
    It appears not to do anything.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    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
    What is that code supposed to do?
    It appears not to do anything.
    so how can i duplicate the output ??? can you show me please

  16. #16
    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: Time Class problem

    how can i duplicate the output
    Is this what you mean by duplicate output:
       // print two duplicate lines
       System.out.println("a line");
       System.out.println("a line");

    Otherwise please explain and show an example of how you print the one line you want to duplicate.
    If you don't understand my answer, don't ignore it, ask a question.

  17. #17
    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
    Is this what you mean by duplicate output:
       // print two duplicate lines
       System.out.println("a line");
       System.out.println("a line");

    Otherwise please explain and show an example of how you print the one line you want to duplicate.
    Time1 t2 = new Time1(t1);
            System.out.println("\tt2=" + t2);
    first line appears to be an object which requires construct in the main class file

  18. #18
    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: Time Class problem

    first line appears to be an object which requires construct in the main class file
    Are you saying that the line:
    Time1 t2 = new Time1(t1);
    creates an instance of the class Time1 by calling its constructor?

    What is the main class file? Is that the file that holds the declaration for the class with the main() method?

    requires construct
    Does that refer to a line of code?
    If you don't understand my answer, don't ignore it, ask a question.

  19. #19
    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
    Are you saying that the line:
    Time1 t2 = new Time1(t1);
    creates an instance of the class Time1 by calling its constructor?

    What is the main class file? Is that the file that holds the declaration for the class with the main() method?


    Does that refer to a line of code?
    1. Testing Constructors and toString:
    	t1=17:45
    	t2=17:45
    just help output this lines please and that will do enough

  20. #20
    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: Time Class problem

    Ok write the code that will execute and generate the output for the first line.
    Post the code and the output it creates when executed.

    When that works, we'll work on outputting the second line.
    If you don't understand my answer, don't ignore it, ask a question.

  21. #21
    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 write the code that will execute and generate the output for the first line.
    Post the code and the output it creates when executed.

    When that works, we'll work on outputting the second line.
    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);
        }

  22. #22
    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: Time Class problem

    Ok, that is a start. Now where is the code that will cause it to execute and print out the desired values?
    Post that code and also copy and paste here the output that it creates when it is executed.
    If you don't understand my answer, don't ignore it, ask a question.

  23. #23
    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 is a start. Now where is the code that will cause it to execute and print out the desired values?
    Post that code and also copy and paste here the output that it creates when it is executed.
    1. Testing Constructors and toString:
    	t1=17:45
    	t2=17:45
    this is first input i am missing the t2 = 17:45
    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);
    this is the class testing

  24. #24
    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: Time Class problem

    this is the class testing
    Sorry that is not a class. That is a few lines of code.
    Can you put that code in a method that is in a class with a main method so that the code can be compiled and executed?
    If you don't understand my answer, don't ignore it, ask a question.

  25. #25
    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 that is not a class. That is a few lines of code.
    Can you put that code in a method that is in a class with a main method so that the code can be compiled and executed?
    i am required to build a class named Time1 and than run the test class on the Time 1 class to see its compiling without errors
    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);
    i only need to build for now this part of the final output

Page 1 of 3 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