0

I have project in java.

Create CTime class with the following specifications

  1. Attributes: Hour, minute and second
  2. Hour >=0 and <= 23, minute >=0 and <=59 , second >=0 and <= 59
  3. Methods
    • Constructor that updates the CTime attributes
    • set and get methods for each attribute
    • tick method that add 1 second to the CTime object and returns nothing
    • toString method that creates time string with the following format HH:mm:ss e.g. 22:15:01

and I did this

public class CTime {

    int hour;
    int min;
    int sec;

    public CTime(int h, int m, int s) {
        hour = h;
        min = m;
        sec = s;
    }

    public void setHour(int h) {
        hour = h;
    }

    public int getHour() {
        return hour;
    }

    public void setMin(int m) {
        min = m;
    }

    public int getMin() {
        return min;
    }

    public void setSec(int s) {
        sec = s;
    }

    public int getSec() {
        return sec;
    }

    public void tick() {
        sec++;
        if (sec >= 59) {
            sec = 0;
            min++;
            if (min >= 59) {
                min = 0;
            }
            hour++;
        }
    }

    public String toString() {
        String s = hour + ":" + min + ":" + sec;
        return s;
    }
}

How to make my hour and min and sec with 2 digit, e.g. 05:02:09?

And my code is it correct or not?

Sнаđошƒаӽ
  • 16,753
  • 12
  • 73
  • 90
Mohannad
  • 35
  • 6

2 Answers2

2

If you insist on doing this kind of stuff with Strings the way you are, you could do something like the following:

public String toString() {
    String s =  (hour > 9 ? hour : "0" + hour) + ":" + (min > 9 ? min : "0" + min) + ":" + (sec > 9 ? sec : "0" + sec);
    return s;
}
DeadCereal
  • 66
  • 6
  • Can you explain to me what are you doing – Mohannad Apr 27 '16 at 15:23
  • I'm using inline IF statements (not sure of the technical term for this type of statement) to determine whether or not a leading 0 is required in the output. In simple terms, if hour, min, or sec is greater than 9 you will need a leading 0 on your output to maintain 2 digits in each section. It simply determines if the output is single digit, if it is, it prepends a 0 to the line before appending the single digit value, so it will always be 2 digits. – DeadCereal Apr 27 '16 at 17:15
  • 1
    That syntax is called a [ternary operator](https://en.m.wikipedia.org/wiki/Ternary_operation). Just a compact version of an `if-then` statement. – Basil Bourque Apr 27 '16 at 18:00
  • @Mohannad Was my answer helpful to you? If so, please mark it as the answer to your question. It would be greatly appreciated :) – DeadCereal Apr 29 '16 at 02:30
2

Just use String.format()

public String toString() {
    String s = String.format("%02d:%02d:%02d", hour, min, sec);
    return s;
}

One more link here

And your code, yes it is correct. To make it a live clock, all you have to do is call tick() method every second. Take a look at this SO post to learn how to do that.

Community
  • 1
  • 1
Sнаđошƒаӽ
  • 16,753
  • 12
  • 73
  • 90