SlideShare a Scribd company logo
1 of 12
Download to read offline
Write a program to generate the entire calendar for one year. The program must get two values
from the user: (1) the year and (2) the day of the week for January 1st of that year. The year,
which should be positive, is needed to check for and handle leap years1. The day of the week for
January 1st is needed so that you know where to start the calendar. The user should enter 0 for
Sunday, 1 for Monday, ... or 6 for Saturday. As always, you need to validate the user's input. To
actually print the calendar, you must use a single method that prints out the calendar for one
month and then call this function 12 times from main(), once for each month in the year. To
check for a leap year you will need to write another method that takes the year as a parameter
and returns true if it’s a leap year, or false otherwise. Stubs (i.e. method signatures without any
code) for both of these methods have been provided for you.
The calendar should be printed in the form below. In this example, January starts on a Saturday
(day 6). Note that February starts on a Tuesday in this example because January ended on a
Monday.
January
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 2829
30 31
February
1 2 3 4 5
6 7 8 9 10 11 12 ...
The format he gave us is as follows:
public class PA6a {
/**
* Error to output if year is not positive
*/
static final String E_YEAR = "The year must be positive!";
/**
* Error to output if the day is not between 0 and 6
*/
static final String E_DAY = "The day of January 1st must be between 0 and 6!";
/**
* Determines if an input is a leap year
*
* @param year year in question
* @ereturn true if a leap year
*/
public static boolean isLeapYear(int year) {
return false; // TODO: replace with your code
}
/**
* Outputs a month to the console
*
* @param month title
* @param startDay 0=Sunday ... 6=Saturday
* @param numDays number of days in the month
* @return day of the week of the last day of the month
*/
public static int printMonth(String month, int startDay, int numDays) {
return 0; // TODO: replace with your code
}
/**
* Program execution point:
* input year, day of the week (0-6) of january 1
* output calendar for that year
*
* @param args command-line arguments (ignored)
*/
public static void main(String[] args) {
// TODO: write your code here
}
I am confused especially with the print month method, help is greatly appreciated.
Solution
package org.students;
import java.util.Scanner;
import com.sun.org.apache.bcel.internal.generic.IUSHR;
public class Calander {
//declaring static variables
static final String E_YEAR = "The year must be positive!";
static final String E_DAY = "The day of January 1st must be between 0 and 6!";
public static void main(String[] args) {
//Declaring variables
int getFirstDay;
int year;
Scanner scanner = new Scanner(System.in);
//This while loop continue to execute until user enters valid year
while (true) {
//Getting the year entered by the user
System.out.print("Enter the year: ");
year = scanner.nextInt();
if (year < 0) {
System.out.println(E_YEAR);
} else
break;
}
//This loop continue to execute until user enters valid first day of the month
while (true) {
//Getting the first day entered by the user
System.out.print("Enter 1st day of year ( 0 = Sunday, 6 = Satuday ): ");
getFirstDay = scanner.nextInt();
//Checking whether the first day is within valid range or not
if (getFirstDay < 0 || getFirstDay > 6) {
System.out.println(E_DAY);
continue;
} else
break;
}
for (int month = 1; month <= 12; month++) {
int days = 0;
String monthName = " ";
switch (month) {
case 1:
monthName = "January";
days = 31;
break;
case 2:
monthName = "February";
if (isLeapYear(year)) {
days = 29;
} else {
days = 28;
}
break;
case 3:
monthName = "March";
days = 31;
break;
case 4:
monthName = "April";
days = 30;
break;
case 5:
monthName = "May";
days = 31;
break;
case 6:
monthName = "June";
days = 30;
break;
case 7:
monthName = "July";
days = 31;
break;
case 8:
monthName = "August";
days = 31;
break;
case 9:
monthName = "September";
days = 30;
break;
case 10:
monthName = "October";
days = 31;
break;
case 11:
monthName = "November";
days = 30;
break;
case 12:
monthName = "December";
days = 31;
break;
default:
System.out.print("Invalid Month.");
System.exit(0);
break;
}
System.out.println(" " + monthName + " " + year);
int i = 0;
int firstDay = 0;
switch (month) {
case 1:
firstDay = getFirstDay;
break;
case 2:
firstDay = getFirstDay + 3;
break;
case 3:
firstDay = getFirstDay + 3;
break;
case 4:
firstDay = getFirstDay + 6;
break;
case 5:
firstDay = getFirstDay + 8;
break;
case 6:
firstDay = getFirstDay + 11;
break;
case 7:
firstDay = getFirstDay + 13;
break;
case 8:
firstDay = getFirstDay + 16;
break;
case 9:
firstDay = getFirstDay + 19;
break;
case 10:
firstDay = getFirstDay + 21;
break;
case 11:
firstDay = getFirstDay + 24;
break;
case 12:
firstDay = getFirstDay + 26;
break;
}
if (isLeapYear(year)) {
switch (month) {
case 1:
firstDay = getFirstDay;
break;
case 2:
firstDay = getFirstDay + 3;
break;
case 3:
firstDay = getFirstDay + 4;
break;
case 4:
firstDay = getFirstDay + 7;
break;
case 5:
firstDay = getFirstDay + 9;
break;
case 6:
firstDay = getFirstDay + 12;
break;
case 7:
firstDay = getFirstDay + 14;
break;
case 8:
firstDay = getFirstDay + 17;
break;
case 9:
firstDay = getFirstDay + 20;
break;
case 10:
firstDay = getFirstDay + 22;
break;
case 11:
firstDay = getFirstDay + 25;
break;
case 12:
firstDay = getFirstDay + 27;
break;
}
}
printMonth(month, firstDay, days);
System.out.println();
}
}
//This method checks whether the year is leap year or not
private static boolean isLeapYear(int year) {
boolean bool = false;
if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))
bool = true;
else
bool = false;
return bool;
}
//This method will display the month
public static void printMonth(int month, int startDay, int numDays) {
int dayOfWeek = 0;
System.out.println("_______________________________");
System.out.println(" Sun Mon Tue Wed Thu Fri Sat");
if ((startDay % 7) >= 0) {
if ((startDay % 7) == 0) {
dayOfWeek = 0;
} else if ((startDay % 7) == 1) {
dayOfWeek = 1;
System.out.print(" ");
} else if ((startDay % 7) == 2) {
dayOfWeek = 2;
System.out.print("t ");
} else if ((startDay % 7) == 3) {
dayOfWeek = 3;
System.out.print("tt ");
} else if ((startDay % 7) == 4) {
dayOfWeek = 4;
System.out.print("ttt");
} else if ((startDay % 7) == 5) {
dayOfWeek = 5;
System.out.print("ttt ");
} else if ((startDay % 7) == 6) {
dayOfWeek = 6;
System.out.print("tttt ");
}
}
for (int i = 1; i <= numDays; i++) {
if (i < 10)
System.out.print(" " + i);
else
System.out.print(" " + i);
if ((i + startDay) % 7 == 0)
System.out.println();
}
}
}
____________________________________
Output:
Enter the year: 2016
Enter 1st day of year ( 0 = Sunday, 6 = Satuday ): 5
January 2016
_______________________________
Sun Mon Tue Wed Thu Fri Sat
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
February 2016
_______________________________
Sun Mon Tue Wed Thu Fri Sat
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29
March 2016
_______________________________
Sun Mon Tue Wed Thu Fri Sat
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
April 2016
_______________________________
Sun Mon Tue Wed Thu Fri Sat
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
May 2016
_______________________________
Sun Mon Tue Wed Thu Fri Sat
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
June 2016
_______________________________
Sun Mon Tue Wed Thu Fri Sat
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30
July 2016
_______________________________
Sun Mon Tue Wed Thu Fri Sat
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
August 2016
_______________________________
Sun Mon Tue Wed Thu Fri Sat
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
September 2016
_______________________________
Sun Mon Tue Wed Thu Fri Sat
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30
October 2016
_______________________________
Sun Mon Tue Wed Thu Fri Sat
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31
November 2016
_______________________________
Sun Mon Tue Wed Thu Fri Sat
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30
December 2016
_______________________________
Sun Mon Tue Wed Thu Fri Sat
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
____________________________

