Skip to main content
Code Review

Return to Answer

Commonmark migration
Source Link

I can see several issues in your code:

  1. Days

    Days

    I guess the row with code days = dd % 7; is some historic rest of code when converting to weeks (?).

  2. Boundaries

    If you call Time(0, 0, 0, 60), the condition if (ss > 60) is not true and the addition minutes++; is not executed. You should use if (ss >= 60). The same error is repeated in other if statements.

  3. Incorrect additions

    Try to call your constructor with Time(0, 0, 0, 86400). If my understanding is correct, you should get 1 day, but you get only 1 minute.

I guess the row with code days = dd % 7; is some historic rest of code when converting to weeks (?).

  1. Boundaries

If you call Time(0, 0, 0, 60), the condition if (ss > 60) is not true and the addition minutes++; is not executed. You should use if (ss >= 60). The same error is repeated in other if statements.

  1. Incorrect additions

Try to call your constructor with Time(0, 0, 0, 86400). If my understanding is correct, you should get 1 day, but you get only 1 minute.

The correct logic should be something like this:

public Time(int dd, int hh, int mm, int ss)
{
 seconds = ss % 60;
 minutes = mm % 60;
 hours = hh % 24;
 days = dd;
 if (ss >= 60)
 {
 minutes = mm + ss / 60;
 }
 if (minutes >= 60)
 {
 hours = hh + minutes / 60;
 minutes %= 60;
 }
 if (hours >= 24)
 {
 days = dd + hours / 24;
 hours %= 24;
 }
}

But, as Jamal mentioned, the constructor is for setting data members and other logic should be splitted into other method(s). That's why my example is not perfect from the design point of view. I also used your variable names in my example. I can imagine better names for them.

I can see several issues in your code:

  1. Days

I guess the row with code days = dd % 7; is some historic rest of code when converting to weeks (?).

  1. Boundaries

If you call Time(0, 0, 0, 60), the condition if (ss > 60) is not true and the addition minutes++; is not executed. You should use if (ss >= 60). The same error is repeated in other if statements.

  1. Incorrect additions

Try to call your constructor with Time(0, 0, 0, 86400). If my understanding is correct, you should get 1 day, but you get only 1 minute.

The correct logic should be something like this:

public Time(int dd, int hh, int mm, int ss)
{
 seconds = ss % 60;
 minutes = mm % 60;
 hours = hh % 24;
 days = dd;
 if (ss >= 60)
 {
 minutes = mm + ss / 60;
 }
 if (minutes >= 60)
 {
 hours = hh + minutes / 60;
 minutes %= 60;
 }
 if (hours >= 24)
 {
 days = dd + hours / 24;
 hours %= 24;
 }
}

But, as Jamal mentioned, the constructor is for setting data members and other logic should be splitted into other method(s). That's why my example is not perfect from the design point of view. I also used your variable names in my example. I can imagine better names for them.

I can see several issues in your code:

  1. Days

    I guess the row with code days = dd % 7; is some historic rest of code when converting to weeks (?).

  2. Boundaries

    If you call Time(0, 0, 0, 60), the condition if (ss > 60) is not true and the addition minutes++; is not executed. You should use if (ss >= 60). The same error is repeated in other if statements.

  3. Incorrect additions

    Try to call your constructor with Time(0, 0, 0, 86400). If my understanding is correct, you should get 1 day, but you get only 1 minute.

The correct logic should be something like this:

public Time(int dd, int hh, int mm, int ss)
{
 seconds = ss % 60;
 minutes = mm % 60;
 hours = hh % 24;
 days = dd;
 if (ss >= 60)
 {
 minutes = mm + ss / 60;
 }
 if (minutes >= 60)
 {
 hours = hh + minutes / 60;
 minutes %= 60;
 }
 if (hours >= 24)
 {
 days = dd + hours / 24;
 hours %= 24;
 }
}

But, as Jamal mentioned, the constructor is for setting data members and other logic should be splitted into other method(s). That's why my example is not perfect from the design point of view. I also used your variable names in my example. I can imagine better names for them.

deleted 31 characters in body
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

I can see several issues in your code. Please, see following suggestions.:

1) Days

  1. Days

I guess the row with code days = dd % 7; is some historic rest of code when converting to weeks (?).

2) Boundaries

  1. Boundaries

If you call Time(0, 0, 0, 60), the condition if (ss > 60) is not true and the addition minutes++; is not executed. You should use if (ss >= 60). The same error is repeated in other if statements.

3) Incorrect additions

  1. Incorrect additions

