Question: How do I modify this program into a program into a program that only has me input the year and prints Months 1-12 for that

How do I modify this program into a program into a program that only has me input the year and prints Months 1-12 for that year?

import java.util.Scanner;

public class PrintCalendar {

public static void main ( String args []) {

Scanner input = new Scanner(System.in);

//print the year

System.out.println("Input the Year (Ex: 2017): ");

int y = input.nextInt();

//print the month

System.out.println("Input the Month (1-12): ");

int m = input.nextInt();

//ask method to display calendar for month of year

printMonth(y, m);

}

//method to display Header for Month

public static void printMonthHeader (int y, int m) {

System.out.println(" " + getMonthName (m) + " " + y);

System.out.println("_____________________________");

System.out.println(" Sun Mon Tue Wed Thu Fri Sat");

}

//method to display calendar for a month in a particular year

static void printMonth (int y, int m) {

printMonthHeader(y, m);

printMonthBody (y,m);

}

static String getMonthName (int m)

{

String monthName = null;

switch (m) {

case 1: monthName = "January";

break;

case 2: monthName = "February";

break;

case 3: monthName = "March";

break;

case 4: monthName = "April";

break;

case 5: monthName = "May";

break;

case 6: monthName = "June";

break;

case 7: monthName = "July";

break;

case 8: monthName = "August";

break;

case 9: monthName = "September";

break;

case 10: monthName = "October";

break;

case 11: monthName = "November";

break;

case 12: monthName = "December";

break;

}

return monthName;

}

static void printMonthBody(int y, int m) {

int dayNumb = getStartDay(y, m);

int numberOfDaysInMonth = getNumberOfDaysInMonth(y,m);

int i = 0;

for (i = 0; i <(dayNumb); i++)

System.out.print(" ");

for (i = 1; i <= numberOfDaysInMonth; i++) {

if (i<10)

System.out.print(" " + i);

else System.out.print (" " + i);

if ((i + dayNumb) % 7 == 0)

System.out.println();

}

System.out.println();

}

static int getStartDay (int y, int m) {

return getDayOfWeek (y, m, 1);

}

public static int getDayOfWeek( int y, int m, int d )

{

// Adjust month number & year to fit Zeller's numbering system

if (m < 3)

{

m = m + 12;

y = y - 1;

}

int k = y % 100; // Calculate year within century

int j = y / 100; // Calculate century term

int h = 0; // Day number of first day in month 'm'

h = ( d + ( 13 * ( m + 1 ) / 5 ) + k + ( k / 4 ) + ( j / 4 ) +

( 5 * j ) ) % 7;

// Convert Zeller's value to ISO value (1 = Mon, ... , 7 = Sun )

int dayNum = ( ( h + 5 ) % 7 ) + 1;

return dayNum;

}

public static int getNumberOfDaysInMonth (int y, int m)

{

if (m == 1 || m == 3 || m == 5 || m ==7 || m == 8 || m == 10 || m == 12)

return 31;

if (m == 4 || m == 6 || m == 9 || m == 11)

return 30;

if (m == 2) return isLeapYear (y) ?29:28;

return 0;

}

static boolean isLeapYear (int y)

{

return y % 400 == 0 || (y % 4 ==0 && y % 100 != 0);

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!