Assignemnt #96: Weekday Calculator
Code
/// Name: Kelsey Lieberman
/// Period 5
/// Program Name: WeekdayCalculator
/// File Name: WeekdayCalculator.java
/// Date Finished: 1/15/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 Kdawgg'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();
// put a function call for weekday() here
System.out.println("You were born on " + weekday(mm, dd, yyyy));
}
public static String weekday( int mm, int dd, int yyyy )
{
int yy, total, wkdy;
String date = "";
yy = (yyyy - 1900);
total = ((yy/4) + yy + dd + monthOffset(mm));
if ((MonthName(mm) == "January" || MonthName(mm) == "February") && isLeap(yyyy) == true)
total = total -1;
wkdy = total%7;
date = WeekdayName(wkdy) + ", " + MonthName(mm) + " " + dd + ", " + yyyy;
return date;
}
// paste your functions from MonthName, WeekdayName, and MonthOffset here
public static String WeekdayName(int v)
{
String weekday;
if (v == 1)
weekday = "Sunday";
else if (v == 2)
weekday = "Monday";
else if (v == 3)
weekday = "Tuesday";
else if (v == 4)
weekday = "Wednesday";
else if (v == 5)
weekday = "Thursday";
else if (v == 6)
weekday = "Friday";
else if (v == 7)
weekday = "Saturday";
else
weekday = "Error";
return weekday;
}
public static String MonthName(int x)
{
String z;
if (x == 1)
z = "January";
else if (x == 2)
z = "February";
else if (x == 3)
z = "March";
else if (x == 4)
z = "April";
else if (x == 5)
z = "May";
else if (x == 6)
z = "June";
else if (x == 7)
z = "July";
else if (x == 8)
z = "August";
else if (x == 9)
z = "September";
else if (x == 10)
z = "October";
else if (x == 11)
z = "November";
else if (x == 12)
z = "December";
else
z = "Error";
return z;
}
public static int monthOffset( int month )
{
int result;
if (month == 1)
result = 1;
else if (month == 2)
result = 4;
else if (month == 3)
result = 4;
else if (month == 4)
result = 0;
else if (month == 5)
result = 2;
else if (month == 6)
result = 5;
else if (month == 7)
result = 0;
else if (month == 8)
result = 3;
else if (month == 9)
result = 6;
else if (month == 10)
result = 1;
else if (month == 11)
result = 4;
else if (month == 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;
}
}
Picture of the output