Assignment #96: Weekday Calculator
Code
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();
weekday(mm, dd, yyyy);
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 = ((yyyy / 4) + mm + month_offset(mm));
isLeap(yyyy);
if ((isLeap(yyyy)) && ((mm == 1 )||( mm == 2))) {
total = (total - 1);
}
weekday_name(total % 7);
date = weekday_name(total) + ", " + month_name(mm) + " " + dd + ", " + yyyy;
return date;
}
// paste your methods from MonthName, WeekdayName, and MonthOffset here
public static String month_name( int mm )
{
String resultmonth = "month";
if (mm == 1) {
resultmonth = "January";
}
if (mm == 2) {
resultmonth = "February";
}
if (mm == 3) {
resultmonth = "March";
}
if (mm == 4) {
resultmonth = "April";
}
if (mm == 5) {
resultmonth = "May";
}
if (mm == 6) {
resultmonth = "June";
}
if (mm == 7) {
resultmonth = "July";
}
if (mm == 8) {
resultmonth = "August";
}
if (mm == 9) {
resultmonth = "September";
}
if (mm == 10) {
resultmonth = "October";
}
if (mm == 11) {
resultmonth = "November";
}
if (mm == 12) {
resultmonth = "December";
}
else if (mm > 12) {
resultmonth = "error";
}
return resultmonth;
}
public static String weekday_name( int total ) {
String resultday = "day";
if (total % 7 == 0) {
resultday = "Sunday";
}
if (total % 7 == 1) {
resultday = "Monday";
}
if (total % 7 == 2) {
resultday = "Tuesday";
}
if (total % 7 == 3) {
resultday = "Wednesday";
}
if (total % 7 == 4) {
resultday = "Thursday";
}
if (total % 7 == 5) {
resultday = "Friday";
}
if (total % 7 == 6) {
resultday = "Saturday";
}
return resultday;
}
public static int month_offset( int mm ) {
int resultoff = 0;
if (mm == 1) {
resultoff = 1;
}
if (mm == 2) {
resultoff = 4;
}
if (mm == 3) {
resultoff = 4;
}
if (mm == 4) {
resultoff = 0;
}
if (mm == 5) {
resultoff = 2;
}
if (mm == 6) {
resultoff = 5;
}
if (mm == 7) {
resultoff = 0;
}
if (mm == 8) {
resultoff = 3;
}
if (mm == 9) {
resultoff = 6;
}
if (mm == 10) {
resultoff = 1;
}
if (mm == 11) {
resultoff = 4;
}
if (mm == 12) {
resultoff = 6;
}
else if (mm > 12) {
resultoff = -1;
}
return resultoff;
}
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 resultleap;
if ( year%400 == 0 )
resultleap = true;
else if ( year%100 == 0 )
resultleap = false;
else if ( year%4 == 0 )
resultleap = true;
else
resultleap = false;
return resultleap;
}
}
Picture of the output