Question: Re work assignment 1 onto a drawing panel and submit MUST write a method showGrid(graphic, month, year) that produces output, see example below. You also
Re work assignment 1 onto a drawing panel and submit
MUST write a method showGrid(graphic, month, year) that produces output, see example below.
You also keep showGrid(month, year) in this Class as an optional overload of the method.
int month is a parameter, must be 1-12 for the twelve months of the year
int year is a year of the Gregorian calendar (today's European and American standard)
MUST use a method daysInMonth, Exercise #4, page 309, Chapter 4.
showGrid works for any month (1-12), and any year (1900-2100) including leap years.

(This was assignment 1):
import java.util.Scanner;
public class DisplayCalendar {
public static int day(int month, int day, int year) {
int y = year - (14 - month) / 12;
int x = y + y / 4 - y / 100 + y / 400;
int m = month + 12 * ((14 - month) / 12) - 2;
int d = (day + x + (31 * m) / 12) % 7;
return d;
}
public static void showGrid(int month, int year) {
String[] months = { "", // to make jan takes months[1] we left first value
"January", "February", "March", "April", "May", "June", "July", "August", "September", "October",
"November", "December" };
// days[i] = number of days in month i,in month 0 it is 0 days.in jan -31,in feb
// -28 ..
int[] days = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
// checking for leap year
if (month == 2 && isLeapYear(year))
days[month] = 29;
// print statements for calendar header
System.out.println(" " + months[month] + " " + year);
System.out.println(" sun mon tue wed thu fri Sat");
System.out.println(" +----+----+----+----+----+----+----");
// starting day
int d = day(month, 1, year);
// printing the calendar
for (int i = 0; i
System.out.printf(" ");
for (int i = 1; i
System.out.printf("%4d ", i);
if (((i + d) % 7 == 0) || (i == days[month]))
System.out.println(" ");
}
System.out.println(" +----+----+----+----+----+----+----");
}
// return true if the given year is a leap year
public static boolean isLeapYear(int year) {
if ((year % 4 == 0) && (year % 100 != 0))
return true;
if (year % 400 == 0)
return true;
return false;
}
public static void main(String[] args) {
int month = 0;
int year = 0;
Scanner reader = new Scanner(System.in); // Reading from System.in
System.out.println("Enter month ");
try {
month = reader.nextInt(); // from console input
if (month
throw new NumberFormatException();
}
} catch (NumberFormatException e) {
System.out.println("not a valid number");
System.exit(1);
}
System.out.println("Enter year ");
try {
year = reader.nextInt(); // from console input
if (year 2100) {
throw new NumberFormatException();
}
} catch (NumberFormatException e) {
System.out.println("not a valid number, give value between 1900 to 2100");
System.exit(1); // code execution stops
}
// calling show grid method
showGrid(month, year);
}
}
6 ALWAYS: Name, date, course WHY : Assignment #1 8 and whatever else might be relevant 9 10 11 public class DayGrid 12 public static void main(String[] args) 13 int month 8; // August in human world int year2017; DrawingPanel p new DrawingPanel (400,200); Graphics g p.getGraphics(); showGrid(g, month, year); // Assignment #2 //showGrld (month, year); // Assignment #1 15 17 18 19 20 21 22 public static void showGrid(Graphics g, int month, int year) 23 // method to display month as a grid Drawing Panel File View Help Sun Mon Tue Wed Thu Fri Sat 10 11 12 13 | 14 | 15 | 16 | 17 | 18 | 19 20 21 272829 30 31 22 23 24 25 26 x-385, y-176), r-255 g 255 b-255 43
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
