Advertisement
Guest User

weekdaycalc

a guest
Feb 5th, 2014
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.72 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.         // put a function call for weekday() here
  30.         System.out.println("You were born on " + weekday(mm,dd,yyyy));
  31.     }
  32.  
  33.  
  34.     public static String weekday( int mm, int dd, int yyyy )
  35.     {
  36.         int yy, total;
  37.         String date = "";
  38.                 //find how many years past 1900
  39.                 yy = yyyy -1900;
  40.                 //divide that by four
  41.                 total = yy/4;
  42.                 //add the last two digits, couldn't think of a better way
  43.                 if (yyyy >1999)
  44.                     total = total +(yyyy-2000);
  45.                 if (yyyy < 2000)
  46.                     total = total + (yyyy-1900);
  47.                 //add day to the total
  48.                 total = total+dd;
  49.                 //add month offset to the total
  50.                 total = total+month_offset(mm);
  51.                 //check for leap year and january/february
  52.                 if(mm<3 && is_leap(yyyy) == true)
  53.                     total = total-1;
  54.                 //get the remainder
  55.                 total = total%7;
  56.                 //this is all okay, i think.
  57.                 date = weekday_name(total) + ", " + month_name(mm)+ " " + dd + ", " + yyyy;
  58.  
  59.         return date;
  60.     }
  61.  
  62.  
  63.     public static int month_offset( int month )
  64.     {
  65.         int result = 0;
  66.         if(month==1)
  67.                     result = 1;
  68.                 if(month==2)
  69.                     result = 4;
  70.                 if(month==3)
  71.                     result = 4;
  72.                 if(month==4)
  73.                     result = 0;
  74.                 if(month==5)
  75.                     result = 2;
  76.                 if(month==6)
  77.                     result = 5;
  78.                 if(month==7)
  79.                     result = 0;
  80.                 if(month==8)
  81.                     result = 3;
  82.                 if(month==9)
  83.                     result = 6;
  84.                 if(month==10)
  85.                     result = 1;
  86.                 if(month==11)
  87.                     result = 4;
  88.                 if(month==12)
  89.                     result = 6;
  90.                 if(month>12)
  91.                     result = -1;
  92.                 if(month<1)
  93.                     result = -1;
  94.        
  95.         return result;
  96.     }
  97.         public static String month_name( int month )
  98.     {
  99.         String result = null;
  100.         if(month == 1)
  101.                     result = "January";
  102.                 if(month == 2)
  103.                     result = "February";
  104.                 if(month == 3)
  105.                     result = "March";
  106.                 if(month == 4)
  107.                     result = "April";
  108.                 if(month == 5)
  109.                     result = "May";
  110.                 if(month == 6)
  111.                     result = "June";
  112.                 if(month == 7)
  113.                     result = "July";
  114.                 if(month == 8)
  115.                     result = "August";
  116.                 if(month == 9)
  117.                     result = "September";
  118.                 if(month == 10)
  119.                     result = "October";
  120.                 if(month == 11)
  121.                     result = "November";
  122.                 if(month == 12)
  123.                     result = "December";
  124.                 if(month > 12)
  125.                     result = "Error";      
  126.                
  127.         return result;
  128.     }
  129.         public static String weekday_name( int weekday )
  130.     {
  131.         String result = "";
  132.  
  133.         if ( weekday == 1 )
  134.         {
  135.             result = "Sunday";
  136.         }
  137.         else if ( weekday == 2 )
  138.         {
  139.             result = "Monday";
  140.         }
  141.                 else if ( weekday == 3 )
  142.                 {
  143.                         result = "Tuesday";
  144.                 }
  145.                 else if ( weekday == 4 )
  146.                 {
  147.                          result = "Wednesday" ;
  148.                 }
  149.                 else if ( weekday == 5 )
  150.                 {
  151.                          result = "Thursday";
  152.                 }
  153.                 else if ( weekday == 6 )
  154.                 {
  155.                         result = "Friday" ;
  156.                 }
  157.                 else if ( weekday == 7)
  158.                 {
  159.                          result = "Saturday" ;
  160.                 }
  161.                 else if ( weekday == 0 )
  162.                 {
  163.                         result = "Saturday" ;
  164.                 }
  165.                 else
  166.                 {
  167.                         result = "error";
  168.                 }
  169.                 return result;
  170.     }
  171.        
  172.     public static boolean is_leap( int year )
  173.     {
  174.         // years which are evenly divisible by 4 are leap years,
  175.         // but years divisible by 100 are not leap years,
  176.         // though years divisible by 400 are leap years
  177.         boolean result;
  178.  
  179.         if ( year%400 == 0 )
  180.             result = true;
  181.         else if ( year%100 == 0 )
  182.             result = false;
  183.         else if ( year%4 == 0 )
  184.             result = true;
  185.         else
  186.             result = false;
  187.        
  188.         return result;
  189.     }
  190. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement