*Here is the algorithm to determine what day of the week a given date is:*

Example dates: August 16, 1989 and March 20, 1950

Step 1: Only look at the last two digits of the year and determine how many 12s fit in it

7 12s in 89, 4 12s in 50

Step 2: Look at the remainder of this division:

89 – 7 * 12 = 5, 50 – 4 * 12 = 2

Step 3: How many 4s fit into that remainder:

1 four in 5 0 fours in 2

Step 4: Add the day of the month:

16 for the 16th 20 for the 20th

Step 5: Add the month code:

3 for August 4 for March

Jan = 1 Feb = 4 Mar = 4

Apr = 0 May = 2 Jun = 5

Jul = 0 Aug = 3 Sep = 6

Oct = 1 Nov = 4 Dec = 6

Step 6: Add your numbers, then mod by 7:

7 + 5 + 1 + 16 + 3 = 32 4 + 2 + 0 + 20 + 4 = 30

32 % 7 = 4 30 % 7 = 2

This is your day of the week, as follows:

Sat = 0 Sun = 1 Mon = 2 Tue = 3 Wed = 4 Thu = 5 Fri = 6

August 16, 1989(Wednesday) March 20, 1950(Monday)

NOTE: some dates require special offsets:

January and February dates in leap years: subtract 1 from step 5

Dates in the 1600s: add 6 to step 5

Dates in the 1700s: add 4 to step 5

Dates in the 1800s: add 2 to step 5

Dates in the 2000s: add 6 to step 5

Dates in the 2100s: add 4 to step 5

Example dates: August 16, 1989 and March 20, 1950

Step 1: Only look at the last two digits of the year and determine how many 12s fit in it

7 12s in 89, 4 12s in 50

Step 2: Look at the remainder of this division:

89 – 7 * 12 = 5, 50 – 4 * 12 = 2

Step 3: How many 4s fit into that remainder:

1 four in 5 0 fours in 2

Step 4: Add the day of the month:

16 for the 16th 20 for the 20th

Step 5: Add the month code:

3 for August 4 for March

Jan = 1 Feb = 4 Mar = 4

Apr = 0 May = 2 Jun = 5

Jul = 0 Aug = 3 Sep = 6

Oct = 1 Nov = 4 Dec = 6

Step 6: Add your numbers, then mod by 7:

7 + 5 + 1 + 16 + 3 = 32 4 + 2 + 0 + 20 + 4 = 30

32 % 7 = 4 30 % 7 = 2

This is your day of the week, as follows:

Sat = 0 Sun = 1 Mon = 2 Tue = 3 Wed = 4 Thu = 5 Fri = 6

August 16, 1989(Wednesday) March 20, 1950(Monday)

NOTE: some dates require special offsets:

January and February dates in leap years: subtract 1 from step 5

Dates in the 1600s: add 6 to step 5

Dates in the 1700s: add 4 to step 5

Dates in the 1800s: add 2 to step 5

Dates in the 2000s: add 6 to step 5

Dates in the 2100s: add 4 to step 5

So this is tricky and I'm wondering if someone can help me start this. Seems like it starts off using modulus. Maybe somehow using a code that looks at the last 2 digits of a year but I'm not sure. How should I start this?

What do you think you'd need to do with it to get the last two digits of the year? Remember that the last two digits are the year within a century, and a century is, ofcourse, 100 years.

The latter could even be written such that you pass in two values - the two digit year part (or really, any number), and a second number as the divisor. So you would then pass in something like 88 and 12, and it would return 7.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors

`int`to a String. I think the last two digits of the year should be an

`int`.

Those multiple

`if‑else`s look bad; can you put the numbers into an array? Similarly the days? Or better still put the days into an enumerated type.

*if(month < 1 || month > 12)*

{

return "null";

}

String[] name = {"January", "February", "March", "April", "May", "June", "July", "August", "September",

"October", "November", "December"};

return name[month-1];

{

return "null";

}

String[] name = {"January", "February", "March", "April", "May", "June", "July", "August", "September",

"October", "November", "December"};

return name[month-1];

Should I put in something like that in my algorithm? And I also must need one for my monthCode if statements. Then maybe I can solve this. Below is my new coding. Any suggestions?

`monthCode`:

But are the constants JANUARY, FEBRUARY, etc. set up somewhere?

All things are lawful, but not all things are profitable.

*Practice only makes habit, only perfect practice makes perfect.*—every music teacher ever

*Practice mindfully by doing the right things and doing things right.*— Junilu

[How to Ask Questions] [How to Answer Questions]

*NOTE: some dates require special offsets:*

January and February dates in leap years: subtract 1 from step 5

Dates in the 1600s: add 6 to step 5

Dates in the 1700s: add 4 to step 5

Dates in the 1800s: add 2 to step 5

Dates in the 2000s: add 6 to step 5

Dates in the 2100s: add 4 to step 5

January and February dates in leap years: subtract 1 from step 5

Dates in the 1600s: add 6 to step 5

Dates in the 1700s: add 4 to step 5

Dates in the 1800s: add 2 to step 5

Dates in the 2000s: add 6 to step 5

Dates in the 2100s: add 4 to step 5

