-
August 16th, 2009, 06:46 PM
#1
[RESOLVED] TRying to increment to the next day and next year
Hi,
Here is my task to accomplish so far: Modify the date class of (provided) to perform error checking on the initializer values for instance variables month, date, and year. Also provide a method NextDay to increment the day by one. The Date object should always remain in a consistent state. Write a program that tests the NextDay method in a loop that prints the date during each iteration of the loop to illustrate that the NextDay method works correctly. I have to be careful that I make sure my program increments into the next month and into the next year.
I did created classes and intiliaze values, and created next day. I tried different method but,could not get it right.Can you point me in the right direction. Thanks, and here is my code:
Date.cs
Code:
using System;
using System.Collections.Generic;
using System.Text;
public class Date
{
public static void Main(string[] args)
{
}
private int month, day, year, nextday;
public Date(int theMonth, int theDay, int theYear, int nextDay)
{
Month = theMonth;
Year = theYear;
Day = theDay;
Nextday = theNextDay;
Console.WriteLine( "Date object for date {0}", this );
}
public int Year
{
get
{
return Year;
}
private set{
if (value > 0 && value <= 3000)
year = value;
else
{
Console.WriteLine("Invalid Year ({0}) set to 1.", value);
year = 1;
}
}
}
public int Month
{
get
{
return Month;
}
private set
{
if (value > 0 && value <=12)
month = value;
else
{
Console.WriteLine( "Invalid Month ({0}) set to 1.", value );
month = 1;
}
}
}
public int Day
{
get
{
return day;
}
private set
{
int[] daysPerMonth = { 0, 31, 28, 31, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if (value > 0 && value <= daysPerMonth[Month])
day = value;
else if (Month == 2 && value == 29 && (Year % 400 == 0 || (Year % 4 == 0 && Year % 100 != 0)))
day = value;
else
{
Console.WriteLine("Invalid day {0} set to 1.", value);
day = 1;
}
}
}
public override String ToString()
{
return String.Format("{0}/{1}/{2}", Month, Day, Year);
}
public string ToDateString()
{
return month + "/" + day + "/" + year;
}
public int getnextDay()
{
//int nextDay = 1;
{
if (day.equals("Sun"))
return "Mon";
else if (day.equals("Mon"))
return "Tue";
else if (day.equals("Tue"))
return "Wed";
else if (day.equals("Wed"))
return "Thu";
else if (day.equals("Thu"))
return "Fri";
else if (day.equals("Fri"))
return "Sat";
else
return "Sun";
for (nextDay=0;nextDay<30;nextDay++)
{
nextDay = 33; // This line will ERROR!
Console.WriteLine("Loop: {0}", nextDay);
}
}
}
//end of file
DateTest.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace Date
{
public class DateTest
{
public static void Main(string[] args)
{
Date myDate = new Date();
myDate.nextDate(); // display date
}
}
}
Last edited by lopatko; August 17th, 2009 at 07:46 PM.
Reason: Added code tags
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|