You are on page 1of 3

using System;

namespace try314
{
internal class Time
{
private int hour;
private int minute;
private int second;

public Time(int hour, int minute, int second)


{
if (hour >= 0 && hour <= 23)
{
this.hour = hour;
}

if (minute >= 0 && minute <= 59)


{
this.minute = minute;
}

if (second >= 0 && second <= 59)


{
this.second = second;
}
}

public int getHour()


{
return hour;
}

public int getMinute()


{
return minute;
}

public int getSecond()


{
return second;
}

public void setHour(int hour)


{
if (hour >= 0 && hour <= 23)
{
this.hour = hour;
}
}

public void setMinute(int minute)


{
if (minute >= 0 && minute <= 59)
{
this.minute = minute;
}
}
public void setSecond(int second)
{
if (second >= 0 && second <= 59)
{
this.second = second;
}
}

public void setTime(int hour, int minute, int second)


{
setHour(hour);
setMinute(minute);
setSecond(second);
}

public override string ToString()


{
return $"Time[hour={hour}, minute={minute}, second={second}]";
}

public void nextSecond()


{
if (second == 59)
{
second = 0;
if (minute == 59)
{
minute = 0;
if (hour == 23)
{
hour = 0;
}
else
{
hour++;
}
}
else
{
minute++;
}
}
else
{
second++;
}
}

public void previousSecond()


{
if (second == 0)
{
second = 59;
if (minute == 0)
{
minute = 59;
if (hour == 0)
{
hour = 23;
}
else
{
hour--;
}
}
else
{
minute--;
}
}
else
{
second--;
}
}
}

class Program
{
static void Main(string[] args)
{
Time t = new Time(3, 5, 7);
Console.WriteLine(t);
t.previousSecond();
Console.WriteLine(t);
}
}
}
The changes include:

Fixing the conditional statements in the constructor and setters to use && instead
of ||, since the hour, minute, and second values should all be within their
respective ranges.
Changing the toString() method to ToString() with the proper capitalization.
Changing the return types of nextSecond() and previousSecond() to void, since they
modify the object itself rather than returning a new object.
Fixing the logic in the nextSecond() and previousSecond() methods to properly
handle cases where incrementing or decrementing the seconds causes changes to the
minutes or hours as well.
Adding a Console.WriteLine() statement in the Main() method to print the initial
and modified Time objects

You might also like