Assignemnt #96 and Weekday Calculator
Code
/// Name: Kyle Ivy
/// Period: 5
/// Program Name: WeekdayCalculator
/// File Name: WeekdayCalculator.java
/// Date Finished: 4/20/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 birthday, and it will tell you the day of the week on which you were born. " );
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( "Birthday (mm dd yyyy): " );
int mm = keyboard.nextInt();
int dd = keyboard.nextInt();
int yyyy = keyboard.nextInt();
System.out.println( "You were born on " + weekday( mm, dd, yyyy ) + "!" );
}
public static String weekday( int mm, int dd, int yyyy )
{
int yy = yyyy - 1900;
int total, leftover;
String date = "";
total = (yy/4) + yy + dd + monthOffset(mm);
if ( isLeap(yyyy) == true && ( mm == 1 || mm == 2 ) )
total = total - 1;
leftover = total%7 ;
leftover = leftover - 1;
date = WeekdayName(leftover) + ", " + month_name(mm) + ", " + yyyy;
return date;
}
public static int monthOffset( int mm )
{
int result;
if (mm == 1) result = 1;
else if (mm == 2) result = 4;
else if (mm == 3) result = 4;
else if (mm == 4) result = 0;
else if (mm == 5) result = 2;
else if (mm == 6) result = 5;
else if (mm == 7) result = 0;
else if (mm == 8) result = 3;
else if (mm == 9) result = 6;
else if (mm == 10) result = 1;
else if (mm == 11) result = 4;
else if (mm == 12) result = 6;
else result = -1;
return result;
}
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;
}
public static String WeekdayName( int remain )
{
String result;
if (remain == 0)
result = "Sunday";
else if (remain == 1)
result = "Monday";
else if (remain == 2)
result = "Tuesday";
else if (remain == 3)
result = "Wednesday";
else if (remain == 4)
result = "Thursday";
else if (remain == 5)
result = "Friday";
else if (remain == 6)
result = "Saturday";
else
result = "ERROR";
return result;
}
public static String month_name( int mm )
{
String result;
if (mm == 1)
result = "January";
else if (mm == 2)
result = "February";
else if (mm == 3)
result = "March";
else if (mm == 4)
result = "April";
else if (mm == 5)
result = "May";
else if (mm == 6)
result = "June";
else if (mm == 7)
result = "July";
else if (mm == 8)
result = "August";
else if (mm == 9)
result = "September";
else if (mm == 10)
result = "October";
else if (mm == 11)
result = "November";
else if (mm == 12)
result = "December";
else
result = "ERROR";
return result;
}
}
prog96.PNG