Guest User

Untitled

a guest
Apr 26th, 2014
357
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.46 KB | None | 0 0
  1.   import java.util.Scanner;
  2.  
  3.     public class WeekdayCalculator
  4.     {
  5.         public static void main( String[] args )
  6.         {
  7.         Scanner keyboard = new Scanner(System.in);
  8.  
  9.         System.out.println("Welcome to Mr. Mitchell's fantastic birth-o-meter!");
  10.         System.out.println();
  11.         System.out.println("All you have to do is enter your birth date, and it will");
  12.         System.out.println("tell you the day of the week on which you were born.");
  13.         System.out.println();
  14.         System.out.println("Some automatic tests....");
  15.         System.out.println("12 10 2003 => " + weekday(12,10,2003));
  16.         System.out.println(" 2 13 1976 => " + weekday(2,13,1976));
  17.         System.out.println(" 2 13 1977 => " + weekday(2,13,1977));
  18.         System.out.println(" 7  2 1974 => " + weekday(7,2,1974));
  19.         System.out.println(" 1 15 2003 => " + weekday(1,15,2003));
  20.         System.out.println("10 13 2000 => " + weekday(10,13,2000));
  21.         System.out.println();
  22.  
  23.         System.out.println("Now it's your turn!  What's your birthday?");
  24.         System.out.print("Birth date (mm dd yyyy): ");
  25.         int mm = keyboard.nextInt();
  26.         int dd = keyboard.nextInt();
  27.         int yyyy = keyboard.nextInt();
  28.  
  29.         System.out.println("You were born on "+weekday(mm,dd,yyyy));
  30.     }
  31.  
  32.  
  33.     public static String weekday( int mm, int dd, int yyyy )
  34.     {
  35.         int yy, total, remainder;
  36.         String date = "";
  37.  
  38.         yy =  yyyy - 1900;
  39.  
  40.        
  41.         is_leap(yyyy);
  42.        
  43.             if (is_leap(yyyy)==true && mm==1 || mm==2)
  44.             total = yy/4 + yy + dd + month_offset(mm)-1;
  45.             else
  46.             total = yy/4 + yy + dd + month_offset(mm);
  47.            
  48.         remainder = total%7;
  49.         weekday_name(remainder);
  50.         date = weekday_name(remainder) +", " + month_name(mm) +" "+dd+", " + yyyy;
  51.         return date;
  52.     }
  53.  
  54.     public static int month_offset( int month )
  55.     {
  56.         int result;
  57.        
  58.         if (month==1)
  59.         result=1;
  60.         else if (month==2)
  61.         result=4;
  62.         else if (month==3)
  63.         result=4;
  64.         else if (month==4)
  65.         result=0;
  66.         else if (month==5)
  67.         result=2;
  68.         else if (month==6)
  69.         result=5;
  70.         else if (month==7)
  71.         result=0;
  72.         else if (month==8)
  73.         result=3;
  74.         else if (month==9)
  75.         result=6;
  76.         else if (month==10)
  77.         result=1;
  78.         else if (month==11)
  79.         result=4;
  80.         else if (month==12)
  81.         result=6;
  82.         else
  83.         result=-1;
  84.        
  85.         return result;
  86.     }
  87.    
  88.         public static String weekday_name( int day )
  89.     {
  90.         String result;
  91.        
  92.         if (day==1)
  93.         result="Monday";
  94.         else if (day==2)
  95.         result="Tuesday";
  96.         else if (day==3)
  97.         result="Wednesday";
  98.         else if (day==4)
  99.         result="Thursday";
  100.         else if (day==5)
  101.         result="Friday";
  102.         else if (day==6)
  103.         result="Saturday";
  104.         else if (day==0)
  105.         result="Sunday";
  106.         else
  107.         result="Error";
  108.        
  109.         return result;
  110.     }
  111.    
  112.         public static String month_name( int month )
  113.     {
  114.         String result;
  115.        
  116.         if (month==1)
  117.         result="January";
  118.         else if (month==2)
  119.         result="February";
  120.         else if (month==3)
  121.         result="March";
  122.         else if (month==4)
  123.         result="April";
  124.         else if (month==5)
  125.         result="May";
  126.         else if (month==6)
  127.         result="June";
  128.         else if (month==7)
  129.         result="July";
  130.         else if (month==8)
  131.         result="August";
  132.         else if (month==9)
  133.         result="September";
  134.         else if (month==10)
  135.         result="October";
  136.         else if (month==11)
  137.         result="November";
  138.         else if (month==12)
  139.         result="December";
  140.         else
  141.         result="Error";
  142.        
  143.         return result;
  144.     }
  145.    
  146.        
  147.     public static boolean is_leap( int year )
  148.     {
  149.    
  150.         boolean result;
  151.  
  152.         if ( year%400 == 0 )
  153.             result = true;
  154.         else if ( year%100 == 0 )
  155.             result = false;
  156.         else if ( year%4 == 0 )
  157.             result = true;
  158.         else
  159.             result = false;
  160.        
  161.         return result;
  162.     }
  163. }
Advertisement
Add Comment
Please, Sign In to add comment