Question: Hello, below is my program. I need to make it print a year calendar (So 12 months), when prompted for the year. I have the

Hello, below is my program. I need to make it print a year calendar (So 12 months), when prompted for the year. I have the program for the most part completed, but I need a couple more adjustments to make it run effectively. Can you please check over and edit...and also \\ at any mistakes or adjustments you made, so I can learn for next time. :) Thanks :)

import java.util.Scanner;

public class PrintMyCalendarYear {

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();

//ask method to display calendar for month of year

}

//method to display Header for Month

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

String [] MonthNames = {

"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"

};

System.out.println(" " + MonthNames + " " + 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 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++) //return day number calculated by using Zeller's

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

The reason for rejection suggests providing a more detailed explan... View full answer

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!