Assignemnt #96 Weekday
Code
/// Name: Caelan Dorstad
/// Period: 6
/// Program Name: WeekdayCalculator
/// File Name: WeekdayCalculator.java
/// Date Finished: 4/28/2016
import java.util.Scanner;
public class WeekdayCalculator
{
public static void main( String[] args )
{
Scanner keyboard = new Scanner(System.in);
System.out.println("Welcome to Mr. Davis's fantastic birth-o-meter!");
System.out.println();
System.out.println("All you have to do is enter your birth date, and it will");
System.out.println("tell you the day of the week on which you were born.");
System.out.println();
System.out.println("Some automatic tests....");
System.out.println("12 10 2003 => " + weekday(12,10,2003));
System.out.println(" 2 13 1976 => " + weekday(2,13,1976));
System.out.println(" 2 13 1977 => " + weekday(2,13,1977));
System.out.println(" 7 2 1974 => " + weekday(7,2,1974));
System.out.println(" 1 15 2003 => " + weekday(1,15,2003));
System.out.println("10 13 2000 => " + weekday(10,13,2000));
System.out.println();
System.out.println("Now it's your turn! What's your birthday?");
System.out.print("Birth date (mm dd yyyy): ");
int mm = keyboard.nextInt();
int dd = keyboard.nextInt();
int yyyy = keyboard.nextInt();
// put a method call for weekday() here
System.out.println("You were born on "+weekday(mm,dd,yyyy));
}
public static String weekday( int mm, int dd, int yyyy )
{
int yy, total;
String date = "";
yy = (yyyy - 1900);
total = (yy / 4) + yy + dd+ monthOffset(mm);
if ((mm==1||mm==2) && isLeap(yyyy)) total--;
// bunch of calculations go here
date = dayName(total%7)+", "+month_name(mm) +" "+dd+ ", " + yyyy;
return date;
}
static int[] monthoff = {1,4,4,0,2,5,0,3,6,1,4,6};
public static int monthOffset( int month )
{
if (month>0 && month<13)
return monthoff[month-1];
else return -1;
}
static String[] months = {"January","February","March","April","May","June","July","August","September","October","November","December"};
public static String month_name( int month )
{
if (month>0 && month < 13)
return months[month-1];
else return "D.N.E";
}
static String[] days = {"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"};
public static String dayName ( int day ){
if ( day >-1 && day < 7)
return days[day-1];
else return "N/A";
}
// paste your methods from MonthName, WeekdayName, and MonthOffset here
public static boolean isLeap( int year )
{
// years which are evenly divisible by 4 are leap years,
// but years divisible by 100 are not leap years,
// though years divisible by 400 are leap years
boolean result;
if ( year%400 == 0 )
result = true;
else if ( year%100 == 0 )
result = false;
else if ( year%4 == 0 )
result = true;
else
result = false;
return result;
}
}
Picture of the output