More Related Content

Similar to Write a program to generate the entire calendar for one year. The pr.pdf

I Have the following Java program in which converts Date to Words an.pdf
I Have the following Java program in which converts Date to Words an.pdfI Have the following Java program in which converts Date to Words an.pdf
I Have the following Java program in which converts Date to Words an.pdf
allystraders
 
CounterTest.javaimport static org.junit.Assert.;import org.jun.pdf
CounterTest.javaimport static org.junit.Assert.;import org.jun.pdfCounterTest.javaimport static org.junit.Assert.;import org.jun.pdf
CounterTest.javaimport static org.junit.Assert.;import org.jun.pdf
deepua8
 
Change to oop formatimport java.util.Scanner;import java.io.;.pdf
Change to oop formatimport java.util.Scanner;import java.io.;.pdfChange to oop formatimport java.util.Scanner;import java.io.;.pdf
Change to oop formatimport java.util.Scanner;import java.io.;.pdf
MAYANKBANSAL1981
 
Please help me find my errors. I am lost as I know what the problem.pdf
Please help me find my errors. I am lost as I know what the problem.pdfPlease help me find my errors. I am lost as I know what the problem.pdf
Please help me find my errors. I am lost as I know what the problem.pdf
fathimahardwareelect
 
Date and Timestamp Types In Snowflake (By Faysal Shaarani)
Date and Timestamp Types In Snowflake (By Faysal Shaarani)Date and Timestamp Types In Snowflake (By Faysal Shaarani)
Date and Timestamp Types In Snowflake (By Faysal Shaarani)
Faysal Shaarani (MBA)
 
