[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

Can I ask for some homework help here?

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 7
Thread images: 1

File: 7JOskDw.png (137KB, 1280x544px) Image search: [Google]
7JOskDw.png
137KB, 1280x544px
I'm trying to set up a class that receives a date and if it's not a valid date sets it to 1/1/2000 (among other things it does)

So let's say this is how I check if it's valid

 private static int numDaysInMonth(int month, boolean isLeapYear) 
{
switch (month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
return 31;
case 4:
case 6:
case 9:
case 11:
return 30;
case 2:
if (isLeapYear)
{
return 29;
} else
{
return 28;
}
default:
return 0;
}
}

public static boolean isLeapYear(int year)
{
return (((year % 4 == 0) && !(year % 100 == 0)) || (year % 400 == 0));
}

public static boolean isValidDate(int month, int day, int year)
{
return (month >= 1 && month <= 12) && (day >= 1 && day <= numDaysInMonth(month, isLeapYear(year))) && (year >= 0 && year <= maxYear);
}


And these are my constructors for the Date object

 public Date(int day, int month, int year) 
{
_day = day;
_month = month;
_year = year;
}

// Additional constructor
public Date(Date date)
{
_day = date._day
_month = date._month
_year = date._year
}



How do I "fuse" them so that every date I receive is checked to be valid and reset to 1/1/2000 if not?
>>
I forgot to say it's java
>>
>Terraria
good taste
>>
>>57898064
>
public Date(int day, int month, int year) 
{
_day = day;
_month = month;
_year = year;
}


Who is teaching you java? The _attribute practice is usually used in languages that don't have private attributes to distinguish from public ones.
>>
>>57898588
We aren't allowed to add our own private attributes, and only allowed to add private methods and finals

is that related to why they would do this
>>
>>57898618

Nonsense.

Anyways, for your initial question, on each consutrctor, check if isValidDate(_day, _month, _year) == false, then assign _day = 1, _month = 1, _year = 2000.

Same thing for the 2nd constructor
>>
>>57898653
Why nonsense? Is it stupid to teach that way?


Is it enough to just have the code for checking if a date is valid somewhere in the class, and then do the if isValidDate(_day, _month, _year) == false thing?

Like, do I need to declare any of those ints or booleans somewhere else or is it enough the way I wrote it here?

I'm just still not sure about totally basic stuff like this.

Also, if I need to check if a day I am setting in my set day method

 public void  setDay(int dayToSet)
{
_day = dayToSet;
}


But instead of changing to 1/1/2000 I don't change it at all (IE if a day changes from 31 to 32, nothing happens)
How do I use it here?

Guessing something like

public void  setDay(int dayToSet)
{
if isValidDate(dayToSet, _month, _year)
_day = dayToSet;
}


correct me if I'm wrong here.
Thread posts: 7
Thread images: 1


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties.
Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.