2 Replies - 3645 Views - Last Post: 08 November 2010 - 08:13 AM Rate Topic: -----

#1 Guest_JRmendz*


Reputation:

Can you Help me do this Gregorian Calendar?

Posted 08 November 2010 - 08:05 AM

View PostJRmendz, on 08 November 2010 - 07:00 AM, said:

IT 122 : Programming Fundamentals 2
Output: Handwritten program
Due Date: November 19, 2010
----------------------------------------------------------------------------------
Write a program that can be used to generate a Gregorian calendar for a specified year.
INPUTS: year, day for January 1 of the year
OUTPUT: Calendar for the year such that monthly calendars are shown on the screen one at a time .
Notes:
You are NOT expected to use the Calendar and /or Date classes. You have to write the complete program
by utilizing only the primitive data types and the String class.
Take note that you have to determine if the input year is a leap year or not. There are 29 days for the month
of February of a leap year. Otherwise, February has 28 days. A year is a leap year if it is divisble by 4 but
not by 100 or if it is divisible by 400. For example, the yer 2003 is not a leap year; 2004 is a leap year.
1900 hundred is NOT a leap year becuase it is divisible by 100. 2000 is a leap year because it is dvisible by
400 although it is divisible by 100. Since the Gregorian calendar was adopted in 1582, validation of the
year entered must be done such that an error message is produced when the year entered is less than 1582.
------------------------------------------------------------------------------------
Sample run:
Calendar maker
Enter a year: 2011
Enter the day for January 1 <1/2/3/4/5/6/7>
Type 1 for Sunday.
Type 2 for Monday.
Type 3 for Tuesday.
Type 4 for Wednesday.
Type 5 for Thursday.
Type 6 for Friday.
Type 7 for Saturday.
Enter the day: 7
January 2011
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
Press enter to continue...
February 2011
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
Press enter to continue...
March 2011
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
Press enter to continue...
April 2011
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
Press enter to continue...
May 2011
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
Press enter to continue...
June 2011
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
Press enter to continue...
July 2011
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
Press enter to continue...
August 2011
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
Press enter to continue...
September 2011
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
Press enter to continue...
October 2011
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
Press enter to continue...
November 2011
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
Press enter to continue...
December 2011
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

i found a program, the output are the same. But we should use methods in it, i wish you could help me and for me to understand that program myself.


import java.util.Scanner;

public class calendar {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		
		System.out.print("Enter the year: ");
		int year = scanner.nextInt();
		
		System.out.print("Enter 1st day of year ( 0 = Sunday, 6 = Satuday ): ");
		int getFirstDay = scanner.nextInt();
		
		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("Error: this month does not exist"); 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;
			if ( month == 1 ){
				firstDay = getFirstDay;
			} else if ( month == 2 ){
				firstDay = getFirstDay + 3;
			} else if ( month == 3 ){
				firstDay = getFirstDay + 3;
			} else if ( month == 4 ){
				firstDay = getFirstDay + 6;
			} else if ( month == 5 ){
				firstDay = getFirstDay + 8;
			} else if ( month == 6 ){
				firstDay = getFirstDay + 11;
			} else if ( month == 7 ){
				firstDay = getFirstDay + 13;
			} else if ( month == 8 ){
				firstDay = getFirstDay + 16;
			} else if ( month == 9 ){
				firstDay = getFirstDay + 19;
			} else if ( month == 10 ){
				firstDay = getFirstDay + 21;
			} else if ( month == 11 ){
				firstDay = getFirstDay + 24;
			} else if ( month == 12 ){
				firstDay = getFirstDay + 26;
			}
			
			if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)){
				if ( month == 1 ){
					firstDay = getFirstDay;
				} else if ( month == 2 ){
					firstDay = getFirstDay + 3;
				} else if ( month == 3 ){
					firstDay = getFirstDay + 4;
				} else if ( month == 4 ){
					firstDay = getFirstDay + 7;
				} else if ( month == 5 ){
					firstDay = getFirstDay + 9;
				} else if ( month == 6 ){
					firstDay = getFirstDay + 12;
				} else if ( month == 7 ){
					firstDay = getFirstDay + 14;
				} else if ( month == 8 ){
					firstDay = getFirstDay + 17;
				} else if ( month == 9 ){
					firstDay = getFirstDay + 20;
				} else if ( month == 10 ){
					firstDay = getFirstDay + 22;
				} else if ( month == 11 ){
					firstDay = getFirstDay + 25;
				} else if ( month == 12 ){
					firstDay = getFirstDay + 27;
				}
			}
				
			int dayOfWeek = 0;
			if ( (firstDay % 7 ) >= 0 ){
				if ( (firstDay % 7 ) == 0 ){
					dayOfWeek = 0;
				} else if ( (firstDay % 7 ) == 1 ){
					dayOfWeek = 1;
					System.out.print("\t " );
				} else if ( (firstDay % 7 ) == 2 ){
					dayOfWeek = 2;
					System.out.print("\t\t  " );
				} else if ( (firstDay % 7 ) == 3 ){
					dayOfWeek = 3;
					System.out.print("\t\t\t   " );
				} else if ( (firstDay % 7 )  == 4 ){
					dayOfWeek = 4;
					System.out.print("\t\t\t\t\t" );
				} else if ( (firstDay % 7 ) == 5 ){
					dayOfWeek = 5;
					System.out.print("\t\t\t\t\t\t " );
				} else if ( (firstDay % 7 ) == 6 ){
					dayOfWeek = 6;
					System.out.print("\t\t\t\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();
		}
	}
}



Is This A Good Question/Topic? 0

Replies To: Can you Help me do this Gregorian Calendar?

#2 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: Can you Help me do this Gregorian Calendar?

Posted 08 November 2010 - 08:06 AM

What problems or errors are you encountering? What is your question?
Was This Post Helpful? 0
  • +
  • -

#3 Guest_JRmendz*


Reputation:

Re: Can you Help me do this Gregorian Calendar?

Posted 08 November 2010 - 08:13 AM

View Postmacosxnerd101, on 08 November 2010 - 07:06 AM, said:

What problems or errors are you encountering? What is your question?


Quote

The Problem is they are not aligned and my instructor said we should use methods and minimize the if else statements.

Was This Post Helpful? 0

Page 1 of 1