Assignment 5 (200 Points – Due Wed 3252015 – Individual As.docx
Assignment 5 (200 Points – Due Wed 3252015 – Individual As.docxAssignment 5 (200 Points – Due Wed 3252015 – Individual As.docx
Assignment 5 (200 Points – Due Wed 3252015 – Individual As.docx
ssuser562afc1
 

Similar to Write a program to generate the entire calendar for one year. The pr.pdf (15)

02
0202
02
 
Lo17
Lo17Lo17
Lo17
 
P2
P2P2
P2
 
I Have the following Java program in which converts Date to Words an.pdf
I Have the following Java program in which converts Date to Words an.pdfI Have the following Java program in which converts Date to Words an.pdf
I Have the following Java program in which converts Date to Words an.pdf
 
CounterTest.javaimport static org.junit.Assert.;import org.jun.pdf
CounterTest.javaimport static org.junit.Assert.;import org.jun.pdfCounterTest.javaimport static org.junit.Assert.;import org.jun.pdf
CounterTest.javaimport static org.junit.Assert.;import org.jun.pdf
 
Change to oop formatimport java.util.Scanner;import java.io.;.pdf
Change to oop formatimport java.util.Scanner;import java.io.;.pdfChange to oop formatimport java.util.Scanner;import java.io.;.pdf
Change to oop formatimport java.util.Scanner;import java.io.;.pdf
 
Steve mo's formulas and life hacks wellington nz 2020-05-05
Steve mo's formulas and life hacks   wellington nz 2020-05-05Steve mo's formulas and life hacks   wellington nz 2020-05-05
Steve mo's formulas and life hacks wellington nz 2020-05-05
 
Steve mo's formulas and life hacks frankfurt de 2020-05-07
Steve mo's formulas and life hacks   frankfurt de 2020-05-07Steve mo's formulas and life hacks   frankfurt de 2020-05-07
Steve mo's formulas and life hacks frankfurt de 2020-05-07
 
8 arrays and pointers
8  arrays and pointers8  arrays and pointers
8 arrays and pointers
 
Discovery and Application of Voyager's New Complex Publication Patterns
Discovery and Application of Voyager's New Complex Publication PatternsDiscovery and Application of Voyager's New Complex Publication Patterns
Discovery and Application of Voyager's New Complex Publication Patterns
 
Please help me find my errors. I am lost as I know what the problem.pdf
Please help me find my errors. I am lost as I know what the problem.pdfPlease help me find my errors. I am lost as I know what the problem.pdf
Please help me find my errors. I am lost as I know what the problem.pdf
 
Python and PostgreSQL: Let's Work Together! | PyConFr 2018 | Dimitri Fontaine
Python and PostgreSQL: Let's Work Together! | PyConFr 2018 | Dimitri FontainePython and PostgreSQL: Let's Work Together! | PyConFr 2018 | Dimitri Fontaine
Python and PostgreSQL: Let's Work Together! | PyConFr 2018 | Dimitri Fontaine
 
Excel DATEDIFF Function
Excel DATEDIFF FunctionExcel DATEDIFF Function
Excel DATEDIFF Function
 
Date and Timestamp Types In Snowflake (By Faysal Shaarani)
Date and Timestamp Types In Snowflake (By Faysal Shaarani)Date and Timestamp Types In Snowflake (By Faysal Shaarani)
Date and Timestamp Types In Snowflake (By Faysal Shaarani)
 
Assignment 5 (200 Points – Due Wed 3252015 – Individual As.docx
Assignment 5 (200 Points – Due Wed 3252015 – Individual As.docxAssignment 5 (200 Points – Due Wed 3252015 – Individual As.docx
Assignment 5 (200 Points – Due Wed 3252015 – Individual As.docx
 

More from arihantmobileselepun

A geneticist is working with a new bacteriophage called phage Y3 that.pdf
A geneticist is working with a new bacteriophage called phage Y3 that.pdfA geneticist is working with a new bacteriophage called phage Y3 that.pdf
A geneticist is working with a new bacteriophage called phage Y3 that.pdf
arihantmobileselepun
 
Create a java project that - Draw a circle with three random init.pdf
Create a java project that - Draw a circle with three random init.pdfCreate a java project that - Draw a circle with three random init.pdf
Create a java project that - Draw a circle with three random init.pdf
arihantmobileselepun
 
aphase The spindle contracts and the sister chromatids are separated.pdf
aphase The spindle contracts and the sister chromatids are separated.pdfaphase The spindle contracts and the sister chromatids are separated.pdf
aphase The spindle contracts and the sister chromatids are separated.pdf
arihantmobileselepun
 
Who is it important that the lymphatic system operate separately f.pdf
Who is it important that the lymphatic system operate separately f.pdfWho is it important that the lymphatic system operate separately f.pdf
Who is it important that the lymphatic system operate separately f.pdf
arihantmobileselepun
 
Provide a full explanation to the below question.1. Summarize the .pdf
Provide a full explanation to the below question.1. Summarize the .pdfProvide a full explanation to the below question.1. Summarize the .pdf
Provide a full explanation to the below question.1. Summarize the .pdf
arihantmobileselepun
 
why nitrogen fixation is so importantSolutionNitrogen fixation.pdf
why nitrogen fixation is so importantSolutionNitrogen fixation.pdfwhy nitrogen fixation is so importantSolutionNitrogen fixation.pdf
why nitrogen fixation is so importantSolutionNitrogen fixation.pdf
arihantmobileselepun
 

More from arihantmobileselepun (20)

How are the epimysium and tendons relatedA. The epimysium surroun.pdf
How are the epimysium and tendons relatedA. The epimysium surroun.pdfHow are the epimysium and tendons relatedA. The epimysium surroun.pdf
How are the epimysium and tendons relatedA. The epimysium surroun.pdf
 
How does DNA differ from RNAA. RNA uses only purinesB. RNA uses.pdf
How does DNA differ from RNAA. RNA uses only purinesB. RNA uses.pdfHow does DNA differ from RNAA. RNA uses only purinesB. RNA uses.pdf
How does DNA differ from RNAA. RNA uses only purinesB. RNA uses.pdf
 
Gerard throws a discus distances of 10 meters, 14.5 meters, 14.8 met.pdf
Gerard throws a discus distances of 10 meters, 14.5 meters, 14.8 met.pdfGerard throws a discus distances of 10 meters, 14.5 meters, 14.8 met.pdf
Gerard throws a discus distances of 10 meters, 14.5 meters, 14.8 met.pdf
 
For Printing and personal use ONLY. DO NOT Hand in this copy of the a.pdf
For Printing and personal use ONLY. DO NOT Hand in this copy of the a.pdfFor Printing and personal use ONLY. DO NOT Hand in this copy of the a.pdf
For Printing and personal use ONLY. DO NOT Hand in this copy of the a.pdf
 
Find and provide a link to a multi-year capital plan from any Illino.pdf
Find and provide a link to a multi-year capital plan from any Illino.pdfFind and provide a link to a multi-year capital plan from any Illino.pdf
Find and provide a link to a multi-year capital plan from any Illino.pdf
 
A geneticist is working with a new bacteriophage called phage Y3 that.pdf
A geneticist is working with a new bacteriophage called phage Y3 that.pdfA geneticist is working with a new bacteriophage called phage Y3 that.pdf
A geneticist is working with a new bacteriophage called phage Y3 that.pdf
 
Assume that the four living species in the figure below evolved from.pdf
Assume that the four living species in the figure below evolved from.pdfAssume that the four living species in the figure below evolved from.pdf
Assume that the four living species in the figure below evolved from.pdf
 
Declining BalanceThe cost of the asset $10,000.00 The salvage v.pdf
Declining BalanceThe cost of the asset $10,000.00 The salvage v.pdfDeclining BalanceThe cost of the asset $10,000.00 The salvage v.pdf
Declining BalanceThe cost of the asset $10,000.00 The salvage v.pdf
 
Create a java project that - Draw a circle with three random init.pdf
Create a java project that - Draw a circle with three random init.pdfCreate a java project that - Draw a circle with three random init.pdf
Create a java project that - Draw a circle with three random init.pdf
 
aphase The spindle contracts and the sister chromatids are separated.pdf
aphase The spindle contracts and the sister chromatids are separated.pdfaphase The spindle contracts and the sister chromatids are separated.pdf
aphase The spindle contracts and the sister chromatids are separated.pdf
 
A population of wild deer mice includes individuals with long or sho.pdf
A population of wild deer mice includes individuals with long or sho.pdfA population of wild deer mice includes individuals with long or sho.pdf
A population of wild deer mice includes individuals with long or sho.pdf
 
Who is it important that the lymphatic system operate separately f.pdf
Who is it important that the lymphatic system operate separately f.pdfWho is it important that the lymphatic system operate separately f.pdf
Who is it important that the lymphatic system operate separately f.pdf
 
Provide a full explanation to the below question.1. Summarize the .pdf
Provide a full explanation to the below question.1. Summarize the .pdfProvide a full explanation to the below question.1. Summarize the .pdf
Provide a full explanation to the below question.1. Summarize the .pdf
 
Write the interval notation for the set of numbers graphed. Solu.pdf
Write the interval notation for the set of numbers graphed.  Solu.pdfWrite the interval notation for the set of numbers graphed.  Solu.pdf
Write the interval notation for the set of numbers graphed. Solu.pdf
 
Hyenas are diploid organisms, and their gametes contain 20 chromosome.pdf
Hyenas are diploid organisms, and their gametes contain 20 chromosome.pdfHyenas are diploid organisms, and their gametes contain 20 chromosome.pdf
Hyenas are diploid organisms, and their gametes contain 20 chromosome.pdf
 
Why doesnt hemoglobin have any intermediate conformations between .pdf
Why doesnt hemoglobin have any intermediate conformations between .pdfWhy doesnt hemoglobin have any intermediate conformations between .pdf
Why doesnt hemoglobin have any intermediate conformations between .pdf
 
4. What is gyanandromorphy How does this happenSolutionAnswe.pdf
4. What is gyanandromorphy How does this happenSolutionAnswe.pdf4. What is gyanandromorphy How does this happenSolutionAnswe.pdf
4. What is gyanandromorphy How does this happenSolutionAnswe.pdf
 
Which of the following requires the mitochondria to create contact po.pdf
Which of the following requires the mitochondria to create contact po.pdfWhich of the following requires the mitochondria to create contact po.pdf
Which of the following requires the mitochondria to create contact po.pdf
 
why nitrogen fixation is so importantSolutionNitrogen fixation.pdf
why nitrogen fixation is so importantSolutionNitrogen fixation.pdfwhy nitrogen fixation is so importantSolutionNitrogen fixation.pdf
why nitrogen fixation is so importantSolutionNitrogen fixation.pdf
 
what will happen to the photon if two photons enter the same atom si.pdf
what will happen to the photon if two photons enter the same atom si.pdfwhat will happen to the photon if two photons enter the same atom si.pdf
what will happen to the photon if two photons enter the same atom si.pdf
 

Recently uploaded

Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
Chris Hunter
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 

Recently uploaded (20)

ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIFood Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 

Write a program to generate the entire calendar for one year. The pr.pdf

  • 1. Write a program to generate the entire calendar for one year. The program must get two values from the user: (1) the year and (2) the day of the week for January 1st of that year. The year, which should be positive, is needed to check for and handle leap years1. The day of the week for January 1st is needed so that you know where to start the calendar. The user should enter 0 for Sunday, 1 for Monday, ... or 6 for Saturday. As always, you need to validate the user's input. To actually print the calendar, you must use a single method that prints out the calendar for one month and then call this function 12 times from main(), once for each month in the year. To check for a leap year you will need to write another method that takes the year as a parameter and returns true if it’s a leap year, or false otherwise. Stubs (i.e. method signatures without any code) for both of these methods have been provided for you. The calendar should be printed in the form below. In this example, January starts on a Saturday (day 6). Note that February starts on a Tuesday in this example because January ended on a Monday. January 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 2829 30 31 February 1 2 3 4 5 6 7 8 9 10 11 12 ... The format he gave us is as follows: public class PA6a { /** * Error to output if year is not positive */ static final String E_YEAR = "The year must be positive!"; /** * Error to output if the day is not between 0 and 6 */ static final String E_DAY = "The day of January 1st must be between 0 and 6!";
  • 2. /** * Determines if an input is a leap year * * @param year year in question * @ereturn true if a leap year */ public static boolean isLeapYear(int year) { return false; // TODO: replace with your code } /** * Outputs a month to the console * * @param month title * @param startDay 0=Sunday ... 6=Saturday * @param numDays number of days in the month * @return day of the week of the last day of the month */ public static int printMonth(String month, int startDay, int numDays) { return 0; // TODO: replace with your code } /** * Program execution point: * input year, day of the week (0-6) of january 1 * output calendar for that year * * @param args command-line arguments (ignored) */ public static void main(String[] args) {
  • 3. // TODO: write your code here } I am confused especially with the print month method, help is greatly appreciated. Solution package org.students; import java.util.Scanner; import com.sun.org.apache.bcel.internal.generic.IUSHR; public class Calander { //declaring static variables static final String E_YEAR = "The year must be positive!"; static final String E_DAY = "The day of January 1st must be between 0 and 6!"; public static void main(String[] args) { //Declaring variables int getFirstDay; int year; Scanner scanner = new Scanner(System.in); //This while loop continue to execute until user enters valid year while (true) { //Getting the year entered by the user System.out.print("Enter the year: "); year = scanner.nextInt(); if (year < 0) { System.out.println(E_YEAR); } else break; } //This loop continue to execute until user enters valid first day of the month while (true) { //Getting the first day entered by the user System.out.print("Enter 1st day of year ( 0 = Sunday, 6 = Satuday ): "); getFirstDay = scanner.nextInt();
  • 4. //Checking whether the first day is within valid range or not if (getFirstDay < 0 || getFirstDay > 6) { System.out.println(E_DAY); continue; } else break; } for (int month = 1; month <= 12; month++) { int days = 0; String monthName = " "; switch (month) { case 1: monthName = "January"; days = 31; break; case 2: monthName = "February"; if (isLeapYear(year)) { days = 29; } else { days = 28; } break; case 3: monthName = "March"; days = 31; break; case 4: monthName = "April"; days = 30; break; case 5: monthName = "May"; days = 31; break; case 6:
  • 5. monthName = "June"; days = 30; break; case 7: monthName = "July"; days = 31; break; case 8: monthName = "August"; days = 31; break; case 9: monthName = "September"; days = 30; break; case 10: monthName = "October"; days = 31; break; case 11: monthName = "November"; days = 30; break; case 12: monthName = "December"; days = 31; break; default: System.out.print("Invalid Month."); System.exit(0); break; } System.out.println(" " + monthName + " " + year); int i = 0; int firstDay = 0;
  • 6. switch (month) { case 1: firstDay = getFirstDay; break; case 2: firstDay = getFirstDay + 3; break; case 3: firstDay = getFirstDay + 3; break; case 4: firstDay = getFirstDay + 6; break; case 5: firstDay = getFirstDay + 8; break; case 6: firstDay = getFirstDay + 11; break; case 7: firstDay = getFirstDay + 13; break; case 8: firstDay = getFirstDay + 16; break; case 9: firstDay = getFirstDay + 19; break; case 10: firstDay = getFirstDay + 21; break; case 11: firstDay = getFirstDay + 24; break; case 12: firstDay = getFirstDay + 26;
  • 7. break; } if (isLeapYear(year)) { switch (month) { case 1: firstDay = getFirstDay; break; case 2: firstDay = getFirstDay + 3; break; case 3: firstDay = getFirstDay + 4; break; case 4: firstDay = getFirstDay + 7; break; case 5: firstDay = getFirstDay + 9; break; case 6: firstDay = getFirstDay + 12; break; case 7: firstDay = getFirstDay + 14; break; case 8: firstDay = getFirstDay + 17; break; case 9: firstDay = getFirstDay + 20; break; case 10: firstDay = getFirstDay + 22; break; case 11: firstDay = getFirstDay + 25;
  • 8. break; case 12: firstDay = getFirstDay + 27; break; } } printMonth(month, firstDay, days); System.out.println(); } } //This method checks whether the year is leap year or not private static boolean isLeapYear(int year) { boolean bool = false; if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) bool = true; else bool = false; return bool; } //This method will display the month public static void printMonth(int month, int startDay, int numDays) { int dayOfWeek = 0; System.out.println("_______________________________"); System.out.println(" Sun Mon Tue Wed Thu Fri Sat"); if ((startDay % 7) >= 0) { if ((startDay % 7) == 0) { dayOfWeek = 0; } else if ((startDay % 7) == 1) { dayOfWeek = 1; System.out.print(" "); } else if ((startDay % 7) == 2) { dayOfWeek = 2; System.out.print("t "); } else if ((startDay % 7) == 3) { dayOfWeek = 3; System.out.print("tt ");
  • 9. } else if ((startDay % 7) == 4) { dayOfWeek = 4; System.out.print("ttt"); } else if ((startDay % 7) == 5) { dayOfWeek = 5; System.out.print("ttt "); } else if ((startDay % 7) == 6) { dayOfWeek = 6; System.out.print("tttt "); } } for (int i = 1; i <= numDays; i++) { if (i < 10) System.out.print(" " + i); else System.out.print(" " + i); if ((i + startDay) % 7 == 0) System.out.println(); } } } ____________________________________ Output: Enter the year: 2016 Enter 1st day of year ( 0 = Sunday, 6 = Satuday ): 5 January 2016 _______________________________ Sun Mon Tue Wed Thu Fri Sat 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 February 2016
  • 10. _______________________________ Sun Mon Tue Wed Thu Fri Sat 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 March 2016 _______________________________ Sun Mon Tue Wed Thu Fri Sat 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 April 2016 _______________________________ Sun Mon Tue Wed Thu Fri Sat 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 May 2016 _______________________________ Sun Mon Tue Wed Thu Fri Sat 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 June 2016 _______________________________ Sun Mon Tue Wed Thu Fri Sat 1 2 3 4 5 6 7 8 9 10 11
  • 11. 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 July 2016 _______________________________ Sun Mon Tue Wed Thu Fri Sat 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 August 2016 _______________________________ Sun Mon Tue Wed Thu Fri Sat 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 September 2016 _______________________________ Sun Mon Tue Wed Thu Fri Sat 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 October 2016 _______________________________ Sun Mon Tue Wed Thu Fri Sat 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
  • 12. 30 31 November 2016 _______________________________ Sun Mon Tue Wed Thu Fri Sat 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 December 2016 _______________________________ Sun Mon Tue Wed Thu Fri Sat 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 ____________________________