Try to call your constructor with Time(0, 0, 0, 86400). If my understanding is correct, you should get 1 day, but you get only 1 minute.

The correct logic should be something like this:

public Time(int dd, int hh, int mm, int ss)
{
 seconds = ss % 60;
 minutes = mm % 60;
 hours = hh % 24;
 days = dd;
 if (ss >= 60)
 {
 minutes = mm + ss / 60;
 }
 if (minutes >= 60)
 {
 hours = hh + minutes / 60;
 minutes %= 60;
 }
 if (hours >= 24)
 {
 days = dd + hours / 24;
 hours %= 24;
 }
}

But, as Jamal mentioned, the constructor is for setting data members and other logic should be splitted into other method(s). That's why my example is not perfect from the design point of view. I also used your variable names in my example. I can imagine better names for them.

I can see several issues in your code. Please, see following suggestions.

1) Days

I guess the row with code days = dd % 7; is some historic rest of code when converting to weeks (?).

2) Boundaries

If you call Time(0, 0, 0, 60), the condition if (ss > 60) is not true and the addition minutes++; is not executed. You should use if (ss >= 60). The same error is repeated in other if statements.

3) Incorrect additions

Try to call your constructor with Time(0, 0, 0, 86400). If my understanding is correct, you should get 1 day, but you get only 1 minute.

The correct logic should be something like this:

public Time(int dd, int hh, int mm, int ss)
{
 seconds = ss % 60;
 minutes = mm % 60;
 hours = hh % 24;
 days = dd;
 if (ss >= 60)
 {
 minutes = mm + ss / 60;
 }
 if (minutes >= 60)
 {
 hours = hh + minutes / 60;
 minutes %= 60;
 }
 if (hours >= 24)
 {
 days = dd + hours / 24;
 hours %= 24;
 }
}

But, as Jamal mentioned, the constructor is for setting data members and other logic should be splitted into other method(s). That's why my example is not perfect from the design point of view. I also used your variable names in my example. I can imagine better names for them.

I can see several issues in your code:

  1. Days

I guess the row with code days = dd % 7; is some historic rest of code when converting to weeks (?).

  1. Boundaries

If you call Time(0, 0, 0, 60), the condition if (ss > 60) is not true and the addition minutes++; is not executed. You should use if (ss >= 60). The same error is repeated in other if statements.

  1. Incorrect additions

Try to call your constructor with Time(0, 0, 0, 86400). If my understanding is correct, you should get 1 day, but you get only 1 minute.

The correct logic should be something like this:

public Time(int dd, int hh, int mm, int ss)
{
 seconds = ss % 60;
 minutes = mm % 60;
 hours = hh % 24;
 days = dd;
 if (ss >= 60)
 {
 minutes = mm + ss / 60;
 }
 if (minutes >= 60)
 {
 hours = hh + minutes / 60;
 minutes %= 60;
 }
 if (hours >= 24)
 {
 days = dd + hours / 24;
 hours %= 24;
 }
}

But, as Jamal mentioned, the constructor is for setting data members and other logic should be splitted into other method(s). That's why my example is not perfect from the design point of view. I also used your variable names in my example. I can imagine better names for them.

Source Link
Tomas Paul
  • 632
  • 3
  • 9

I can see several issues in your code. Please, see following suggestions.

1) Days

I guess the row with code days = dd % 7; is some historic rest of code when converting to weeks (?).

2) Boundaries

If you call Time(0, 0, 0, 60), the condition if (ss > 60) is not true and the addition minutes++; is not executed. You should use if (ss >= 60). The same error is repeated in other if statements.

3) Incorrect additions

Try to call your constructor with Time(0, 0, 0, 86400). If my understanding is correct, you should get 1 day, but you get only 1 minute.

The correct logic should be something like this:

public Time(int dd, int hh, int mm, int ss)
{
 seconds = ss % 60;
 minutes = mm % 60;
 hours = hh % 24;
 days = dd;
 if (ss >= 60)
 {
 minutes = mm + ss / 60;
 }
 if (minutes >= 60)
 {
 hours = hh + minutes / 60;
 minutes %= 60;
 }
 if (hours >= 24)
 {
 days = dd + hours / 24;
 hours %= 24;
 }
}

But, as Jamal mentioned, the constructor is for setting data members and other logic should be splitted into other method(s). That's why my example is not perfect from the design point of view. I also used your variable names in my example. I can imagine better names for them.

lang-cs

AltStyle によって変換されたページ (->オリジナル) /