Goals: Writing Java Class Divide and Conquer Problem Solving Read PrintCalendar.
ID: 3786606 • Letter: G
Question
Goals:
Writing Java Class
Divide and Conquer
Problem Solving
Read PrintCalendar.java and write a corresponding Java class Calendar.java that prints the calendar of a specific month. The calendar should support the months back to January 1800.
PrintCalendar.java:
public class PrintCalendar {
public static void main(String[] args){
Calendar cal1 = new Calendar();
cal1.printMonth();
Calendar cal2 = new Calendar(2017,1);
cal2.printMonth();
cal2.setYear(2016);
cal2.setMonth(10);
cal2.printMonth();
}
}
Following is the output when PrintCalendar program is executed:
Correctness: Program works without bugs
Correct class definitions, including private fields, assessors, mutators, etc.
Divide and Conquer with multiple methods
Presentation: Organization, legibility, and readability
Style: Descriptive identifier, indentation, bracket placement.
Documentation: Appropriate Comments
Calendar.java is responsible to work with PrintCalendar.java provided.
Donot ask for user input but PrintCalendar should work perfectly with different data.
Edit Vi Adobe Reed Wind prints the calendar of a specific month. The calendar should support the months back to January 1800. Following is the output when PrintCalendar program is executed D: Code: java PrintCalendar Feburary 2017 Sun Moon Tue Wed Thu Fri Sat 10 11 12 13 15 16 17 18 20 21 22 23 25 19 26 27 January 2017 Sun Mon Tue Wed Thu Fri Sat 10 11 12 13 14 16 17 15 18 19 20 21 22 23 25 26 27 28 29 31 October 2016 Sun Mon Tue Med Thu Fri Sat 10 11 12 13 14 15 16 17 19 20 21 22 23 25 26 27 28 29 30 31 Tools Sign Commcnt Export PDF Adobe Export PDF nt 1.pdf Convert To: Microsoft Vosd docs) shtul.S. Convert Create PDF Send Files Store FilesExplanation / Answer
//Calendar.java
import java.time.LocalDateTime;
public class Calendar {
//declare integer variables
private int year;
private int month;
//Constructor
public Calendar(){
//get current month and year
LocalDateTime now = LocalDateTime.now();
year = now.getYear();
month = now.getMonthValue();
}
//Constrctor that takes year and month
public Calendar(int year, int month){
this.year=year;
this.month=month;
}
/** Print the calendar for a month in a year */
void printMonth() {
// Get start day of the week for the first date in the month
int startDay = getStartDay(year, month);
// Get number of days in the month
int numOfDaysInMonth = getNumOfDaysInMonth(year, month);
// Print headings
printMonthTitle(year, month);
// Print body
printMonthBody(startDay, numOfDaysInMonth);
}
//Set year
public void setYear(int year){
this.year=year;
}
//Set month
public void setMonth(int month){
this.month=month;
}
/** Return starting day of month in a year */
int getStartDay(int year, int month) {
// Number of days since 1/1/1800
int startDay1800 = 3;
long totalNumOfDays = getTotalNumOfDays(year, month);
// return start day
return (int)((totalNumOfDays + startDay1800) % 7);
}
/** Returns total number of days in a given year and month */
static long getTotalNumOfDays(int year, int month) {
long total = 0;
// Get the total days from 1800 to year -1
for (int i = 1800; i < year; i++)
{
if (isLeapYear(i))
total = total + 366;
else
total = total + 365;
}
for (int i = 1; i < month; i++)
total = total + getNumOfDaysInMonth(year, i);
return total;
}
/**Returns number of days in a monht */
static int getNumOfDaysInMonth(int year, int month) {
if (month == 1 || month==3 || month == 5 || month == 7 ||
month == 8 || month == 10 || month == 12)
return 31;
if (month == 4 || month == 6 || month == 9 || month == 11)
return 30;
if (month == 2)
if (isLeapYear(year))
return 29;
else
return 28;
return 0;
}
/** Check if year is leap year */
static boolean isLeapYear(int year) {
if ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)))
return true;
return false;
}
/** Print days of month to console */
static void printMonthBody(int startDay, int numOfDaysInMonth) {
int i = 0;
for (i = 0; i < startDay; i++)
System.out.print(" ");
for (i = 1; i <= numOfDaysInMonth; i++) {
if (i < 10)
System.out.print(" " + i);
else
System.out.print(" " + i);
//print a new line for every 7 days
if ((i + startDay) % 7 == 0)
System.out.println();
}
System.out.println();
}
/**Print month title */
static void printMonthTitle(int year, int month) {
System.out.println(" " + getMonthName(month)
+ ", " + year);
System.out.println("-----------------------------");
System.out.println(" Sun Mon Tue Wed Thu Fri Sat");
}
/** Return month name */
static String getMonthName(int month) {
String monthName = null;
switch (month) {
case 1: monthName = "January"; break;
case 2: monthName = "February"; break;
case 3: monthName = "March"; break;
case 4: monthName = "April"; break;
case 5: monthName = "May"; break;
case 6: monthName = "June"; break;
case 7: monthName = "July"; break;
case 8: monthName = "August"; break;
case 9: monthName = "September"; break;
case 10: monthName = "October"; break;
case 11: monthName = "November"; break;
case 12: monthName = "December";
}
return monthName;
}
}
---------------------------------------------------------------------------------------------------------------------------------------------------
/**
* The java program PrintCalendar that tests
* the Calendar class and prints month of the year
* to console.
* */
//PrintCalendar.java
public class PrintCalendar {
public static void main(String[] args){
//Creat an instance of Calendar
Calendar cal1 = new Calendar();
//call printMonth method
cal1.printMonth();
Calendar cal2 = new Calendar(2017,1);
//call printMonth method
cal2.printMonth();
//Set year
cal2.setYear(2016);
//Set month
cal2.setMonth(10);
//call printMonth method
cal2.printMonth();
}
}
---------------------------------------------------------------------------------------------------------------------------------------------------
Sample output:
February, 2017
-----------------------------
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
January, 2017
-----------------------------
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
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
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.