- /**
- * Assignment 2 P2.java Due July 11, 2012
- * login: cs11ubu
- **/
- /**
- * class P2
- * This program produces a calendar month to match user input of specified
- * month. It will then prompt for a a new input to display another month.
- * This will continue until the user ends the program.
- **/
- import java.util.*;
- public class P2
- {
- private static final int JAN = 1; // January
- public static void main( String args[] )
- {
- final int SUN = 1; // Sunday - 1st day in year
- final int DAYS_WK = 7; // 7 days in week
- char choice ; // Repeat loop
- int mon; // Month of year
- int days = 0; // Number of days in a month
- int i = 0;
- int j = 0;
- int k = 0;
- int total_days = 0;
- int offset = 0;
- Scanner scan = new Scanner(System.in); // Read input from keyboard
- String inputStr = null; // Input string
- System.out.print("Enter month in 2012 to display calendar (1-12): ");
- mon = scan.nextInt(); // Input calendar month
- prtMonthTitle(mon);
- for( i = 1 ; i <= mon ; i++ )
- {
- if(i == 1)
- {
- days = getNumDaysInMonth(i);
- offset = 0;
- }
- else
- {
- days = getNumDaysInMonth(i);
- total_days = total_days + days;
- offset = total_days%DAYS_WK;
- }
- }
- for ( j = 1; j <= offset ; j++ )
- {
- System.out.print(" ");
- }
- for ( k = 1; k <= days ; k++ )
- {
- if(k < 10)
- {
- System.out.print(" " + k + " ");
- }
- else
- {
- System.out.print(k + " ");
- }
- if((offset + k)%7 == 0)
- {
- System.out.print("\n");
- }
- }
- }
- public static void prtMonthTitle( int month )
- {
- switch ( month ) {
- case 1:
- System.out.print("*******************JANUARY*******************"
- +"\nSUN MON TUES WED THU FRI "
- +"SAT\n");
- break;
- case 2:
- System.out.print("*******************FEBRUARY******************"
- +"\nSUN MON TUES WED THU FRI "
- +"SAT\n");
- break;
- case 3:
- System.out.print("********************MARCH********************"
- +"\nSUN MON TUES WED THU FRI "
- +"SAT\n");
- break;
- case 4:
- System.out.print("********************APRIL********************"
- +"\nSUN MON TUES WED THU FRI "
- +"SAT\n");
- break;
- case 5:
- System.out.print("*********************MAY*********************"
- +"\nSUN MON TUES WED THU FRI "
- +"SAT\n");
- break;
- case 6:
- System.out.print("*********************JUNE********************"
- +"\nSUN MON TUES WED THU FRI "
- +"SAT\n");
- break;
- case 7:
- System.out.print("*********************JULY********************"
- +"\nSUN MON TUES WED THU FRI "
- +"SAT\n");
- break;
- case 8:
- System.out.print("********************AUGUST*******************"
- +"\nSUN MON TUES WED THU FRI "
- +"SAT\n");
- break;
- case 9:
- System.out.print("*****************SEPTEMBER*******************"
- +"\nSUN MON TUES WED THU FRI "
- +"SAT\n");
- break;
- case 10:
- System.out.print("*******************OCTOBER*******************"
- +"\nSUN MON TUES WED THU FRI "
- +"SAT\n");
- break;
- case 11:
- System.out.print("*******************NOVEMBER******************"
- +"\nSUN MON TUES WED THU FRI "
- +"SAT\n");
- break;
- case 12:
- System.out.print("*******************DECEMBER******************"
- +"\nSUN MON TUES WED THU FRI "
- +"SAT\n");
- break;
- }
- }
- public static int getNumDaysInMonth( int month )
- {
- int days_month = 0;
- switch ( month) {
- case 1:
- days_month = 31;
- break;
- case 2:
- days_month = 29;
- break;
- case 3:
- days_month = 31;
- break;
- case 4:
- days_month = 30;
- break;
- case 5:
- days_month = 31;
- break;
- case 6:
- days_month = 30;
- break;
- case 7:
- days_month = 31;
- break;
- case 8:
- days_month = 31;
- break;
- case 9:
- days_month = 30;
- break;
- case 10:
- days_month = 31;
- break;
- case 11:
- days_month = 30;
- break;
- case 12:
- days_month = 31;
- break;
- }
- return ( days_month );
- }
- }