Assignemnt #96 Weekday Calculator
Code
///Name: Morgan Kaplan
///Period: 5
///Project Name: Weekday Calculator
///File Name: WeekdayCalculator.java
import java.util.Scanner;
public class WeekdayCalculator
{
public static void main( String[] args )
{
Scanner keyboard = new Scanner(System.in);
System.out.println("Welcome to Momo'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();
System.out.println("You were born on " + weekday(mm, dd, yyyy) + "." );
}
public static int month_offset( 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 yyyy )
{
boolean result;
if ( yyyy%400 == 0 )
result = true;
else if ( yyyy%100 == 0 )
result = false;
else if ( yyyy%4 == 0 )
result = true;
else
result = false;
return result;
}
public static String weekday( int mm, int dd, int yyyy )
{
int yy, total, day;
String date = "";
yy = yyyy - 1900;
total = yy/ 4 + yy + month_offset(mm) + dd;
if ( isLeap(yyyy) == true && mm == 1 )
total = total - 1;
else if ( isLeap(yyyy) == true && mm ==2 )
total = total -1 ;
day = total % 7 ;
date = weekday_name(yyyy,mm,dd) + ", " + mm + " " + dd + ", " + yyyy;
return date;
}
public static String weekday_name(int yyyy, int mm, int dd)
{
int yy, total, day;
String date = "";
yy = yyyy - 1900;
total = yy/ 4 + yy + month_offset(mm) + dd;
if ( isLeap(yyyy) == true && mm == 1 )
total = total - 1;
else if ( isLeap(yyyy) == true && mm ==2 )
total = total -1 ;
String Day;
day = total % 7;
if ( day == 1 )
Day = "Sunday";
else if ( day == 2 )
Day = "Monday";
else if ( day == 3 )
Day = "Tuesday";
else if ( day == 4 )
Day = "Wednesday";
else if ( day == 5 )
Day = "Thursday";
else if ( day == 6 )
Day = "Friday";
else if ( day == 7 )
Day = "Saturday";
else
Day = "error";
return Day;
}
Picture of Output