*Practice only makes habit, only perfect practice makes perfect.*—every music teacher ever

*Practice mindfully by doing the right things and doing things right.*— Junilu

[How to Ask Questions] [How to Answer Questions]

*NOTE: some dates require special offsets:*

January and February dates in leap years: subtract 1 from step 5

Dates in the 1600s: add 6 to step 5

Dates in the 1700s: add 4 to step 5

Dates in the 1800s: add 2 to step 5

Dates in the 2000s: add 6 to step 5

Dates in the 2100s: add 4 to step 5

January and February dates in leap years: subtract 1 from step 5

Dates in the 1600s: add 6 to step 5

Dates in the 1700s: add 4 to step 5

Dates in the 1800s: add 2 to step 5

Dates in the 2000s: add 6 to step 5

Dates in the 2100s: add 4 to step 5

I guess I have to start from the beginning and get the first 2 digits for the centuries. Then just add two things. But then January and February is tricky. My code is below:

*are*leap years. Clear as mud?

*Practice only makes habit, only perfect practice makes perfect.*—every music teacher ever

*Practice mindfully by doing the right things and doing things right.*— Junilu

[How to Ask Questions] [How to Answer Questions]

you should immediately think "Lookup table" - you can use the value of

`mod`as in index into an array (your lookup table). That way, you can do this in one line to do the lookup and another line to declare the lookup table and its set of values. Do you see how the numbers you're comparing mod with can be array indices? So, based on the index, you'd put the appropriate value that you want to return in each element of the lookup array. It's really a quick-and-dirty hashtable.

*Practice only makes habit, only perfect practice makes perfect.*—every music teacher ever

*Practice mindfully by doing the right things and doing things right.*— Junilu

[How to Ask Questions] [How to Answer Questions]

*Practice only makes habit, only perfect practice makes perfect.*—every music teacher ever

*Practice mindfully by doing the right things and doing things right.*— Junilu

[How to Ask Questions] [How to Answer Questions]

*private static final String[] dayWeek = {"Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday"}*;

Was that what I was supposed to do? And is that what I should do with my other if than statements?

*Practice only makes habit, only perfect practice makes perfect.*—every music teacher ever

*Practice mindfully by doing the right things and doing things right.*— Junilu

[How to Ask Questions] [How to Answer Questions]

What about this dayWeek array that you declared, the one that you patterned after my example, what are you supposed to do with that? Your method is supposed to figure out what day of the week a given date is, right? So... that array has the strings "Saturday", "Sunday", .... "Friday" in it. The last value you calculate is this "mod" thing whose name, again, doesn't really say much. But it must be good for something, right, otherwise why would you calculate it? Do you see what I'm getting at?

*Practice only makes habit, only perfect practice makes perfect.*—every music teacher ever

*Practice mindfully by doing the right things and doing things right.*— Junilu

[How to Ask Questions] [How to Answer Questions]

NOTE: some dates require special offsets:

January and February dates in leap years: subtract 1 from step 5

Dates in the 1600s: add 6 to step 5

Dates in the 1700s: add 4 to step 5

Dates in the 1800s: add 2 to step 5

Dates in the 2000s: add 6 to step 5

Dates in the 2100s: add 4 to step 5

NOTE: some dates require special offsets:

January and February dates in leap years: subtract 1 from step 5

Dates in the 1600s: add 6 to step 5

Dates in the 1700s: add 4 to step 5

Dates in the 1800s: add 2 to step 5

Dates in the 2000s: add 6 to step 5

Dates in the 2100s: add 4 to step 5

About that first line: here is what I'm trying:

The error is: "method isLeapYear in class Date cannot be applied to given types; required: int; found: no arguments; reason: actual and formal arguments lists differ in length."

Here is my leap year method:

Can't seem to find the best way to implement this.

The error is: "method isLeapYear in class Date cannot be applied to given types; required: int; found: no arguments; reason: actual and formal arguments lists differ in length."

You need to learn how to interpret these error messages. It's not that hard.

"method isLeapYear in class Date" -- there's something with the call you're trying to make to isLeapYear

"cannot be applied to given types" -- you tried to call it with some kind of parameter but the method was expecting some other kind.

"required: int" - the method is expecting to be given an int value for its parameter

"found: no arguments" - you tried to call the method without any arguments

"reason: actual and formal argument lists differ in length" - the number of parameters you called it with (actual) and the number of parameters it was expecting (formal) are different.

*Practice only makes habit, only perfect practice makes perfect.*—every music teacher ever

*Practice mindfully by doing the right things and doing things right.*— Junilu

[How to Ask Questions] [How to Answer Questions]

For January and February dates in leap years.

Trying to work with the centuries. I created an int called centuryCode = 0;

Now when I try and compile, I get an unreachable statement error for line 1.

Harry Peters wrote:Here is my progress:

Trying to work with the centuries...

Question: Assuming you have an

`int`field called '

`year`' that contains a Gregorian year - eg, 1969 or 1753 - what is the value of '

`year / 100`'?

HIH

Winston

"Leadership is nature's way of removing morons from the productive flow" - Dogbert

Articles by Winston can be found here

It is sorta covered in the JavaRanch Style Guide. |