Monday, October 18, 2010

Fun with Java-Part 1 (Calendar) Contd..



This is the main logic class.
It does not contain a main method.
Create a folder named calendarlogic and paste the class in that folder.


Download this Code click
__________________________________________________________________________________
//---------------------------------------CODE SNIPPET---------------------------------------------------

package calendarlogic;

/**
 *
 * @author Nayak
 */
public class Calendarlogic {

/*----------------------Author Arani Nayak----------------------------*/
/*---------------------Calender (Version3.1)--------------------------*/




 int a,year,diff,d,i=1,j,k,dm,l;
 long days;
 int st=1900,ch;
 int month;


        /// Creating a constructor named Calendarlogic, with intake parameters y, and m
        /// y will intake year as input and m the month to be displayed.. Both the value will
        /// be in integers...


        public Calendarlogic(int y, int m) {    
          
        this.year = y;
        this.month = m;

     if(year<1900 || year>5000)
     {

         System.out.println("Enter an year within 1900-5000 A.D.");

     }

     else{

          if(month>=1 && month <=12)
          {

        a=0;
                for(i=st;i
                  {
                   if((i%400==0) || (i%4==0 && i%100!=0))
                   a++;
                   }

   if(month==1 || month==3 ||month==5 || month==7 || month==8 || month==10 ||month==12)
   dm=31;
   else if(month==2)
   dm=28;
   else dm=30;
   if((month==2) && ((year%400==0) || (year%4==0 && year%100!=0)))
   dm=29;

  System.out.format("%20d",year);
  System.out.print(" ");
  if(month==1){System.out.print("Jan");  l=0;}
  if(month==2){System.out.print("Feb");  l=31;}
  if(month==3){System.out.print("Mar");  l=59;}
  if(month==4){System.out.print("Apr");  l=90;}
  if(month==5){System.out.print("May");  l=120;}
  if(month==6){System.out.print("Jun");  l=151;}
  if(month==7){System.out.print("Jul");  l=181;}
  if(month==8){System.out.print("Aug");  l=212;}
  if(month==9){System.out.print("Sep");  l=243;}
  if(month==10){System.out.print("Oct"); l=273;}
  if(month==11){System.out.print("Nov"); l=304;}
  if(month==12){System.out.print("Dec"); l=334;}

                System.out.println("");

 if((month>2) && ((year%400==0) || (year%4==0 && year%100!=0)))
   l++;

  diff = year-st;
  days = diff*365+a+l;


  if(days%7==0)
   d=0;
  else if(days%7==1)
   d=1;
  else if(days%7==2)
   d=2;
  else if(days%7==3)
   d=3;
  else if(days%7==4)
   d=4;
  else if(days%7==5)
   d=5;
  else
   d=6;

  System.out.println("  Mon  Tue  Wed  Thu  Fri  Sat  Sun");
        for(j=0;j<5*d;j++)
            System.out.print(" ");
   i=1;

  for(j=0;j<6;j++){
        for(k=d;k<7&&i<=dm;k++){
            System.out.format("%5d",i);
            i++; }
   d=0;
  System.out.println("");
    }
              
                    } //end of if(month checking)

          else{
                System.out.println("Value of month Entered should be within 1-12");
          }

                }// end of else


            } // end of constructor

}// end of class

_________________________________________________________________________________


Driver class to use the above Java class :
Download this Code click
__________________________________________________________________________________




import calendarlogic.Calendarlogic;
/**
 *
 * @author Nayak
 */
public class Main {


    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
       
        Calendarlogic cl = new Calendarlogic(2010,10);
    }


}


__________________________________________________________________________________


N.B: Feel free to copy the code.

No comments:

Post a Comment