Here is my code: public class CalendarAssignment { /** * Error to output if year
ID: 3937202 • Letter: H
Question
Here is my code:
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, s = 1; j > 0; ) {
if (startDay >= 1 ) {
for ( int i = 0; i < startDay; i++ ) {
System.out.print(" ");
nextLine++;
}
startDay = 0;
} else {
if ( s < 10 )
System.out.print( " " + s );
if ( s >= 10 )
System.out.print( " " + s );
nextLine++;
j--;
s++;
}
if (nextLine > 7) {
System.out.println( "" );
nextLine = 1;
}
}
return nextLine-1;
}
/**
* 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) {
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): ");
int firstDay = input.nextInt();
if (year<=0) {
System.out.println("The year must be positive!");
System.exit(0);
}
if (firstDay<0 || firstDay>6) {
System.out.println("The day of January 1st must be between 0 and 6!");
System.exit(0);
}
int numFebDays;
if (isLeapYear(year)) {
numFebDays = 29;
} else {
numFebDays = 28;
}
int lastDayOfWeek;
lastDayOfWeek = printMonth("January", firstDay, 31);
System.out.println();
lastDayOfWeek = printMonth("February", lastDayOfWeek, numFebDays);
System.out.println();
lastDayOfWeek = printMonth("March", lastDayOfWeek, 31);
System.out.println();
System.out.println();
lastDayOfWeek = printMonth("April", lastDayOfWeek, 30);
System.out.println();
System.out.println();
lastDayOfWeek = printMonth("May", lastDayOfWeek, 31);
System.out.println();
System.out.println();
lastDayOfWeek = printMonth("June", lastDayOfWeek, 30);
System.out.println();
System.out.println();
lastDayOfWeek = printMonth("July", lastDayOfWeek, 31);
System.out.println();
System.out.println();
lastDayOfWeek = printMonth("August", lastDayOfWeek, 31);
System.out.println();
System.out.println();
lastDayOfWeek = printMonth("September", lastDayOfWeek, 30);
System.out.println();
System.out.println();
lastDayOfWeek = printMonth("October", lastDayOfWeek, 31);
System.out.println();
lastDayOfWeek = printMonth("November", lastDayOfWeek, 30);
System.out.println();
System.out.println();
lastDayOfWeek = printMonth("December", lastDayOfWeek, 31);
System.out.println();
System.out.println();
}
}
Any help would be greatly appreciated. Thanks!!
Explanation / Answer
import java.util.Scanner;
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, s = 1; j > 0; ) {
if (startDay >= 1 ) {
for ( int i = 0; i < startDay; i++ ) {
System.out.print(" ");
nextLine++;
}
startDay = 0;
} else {
if ( s < 10 )
System.out.print( " " + s );
if ( s >= 10 )
System.out.print( " " + s );
nextLine++;
j--;
s++;
}
if (nextLine > 7) {
System.out.println( "" );
nextLine = 1;
}
}
return nextLine-1;
}
/**
* 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) {
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): ");
int firstDay = input.nextInt();
if (year<=0) {
System.out.println("The year must be positive!");
System.exit(0);
}
if (firstDay<0 || firstDay>6) {
System.out.println("The day of January 1st must be between 0 and 6!");
System.exit(0);
}
int numFebDays;
if (isLeapYear(year)) {
numFebDays = 29;
} else {
numFebDays = 28;
}
int lastDayOfWeek;
lastDayOfWeek = printMonth("January", firstDay, 31);
System.out.println();
lastDayOfWeek = printMonth("February", lastDayOfWeek, numFebDays);
System.out.println();
lastDayOfWeek = printMonth("March", lastDayOfWeek, 31);
System.out.println();
System.out.println();
lastDayOfWeek = printMonth("April", lastDayOfWeek, 30);
System.out.println();
System.out.println();
lastDayOfWeek = printMonth("May", lastDayOfWeek, 31);
System.out.println();
System.out.println();
lastDayOfWeek = printMonth("June", lastDayOfWeek, 30);
System.out.println();
System.out.println();
lastDayOfWeek = printMonth("July", lastDayOfWeek, 31);
System.out.println();
System.out.println();
lastDayOfWeek = printMonth("August", lastDayOfWeek, 31);
System.out.println();
System.out.println();
lastDayOfWeek = printMonth("September", lastDayOfWeek, 30);
System.out.println();
System.out.println();
lastDayOfWeek = printMonth("October", lastDayOfWeek, 31);
System.out.println();
lastDayOfWeek = printMonth("November", lastDayOfWeek, 30);
System.out.println();
System.out.println();
lastDayOfWeek = printMonth("December", lastDayOfWeek, 31);
System.out.println();
System.out.println();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.