Hello all, I would really appreciate some help. I have an assignment for my class that is due where the objective is for the program to recognize the holiday when a specific date comes around. The easy ones are holidays that fall on a specific date every year, examples such as Christmas and the 4th of July. The part I am stuck with are the holidays that are different every year, such as something like "The fourth Thursday of the month" aka Thanksgiving, in November. If anyone can help with that part I would greatly appreciate it, and here is the program he gave us as it is (incomplete):

/* Application program that produces a list of "known holidays" for a given calendar interval.
    The beginning date value and ending date value are either given as command line argument
    values or the user as prompted to enter them.  In either case, the dates must be given
    in the form mm/dd/yyyy.  Furthermore, the first date value MUST NOT represent a date later
    than the second date value.

*/
   import java.util.Scanner;
   public class Holidays {

      static Scanner input = new Scanner(System.in);
      public static void main(String[] args) {
        // Initialize the starting and ending date values
        ////////////////////////////////////////////////////////////
         SimpleDate start = new SimpleDate(getString(args,0).trim());
         SimpleDate stop  = new SimpleDate(getString(args,1).trim());
        ////////////////////////////////////////////////////////////
        // Loop to iterate over the interval
         while(!start.equals(stop)) {
            // Process the current date
            String result = holidaysOf(start);
            if(result.length() > 0) {   //Check for non-empty string result
               System.out.println(dayOfWeekAbbreviation(start.getDayOfWeek()) +
                                  " " + start + " " + result);
            }
            start.nextDay();        //Advance to the next date
         }
      }

    /* Functional Method that returns a string expressing the name (or names) of
        the holidays occuring on the given date.  If not holiday occurs on the 
        given date then the empty string as returned indicating this.
    */
      static String holidaysOf(SimpleDate date) {
         return asChristmas(date);
      }
    /* Functional Method that returns "Christmas" if the given date
        corresponds to the Christmas Holiday, or the empty string if
        it does not.
    */
      static String asChristmas(SimpleDate date) {
         String result = "";
         if((date.getMonth() == 12) && (date.getDay() == 25)) {
            result = "Christmas";
         }
         return result;
      }

    //Constants useful with the getDayOfWeek method
      static final int SUN = 1;
      static final int MON = 2;
      static final int TUE = 3;
      static final int WED = 4;
      static final int THU = 5;
      static final int FRI = 6;
      static final int SAT = 7;

    /* Functional Method that returns the abbreviation for the given "day of the week
        value";  where, 1 == "Sun", 2 == "Mon", etc.
    */
      static String dayOfWeekAbbreviation(int dow) {
         String result = "???";
         if(dow == 1) {
            result = "Sun";
         } 
         else if(dow == 2) {
            result = "Mon";
         } 
         else if(dow == 3) {
            result = "Tue";
         } 
         else if(dow == 4) {
            result = "Wed";
         } 
         else if(dow == 5) {
            result = "Thu";
         } 
         else if(dow == 6) {
            result = "Fri";
         } 
         else if(dow == 7) {
            result = "Sat";
         }
         return result;
      }

    /* Functional Method that as passed the command line arguments and the 
        index of the argument desired. If that argument exists then it as 
        returned as the value of this function; if not then the user as
        prompted to enter a string value, which as then returned as the result
        of this function.
    */
      static String getString(String[] commandLineArgs, int i) {
         String result;
         if (commandLineArgs.length > i) {
            result = commandLineArgs[i];
         }
         else {
            Scanner keyboard = new Scanner(System.in);
            System.out.print("Enter "+i+":");
            result = keyboard.nextLine();
         }
         return result; 
      }

   }

Does your SimpleDate class provides day of week method? Is it implemented from Calendar class?

Anyway, you could implement it from scratch as well and cooperate it with Calendar class which provides day of week. Then you should be able to find out using day of month and day of week.

Hi thanks for the reply. I am sorry, I don't really know what you are asking...I am still really a noob at this. But, what exactly should I do? Where do i look? You said the calendar class?

java by default provide calendar class in java.util.*;
use this class, it has so many defalut methods of day of week,day of year and so on.

Thanks I will check that out

Member Avatar for vishnu.khera.5

Where do i look? You said the calendar class?

You can start at: Here

There are a lot of helper class defined in Java API. The above link will help you start with java.util.Calendar class.

Thanks a bunch i will check that out too.

So like i said, i am not very good at this. I tried the calendar class, but confused myself. Does anyone have anything else to offer? Or does anyone know how to do what i am asking? Thanks again.

Alright, update. To those who instructed me to use the calendar class, I dont believe I am allowed to use it in my assignment. The only import he has is scanner, and nothing else. So I guess the question now is going about how to do this without the calendar class. Thanks.

Member Avatar for vishnu.khera.5

I'll try and give a few tips:

1st - You need to write a class SimpleDate. This class would take input from the console at the time you are running the java command - for e.g. your main() method is in class KnownHolidays and you have compiled your program to produce KnownHolidays.class file - you need to say C:>java KnownHolidays 08/22/2014 05/12/2015

You need an interactive app - this means you need to check if user has given correct i/p and ask for correct i/p if not.
Usually, you can use a simple loop - implemented in the main() method; also provide an option for the user to exit the program.

The SimpleDate class would implement trim() method and getString(<arrayname>, <arrayindex>) method |||| trim() = Returns a copy of the string, with leading and trailing whitespace omitted.
You have to implement the getstring method yourself.

The part I am stuck with are the holidays that are different every year, such as something like "The fourth Thursday of the month" aka Thanksgiving, in November.

2nd - Similar to the construct of 'static String asChristmas(SimpleDate date)' you need to implement methods for each public holiday.

OK, if you cannot use Calendar class, you would have to implement the day of week from scratch. It is not difficult and you could use brute-force algorithm to deal with it. You just need to know what's the day of week of the first day of the year. The first day of year could be hard-coded (such as starting from Januaray 1, 1970).

When dealing with day of week, you may have to design what value would be returned. You could give the starting day of week to Monday and ending on Sunday (some implementation starts on Sunday and ends on Saturday). Then you simply compute day of year by adding all days of each month. You should check for leap year before you add up the whole year days. Once you have the day of year and a known day of week of the first day of a year, you could apply modulo (%) to find out the day of week. Please note that you must watch out for 0 value from modulo if you want to keep value 1~7. The 0 is equal to 7.

/*
i.e.
Value set for starting week day is 1 (Monday) up to 7 (Sunday)
The day of week of January 1, 1970 is 4 (Thursday).

Given a year number, you should be able to find out the day of week of
the given year.
i.e. year 2012
Number of days in the year 1970 is 365 (not a leap year)
  The value of day of week of the first day of the year 1971
  (4 + 365) % 7 = 5
Number of days in the year 1971 is 365 (not a leap year)
  The value of day of week of the first day of the year 1972
  (5 + 365) % 7 = 6
Number of days in the year 1972 is 366 (leap year)
  The value of day of week of the first day of the year 1973
  (6 + 366) % 7 = 1
...
...
Number of days in the year 2011 is 366 (not a leap year)
  The value of day of week of the first day of the year 2012
  (6 + 365) % 7 = 0 => 7
*/

Thanks for the last 2 replies, I appreciate it a lot, but i had to submit my assingment already.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.