# daysOfWeek function implementation

posted 4 years ago

Hello

I need to implement a function that returns the day of weeks passed in a year according to these:

- week 1 in any year is the week of Jan 1st

- weeks start on Mondays (not like the ISO defined method)

I have implemented these subroutines so far that can help me:

public static int ymd2w(int year, int month, int day) calculates the weekday of a give date (Sunday=0, Monday=1,....Sat=6)

public static int whichDay(int year, int month, int day) returns the day count of date input

This is what i have done. It calculates the number of weeks by excluding the first week of the year and the actual week then divinding by 7, but cannot get it to count the weeks from Monday always does it from Sunday.

Could someone help to get this work, wasted more than enough time on this. Thanks a lot

[Edit - added code tags - MB]

I need to implement a function that returns the day of weeks passed in a year according to these:

- week 1 in any year is the week of Jan 1st

- weeks start on Mondays (not like the ISO defined method)

I have implemented these subroutines so far that can help me:

public static int ymd2w(int year, int month, int day) calculates the weekday of a give date (Sunday=0, Monday=1,....Sat=6)

public static int whichDay(int year, int month, int day) returns the day count of date input

This is what i have done. It calculates the number of weeks by excluding the first week of the year and the actual week then divinding by 7, but cannot get it to count the weeks from Monday always does it from Sunday.

Could someone help to get this work, wasted more than enough time on this. Thanks a lot

[Edit - added code tags - MB]

Joanne Neal

Rancher

Posts: 3742

16

posted 4 years ago

- 1

Have you looked at the Calendar class ? It has methods which should help you do what you want.

Joanne

posted 4 years ago

Your original post didn't say what was wrong, so that's probably why nobody has tried to help.

You might think you said what was wrong, but you explained it in terms that only you can understand. For example you said

but it is not at all obvious to somebody who tries to read your code what "counting weeks from Monday" is supposed to mean. Now if you had said "for this particular set of inputs I should see line X outputting Y but I'm seeing Z instead" then it might be possible to say something about that. Instead we just have some cryptic code with not-very-meaningful variable names and no comments explaining it.

You might think you said what was wrong, but you explained it in terms that only you can understand. For example you said

cannot get it to count the weeks from Monday always does it from Sunday

but it is not at all obvious to somebody who tries to read your code what "counting weeks from Monday" is supposed to mean. Now if you had said "for this particular set of inputs I should see line X outputting Y but I'm seeing Z instead" then it might be possible to say something about that. Instead we just have some cryptic code with not-very-meaningful variable names and no comments explaining it.

posted 4 years ago

Apologies!. I make this mistake in real life too plus I am a rookie.

