Assignment #96 and Weekday Calculator
Code
/// Name: Ali Kurland
/// Period: 6
/// Program Name: Weekday Calculator
/// File Name: WeekdayCalculator.java
/// Date Finished: 11/19/2015
import java.util.Scanner;
public class WeekdayCalculator
{
public static void main( String[] args )
{
Scanner keyboard = new Scanner(System.in);
System.out.println("Welcome to Ali's amazing Weekday Calculator!");
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("\nYou 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 ( isLeap(yyyy) && ( 0 < mm ) && ( mm <= 2 ) )
total = total - 1;
int wd = total % 7;
date = weekdayName(wd) + ", " + monthName(mm) + " " + dd + ", " + yyyy;
return date;
}
public static String monthName( int mm )
{
String outcome;
if ( mm == 1 )
outcome = "January";
else if ( mm == 2 )
outcome = "February";
else if ( mm == 3 )
outcome = "March";
else if ( mm == 4 )
outcome = "April";
else if ( mm == 5 )
outcome = "May";
else if ( mm == 6 )
outcome = "June";
else if ( mm == 7 )
outcome = "July";
else if ( mm == 8 )
outcome = "August";
else if ( mm == 9 )
outcome = "September";
else if ( mm == 10 )
outcome = "October";
else if ( mm == 11 )
outcome = "November";
else if ( mm == 12 )
outcome = "December";
else
outcome = "error";
return outcome;
}
public static String weekdayName( int wd )
{
String name;
if ( wd == 1 )
name = "Sunday";
else if ( wd == 2 )
name = "Monday";
else if ( wd == 3 )
name = "Tuesday";
else if ( wd == 4 )
name = "Wednesday";
else if ( wd == 5 )
name = "Thursday";
else if ( wd == 6 )
name = "Friday";
else if ( wd == 0 )
name = "Saturday";
else
name = "error";
return name;
}
public static int monthOffset( int mm )
{
int offset;
if ( mm == 1 )
offset = 1;
else if ( mm == 2 )
offset = 4;
else if ( mm == 3 )
offset = 4;
else if ( mm == 4 )
offset = 0;
else if ( mm == 5 )
offset = 2;
else if ( mm == 6 )
offset = 5;
else if ( mm == 7 )
offset = 0;
else if ( mm == 8 )
offset = 3;
else if ( mm == 9 )
offset = 6;
else if ( mm == 10 )
offset = 1;
else if ( mm == 11 )
offset = 4;
else if ( mm == 12 )
offset = 6;
else
offset = -1;
return offset;
}
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;
}
}
Picture of the output