HELP JAVA ASSIGNMENT So here is my programming assignment Wr
HELP JAVA ASSIGNMENT!!
So here is my programming assignment: \"Write a program in Java 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. \"
I have everything in my code working except the numbers on the calendar don\'t line up like they should. Here\'s a picture of how it is SUPPOSED TO LOOK LIKE THE LEFT SIDE. Mine looks like the one to the RIGHT. I somehow have extra spaces. Can you PLEASE help with this problem? THANKS in advance!!
This is my code ( 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. To actually print the calendar, a SINGLE method that prints out the calendar for one month must be used and then called 12 times from main(), once for each month in the year. To check for a leap year we 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.)
:
public class CalendarAssignment {
/**
* 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) {
boolean leapyear = false;
if(year%4 == 0) {
if(year%100 == 0){
if(year%400 == 0){
return !leapyear;
};
}else{
return !leapyear;
}
}
return leapyear; // 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) {
System.out.println(month);
int nextLine=1;
for(int j=numDays,k=1;j>0;){
if(startDay>0){
for(int i=0;i System.out.printf(\"\\t\");
nextLine++;
}
startDay=0;
}else{
System.out.printf(k + \"\\t\");
nextLine++;
k++;
j--;
}
if((nextLine >7)){System.out.println(); nextLine=1;}
}
return (nextLine-1);
// 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) {
String[] months = {\"January\", \"February\", \"March\", \"April\", \"May\",
\"June\", \"July\", \"August\", \"September\", \"October\", \"November\",
\"December\" };
int[] total_Days_In_Months = {31,28,31,30,31,30,31,31,30,31,30,31};
Scanner input = new Scanner(System.in);
System.out.print(\"Enter the year: \");
int year = input.nextInt();
System.out.print(\"Enter the day of the week of January 1st (0=Sunday, 1=Monday, ... 6=Saturday): \");
if(year <= 0) {
System.out.print(E_YEAR);
System.out.println();
System.exit(0);
}
int day_of_week = input.nextInt();
if(day_of_week <0 || day_of_week >6)
{
System.out.print(E_DAY);
System.out.println();
System.exit(0);
}
if(isLeapYear(year)){
total_Days_In_Months[1] = 29;
}
for(int j=0;j int endDay = day_of_week;
day_of_week = printMonth(months[j], endDay, total_Days_In_Months[j]);
System.out.println();
}
}
}
Solution
package org.students;
import java.util.Scanner;
public class DisplayCalendar {
public static void main(String[] args) {
int getFirstDay;
Scanner scanner = new Scanner(System.in);
System.out.print(\"Enter the year: \");
int year = scanner.nextInt();
while(true)
{
System.out.print(\"Enter 1st day of year ( 0 = Sunday, 6 = Satuday ): \");
getFirstDay = scanner.nextInt();
if(getFirstDay<0 || getFirstDay>6)
{
System.out.println(\":: Invalid Day.Enter Valid number between 0 and 6 ::\");
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 (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) {
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);
System.out.println(\"__________________________________\");
System.out.println(\" Sun Mon Tue Wed Thu Fri Sat\");
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 (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) {
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;
}
}
int dayOfWeek = 0;
if ((firstDay % 7) >= 0) {
if ((firstDay % 7) == 0) {
dayOfWeek = 0;
} else if ((firstDay % 7) == 1) {
dayOfWeek = 1;
System.out.print(\" \");
} else if ((firstDay % 7) == 2) {
dayOfWeek = 2;
System.out.print(\"\\t \");
} else if ((firstDay % 7) == 3) {
dayOfWeek = 3;
System.out.print(\"\\t\\t \");
} else if ((firstDay % 7) == 4) {
dayOfWeek = 4;
System.out.print(\"\\t\\t\\t\");
} else if ((firstDay % 7) == 5) {
dayOfWeek = 5;
System.out.print(\"\\t\\t\\t \");
} else if ((firstDay % 7) == 6) {
dayOfWeek = 6;
System.out.print(\"\\t\\t\\t\\t \");
}
}
for (i = 1; i <= days; i++) {
if (i < 10)
System.out.print(\" \" + i);
else
System.out.print(\" \" + i);
if ((i + firstDay) % 7 == 0)
System.out.println();
}
System.out.println();
}
}
}
______________________________
Output:
Enter the year: 2016
Enter 1st day of year ( 0 = Sunday, 6 = Satuday ): 7
:: Invalid Day.Enter Valid number between 0 and 6 ::
Enter 1st day of year ( 0 = Sunday, 6 = Satuday ): 9
:: Invalid Day.Enter Valid number between 0 and 6 ::
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