So what my function (public static in whichWeek (int year, int month, int day) needs to do is to return the week number of a given date. Week 1 is in any year the week of Jan 1st, weeks start on Mondays (in contrast to the week counting method defined by in ISO, where week one of a year is set to be the week of the first Thursday of the year)

I hope it s clear now. Appreciate any help really...

I have these working functions:

public static boolean checkDate(int year, int month, int day) //returns info whether the given day actually exists/existed, true if valid, false otherwise

public static int whichDay(int year, int month, int day) //returns the day count, or -1 if the given date is invalid. day count is the number of days passed since the start of the year plus one

public static int ymd2w (int year, int month, int day) calcualates the weekday of a given date (returns the weekday as: Sunday=0, Monday=1,....Sat=6)

# public static int whichWeek(int year, int month, int day){

# boolean check=checkDate(year, month, day); //check if date exists

# if (check==false)return -1;

#

# int dayTotal=whichDay(year, month, day); //check the total number of days that has passed in the year

#

# int dayJan1=ymd2w(year, 1,1); //check what day of the week the first of January was

#

# int dayWeekactual=ymd2w(year, month, day); //check what day of the week the inputted date is

#

# dayTotal = dayTotal-(8-dayJan1);

# TextIO.putln("dayNumber minus first week" + dayTotal); //distracts the days in the first week

#

# dayTotal = dayTotal-(dayWeekactual);

# TextIO.putln("dayNumber minus last week should be multiple of 7: " +dayTotal); //distrancts the days in the last week now i am left with the weeks in between should be a multiple of 7

#

# int weekNo = (dayTotal/7)+1;

# TextIO.putln("week number " + weekNo);

# return weekNo;

#

# }

So what my function (public static in whichWeek (int year, int month, int day) needs to do is to return the week number of a given date. Week 1 is in any year the week of Jan 1st, weeks start on Mondays (in contrast to the week counting method defined by in ISO, where week one of a year is set to be the week of the first Thursday of the year)

I hope it s clear now. Appreciate any help really...

I have these working functions:

public static boolean checkDate(int year, int month, int day) //returns info whether the given day actually exists/existed, true if valid, false otherwise

public static int whichDay(int year, int month, int day) //returns the day count, or -1 if the given date is invalid. day count is the number of days passed since the start of the year plus one

public static int ymd2w (int year, int month, int day) calcualates the weekday of a given date (returns the weekday as: Sunday=0, Monday=1,....Sat=6)

# public static int whichWeek(int year, int month, int day){

# boolean check=checkDate(year, month, day); //check if date exists

# if (check==false)return -1;

#

# int dayTotal=whichDay(year, month, day); //check the total number of days that has passed in the year

#

# int dayJan1=ymd2w(year, 1,1); //check what day of the week the first of January was

#

# int dayWeekactual=ymd2w(year, month, day); //check what day of the week the inputted date is

#

# dayTotal = dayTotal-(8-dayJan1);

# TextIO.putln("dayNumber minus first week" + dayTotal); //distracts the days in the first week

#

# dayTotal = dayTotal-(dayWeekactual);

# TextIO.putln("dayNumber minus last week should be multiple of 7: " +dayTotal); //distrancts the days in the last week now i am left with the weeks in between should be a multiple of 7

#

# int weekNo = (dayTotal/7)+1;

# TextIO.putln("week number " + weekNo);

# return weekNo;

#

# }

Campbell Ritchie

Sheriff

Posts: 49775

69

posted 4 years ago

Any assignment statement in the form"isLeap==true" / "check==false"? Do you mean? Can you explain what would be considered good style and why they are prone to errors? Thanks

This is the function i finally submitted for the week number exercise. It still doesnt handle the first week of the year correctly and the turning days for new weeks is still not Monday but Sunday

public static int whichWeek(int year, int month, int day) {

boolean check = checkDate(year, month, day);

if (check == false)

return -1;

int mon = 0;

int dayWeekActual = ymd2w(year, month, day); // returns which weekday the entered date falls on (0-6, sun-sat)

int dayCountSum = whichDay(year, month, day); //returns the total number of days passed until the entered date

int dayJan01 = ymd2w(year, 1, 1); //calculates the day of the week for the first day of the given year

if (month == 1 && day == 1)

return mon = 1;

else if (dayJan01 == 1) {

mon = 1;

while (mon <= dayWeekActual) {

mon += 7;

}

return mon / 7;

} else {

while (mon <= dayCountSum)

mon += 7;

}

return mon / 7 + 1;

}

Thanks!

This is the function i finally submitted for the week number exercise. It still doesnt handle the first week of the year correctly and the turning days for new weeks is still not Monday but Sunday

public static int whichWeek(int year, int month, int day) {

boolean check = checkDate(year, month, day);

if (check == false)

return -1;

int mon = 0;

int dayWeekActual = ymd2w(year, month, day); // returns which weekday the entered date falls on (0-6, sun-sat)

int dayCountSum = whichDay(year, month, day); //returns the total number of days passed until the entered date

int dayJan01 = ymd2w(year, 1, 1); //calculates the day of the week for the first day of the given year

if (month == 1 && day == 1)

return mon = 1;

else if (dayJan01 == 1) {

mon = 1;

while (mon <= dayWeekActual) {

mon += 7;

}

return mon / 7;

} else {

while (mon <= dayCountSum)

mon += 7;

}

return mon / 7 + 1;

}

Thanks!

Joanne Neal

Rancher

Posts: 3742

16

posted 4 years ago

if (check == false)

should be

if (!check)

and

if (check == true)

should be

if (check)

It's less error prone because if you accidentally type only one =, it is still valid syntax but wont do what you expect.

if (check = false)

will assign the value false to check and evaluate to false always.

And as an FYI

Those aren't assignment statements - they are conditional statements. With a single = they would be assignment statements.

should be

if (!check)

and

if (check == true)

should be

if (check)

It's less error prone because if you accidentally type only one =, it is still valid syntax but wont do what you expect.

if (check = false)

will assign the value false to check and evaluate to false always.

And as an FYI

Botond Lakatos wrote:Any assignment statement in the form"isLeap==true" / "check==false"?

Those aren't assignment statements - they are conditional statements. With a single = they would be assignment statements.

Joanne

posted 4 years ago

Then you have an off-by-one error. Perhaps you should have used 6 or 8 instead of 7 somewhere. Or perhaps 0 instead of 1. Or perhaps < instead of <=. Or vice versa. Without knowing what the program does, though, it's impossible to tell without being given a specific example.

But I already suggested that it was nearly impossible to tell what the program did, and I already asked for a specific example. You didn't address either of those. Your program is still difficult to understand. Example: you have a variable named "mon". Perhaps this is something to do with months, or Mondays; a comment would have explained it. Instead it's just something mysterious.

Botond Lakatos wrote:It still doesnt handle the first week of the year correctly and the turning days for new weeks is still not Monday but Sunday

Then you have an off-by-one error. Perhaps you should have used 6 or 8 instead of 7 somewhere. Or perhaps 0 instead of 1. Or perhaps < instead of <=. Or vice versa. Without knowing what the program does, though, it's impossible to tell without being given a specific example.

But I already suggested that it was nearly impossible to tell what the program did, and I already asked for a specific example. You didn't address either of those. Your program is still difficult to understand. Example: you have a variable named "mon". Perhaps this is something to do with months, or Mondays; a comment would have explained it. Instead it's just something mysterious.

Joanne Neal

Rancher

Posts: 3742

16

posted 4 years ago

the program counts the number of weeks passed in the year up to the entered date. Returns the week count as an integer value.

The rules are:

- week 1 in any year is the week of Jan 1st

- weeks start on Mondays (not like the ISO defined method)

Examples from the 2011 calendar

January 1st (Saturday) -> returns 1 as per the rules

January 2nd (Sunday) -> returns 1 because it s a Sunday so still belongs to the first week, the week of Jan 1st (unfortunately my implementation returns 2 it counts Sunday as the first day of the new week)

January 3rd Monday -> returns 2, Monday is the start of the new week

January 20th Thursday ->returns 4 (there has been three Mondays till this date plus the first residual week of Jan1st)

the variable "mon" stands for monday, the loop counts weeks (days passed as mutiple of seven) and increments mon with 1 every time

The rules are:

- week 1 in any year is the week of Jan 1st

- weeks start on Mondays (not like the ISO defined method)

Examples from the 2011 calendar

January 1st (Saturday) -> returns 1 as per the rules

January 2nd (Sunday) -> returns 1 because it s a Sunday so still belongs to the first week, the week of Jan 1st (unfortunately my implementation returns 2 it counts Sunday as the first day of the new week)

January 3rd Monday -> returns 2, Monday is the start of the new week

January 20th Thursday ->returns 4 (there has been three Mondays till this date plus the first residual week of Jan1st)

the variable "mon" stands for monday, the loop counts weeks (days passed as mutiple of seven) and increments mon with 1 every time