Problem 2 (name this Lab7_Problem2) (Java Program) Write a program to produce th
ID: 3757675 • Letter: P
Question
Problem 2 (name this Lab7_Problem2)
(Java Program)
Write a program to produce the following output using for loops. You will be doing a portion of your output (the days within the month) by calling a method.
The first for loop must be implemented in main().
The second for loop must be called from within main() at the proper location and implemented using a void() method. Pass the void() method the maximum days for the particular month being evaluated.
Here are some important tips:
The month display must be governed by the for loop in main(); the day by the for loop in the void() method.
To determine the maximum number of days in a particular month (which is needed for your inner loop's for condition), use an if-else if test or a switch block (your choice) and the rhyme you probably learned in elementary school to remember how many days are in each month. If you forgot the rhyme, look it up. "Thirty days has September…"
Do not attempt to correct for leap year/no leap year. That is beyond the scope of this problem.
Sample output below. Note the ellipses are just placeholders; not all data is included.
Month: 1
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
. . .
Day 25
Day 26
Day 27
Day 28
Day 29
Day 30
Day 31
Month: 2
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
Day 9
. . .
Day 26
Day 27
Day 28
Month: 3
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
. . .]
Day 25
Day 26
Day 27
Day 28
Day 29
Day 30
. . .
Month: 12
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
Day 9
Day 10
Day 11
Day 12
Day 13
Day 14
Day 15
. . .
Day 29
Day 30
Day 31
Explanation / Answer
Solution:
package com.chegg.nancy.solutions;
public class CalenderDays {
public static void main(String[] args) {
for (int i = 1; i <= 12; i++) {
System.out.println("Month:" + i);
daysInMonth(i);
}
}
private static void daysInMonth(int monthNum) {
if (monthNum == 1 || monthNum == 3 || monthNum == 5 || monthNum == 7 || monthNum == 8 || monthNum == 10
|| monthNum == 12) {
for (int i = 1; i <= 31; i++)
System.out.println("Day " + i);
} else if (monthNum == 4 || monthNum == 6 || monthNum == 9 || monthNum == 11) {
for (int i = 1; i <= 30; i++)
System.out.println("Day " + i);
} else {
for (int i = 1; i <= 28; i++)
System.out.println("Day " + i);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.