Assignment #96 Weekday Calculator
Code
/// Name: Koosha Kimelman
/// Period: 7
/// Program Name: Weekday Calculator
/// File Name: WeekdayCalculator.java
/// Date Completed: 3/17/16
import java.util.Scanner;
public class WeekdayCalculator {
public static void main( String[] args ) {
Scanner keyboard = new Scanner(System.in);
System.out.println("Find out what day of the week you were born.");
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_name(weekday(12,10,2003)) + ", December 10, 2003.");
System.out.println(" 2 13 1976 => " + weekday_name(weekday(2,13,1976)) + ", February 13, 1976.");
System.out.println(" 2 13 1977 => " + weekday_name(weekday(2,13,1977)) + ", February 13, 1977.");
System.out.println(" 7 2 1974 => " + weekday_name(weekday(7,2,1974)) + ", July 2, 1974.");
System.out.println(" 1 15 2003 => " + weekday_name(weekday(1,15,2003)) + ", January 15, 2003.");
System.out.println("10 13 2000 => " + weekday_name(weekday(10,13,2000)) + ", October 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_name(weekday(mm, dd, yyyy)) + ", " + month_name(mm) + " " + dd + ", " + yyyy + ".");
}
public static int weekday( int mm, int dd, int yyyy ) {
int yy, total;
String date = "";
yy = yyyy - 1900;
total = yy / 4;
total = total + yy;
total = total + dd;
total = total + month_offset(mm);
if (isLeap(yyyy) == true && (mm == 1 || mm == 2))
total--;
total = total % 7;
return total;
}
public static String weekday_name( int weekday ) {
String result = "";
if ( weekday == 1 )
{
result = "Sunday";
}
else if ( weekday == 2 )
{
result = "Monday";
}
else if ( weekday == 3 )
{
result = "Tuesday";
}
else if ( weekday == 4 )
{
result = "Wednesday";
}
else if ( weekday == 5 )
{
result = "Thursday";
}
else if ( weekday == 6 )
{
result = "Friday";
}
else if ( weekday == 7 )
{
result = "Saturday";
}
else if ( weekday == 0 )
{
result = "Saturday";
}
else
{
result = "error";
}
return result;
}
public static String month_name(int n) {
String monthName;
if (n == 1)
monthName = "January";
else if (n == 2)
monthName = "February";
else if (n == 3)
monthName = "March";
else if (n == 4)
monthName = "April";
else if (n == 5)
monthName = "May";
else if (n == 6)
monthName = "June";
else if (n == 7)
monthName = "July";
else if (n == 8)
monthName = "August";
else if (n == 9)
monthName = "September";
else if (n == 10)
monthName = "October";
else if (n == 11)
monthName = "November";
else if (n == 12)
monthName = "December";
else
monthName = "error";
return monthName;
}
public static int month_offset(int n) {
int monthOffset;
if (n == 1)
monthOffset = 1;
else if (n == 2)
monthOffset = 4;
else if (n == 3)
monthOffset = 4;
else if (n == 4)
monthOffset = 0;
else if (n == 5)
monthOffset = 2;
else if (n == 6)
monthOffset = 5;
else if (n == 7)
monthOffset = 0;
else if (n == 8)
monthOffset = 3;
else if (n == 9)
monthOffset = 6;
else if (n == 10)
monthOffset = 1;
else if (n == 11)
monthOffset = 4;
else if (n == 12)
monthOffset = 6;
else
monthOffset = -1;
return monthOffset;
}
public static boolean isLeap( int year ) {
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