Hi I made this code that will show when it run a clock and then ask the user to enter new seconds and minutes and hours so they will be added to the old clock I did so but the results are wrong where is the mistake
the main
static void Main(string[] args)
{
Timer S1 = new Timer();
S1.Sec = 10;
S1.Min = 25;
S1.Hour = 23;
S1.Read();
S1.Write();
}
the timer class
namespace Uni_19
{
class Timer
{
private int Seconds;
private int Minutes;
private int Hours;
private int Days = 0;
public int Sec
{
get { return Seconds; }
set
{
Seconds = value % 60;
Minutes += value / 60;
}
}
public int Min
{
get { return Minutes; }
set
{
Minutes = value % 60;
Hour += (value / 60);
}
}
public int Hour
{
get { return Hours; }
set
{
Hours = value % 24;
Days += value / 24;
}
}
public int Day
{
get { return Days; }
set { Days = Hour / 24; }
}
public int AddSec(int A)
{
Sec += A;
return Sec;
}
public int AddMin(int B)
{
Min += B;
return Min;
}
public int AddHour(int C)
{
Hour = Hour + (C % 24);
return Hour;
}
public int AddDay(int D)
{
Day += D;
return Day;
}
public void Read()
{
Console.WriteLine("Time Is: {0}", Hours + " : " + Minutes + " : " + Seconds);
Console.Write("Time Added (in Seconds): ");
Sec = int.Parse(Console.ReadLine());
Console.Write("Time Added (in Minutes): ");
Min = int.Parse(Console.ReadLine());
Console.Write("Time Added (in Houres): ");
Hour = int.Parse(Console.ReadLine());
}
public void Write()
{
Console.WriteLine("New Time Is: {0}", AddDay(Day) + " : " + AddHour(Hour) + " : " + AddMin(Min) + " : " + AddSec(Sec));
}
}
}
thanx in advance
You are using the user input for both setting the day and adding to it. The old time is completely overwritten and not used in the calculation.
On top of that, the AddSec routine can return a number greater than 60, which is probably not what you want.
What is the specification for your homework assignment? Your Timer class is completely unnecessary in .NET because the functionality it provides is already covered by the DateTime and TimeSpan classes. I’m assuming though that you are not allowed to do that otherwise it would make your assignment too easy?
Possible fix, changing
Sec =toAddSec, etc:Full source code: