basic java programming example calendar

3
MyCalendar.java class MyCalendar { public static void main(String[] args) { String month[] = {"J A N U A R Y", "F E B R U A R Y", "M A R C H", "A P R I L", "M A Y", "J U N E", "J U L Y","A U G U S T", "S E P T E M B E R", "O C T O B E R", "N O V E M B E R", "D E C E M B E R"}; String wkday[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; int daysInMonth[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int counter=0; for(int dim=0, m=0; dim<daysInMonth.length && m<month.length; dim++, m++) here:{ System.out.println("\n"); System.out.println(month[m]); for(int w=0; w<wkday.length; w++) System.out.print(wkday[w]+"\t"); System.out.println(); for(int tab=1; tab<=counter; tab++) System.out.print("\t"); for(int d=1; d<=daysInMonth[dim]; d++) { daysInMonth[1]=29; System.out.print(d + "\t"); counter++; if(counter == 7) { System.out.println(); counter=0; } } break here; } } } Page 1

Upload: noeldtan

Post on 10-Apr-2015

3.554 views

Category:

Documents


2 download

DESCRIPTION

This calendar module defaults to a Leap Year with January 1 falling on a Sunday, eg 1956. Next example will allow one to input the YEAR and change the calendar printout. Whoever comes up with an elegant solution for the next example will receive $50 just for fun. You may post your solution in the comment section. Good luck.

TRANSCRIPT

Page 1: BASIC JAVA PROGRAMMING EXAMPLE CALENDAR

MyCalendar.javaclass MyCalendar{public static void main(String[] args){String month[] = {"J A N U A R Y", "F E B R U A R Y", "M A R C H", "A P R I L", "M A Y", "J U N E", "J U L Y","A U G U S T", "S E P T E M B E R", "O C T O B E R", "N O V E M B E R", "D E C E M B E R"};String wkday[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};int daysInMonth[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};int counter=0;

for(int dim=0, m=0; dim<daysInMonth.length && m<month.length; dim++, m++)here:{System.out.println("\n");System.out.println(month[m]);for(int w=0; w<wkday.length; w++)System.out.print(wkday[w]+"\t");System.out.println();

for(int tab=1; tab<=counter; tab++)System.out.print("\t");

for(int d=1; d<=daysInMonth[dim]; d++){daysInMonth[1]=29;System.out.print(d + "\t");counter++;if(counter == 7){System.out.println();counter=0;}}break here;}}}

Page 1

Page 2: BASIC JAVA PROGRAMMING EXAMPLE CALENDAR

MyCalendar.java/* OUTPUT

J A N U A R YSun Mon Tue Wed Thu Fri Sat1 2 3 4 5 6 78 9 10 11 12 13 1415 16 17 18 19 20 2122 23 24 25 26 27 2829 30 31

F E B R U A R YSun Mon Tue Wed Thu Fri Sat 1 2 3 45 6 7 8 9 10 1112 13 14 15 16 17 1819 20 21 22 23 24 2526 27 28 29

M A R C HSun Mon Tue Wed Thu Fri Sat 1 2 34 5 6 7 8 9 1011 12 13 14 15 16 1718 19 20 21 22 23 2425 26 27 28 29 30 31

A P R I LSun Mon Tue Wed Thu Fri Sat1 2 3 4 5 6 78 9 10 11 12 13 1415 16 17 18 19 20 2122 23 24 25 26 27 2829 30

M A YSun Mon Tue Wed Thu Fri Sat 1 2 3 4 56 7 8 9 10 11 1213 14 15 16 17 18 1920 21 22 23 24 25 2627 28 29 30 31

J U N ESun Mon Tue Wed Thu Fri Sat 1 23 4 5 6 7 8 910 11 12 13 14 15 1617 18 19 20 21 22 2324 25 26 27 28 29 30

Page 2

Page 3: BASIC JAVA PROGRAMMING EXAMPLE CALENDAR

MyCalendar.javaJ U L YSun Mon Tue Wed Thu Fri Sat1 2 3 4 5 6 78 9 10 11 12 13 1415 16 17 18 19 20 2122 23 24 25 26 27 2829 30 31

A U G U S TSun Mon Tue Wed Thu Fri Sat 1 2 3 45 6 7 8 9 10 1112 13 14 15 16 17 1819 20 21 22 23 24 2526 27 28 29 30 31

S E P T E M B E RSun Mon Tue Wed Thu Fri Sat 12 3 4 5 6 7 89 10 11 12 13 14 1516 17 18 19 20 21 2223 24 25 26 27 28 2930

O C T O B E RSun Mon Tue Wed Thu Fri Sat 1 2 3 4 5 67 8 9 10 11 12 1314 15 16 17 18 19 2021 22 23 24 25 26 2728 29 30 31

N O V E M B E RSun Mon Tue Wed Thu Fri Sat 1 2 34 5 6 7 8 9 1011 12 13 14 15 16 1718 19 20 21 22 23 2425 26 27 28 29 30

D E C E M B E RSun Mon Tue Wed Thu Fri Sat 12 3 4 5 6 7 89 10 11 12 13 14 1516 17 18 19 20 21 2223 24 25 26 27 28 2930 31

*/

Page 3