Help needed program

Discussion in 'Java' started by Tareeq, Feb 28, 2007.

  1. Tareeq

    Tareeq New Member

    Joined:
    Feb 28, 2007
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    this is a question iam working on

    Write a program that prompts the user to enter the year and first day of the year, and
    displays the calendar table for the year on the console. For example, if the user
    entered the year 2005, and 6 for Saturday, January 1, 2007, your program should
    display the calendar for each month in the year.


    January 2007
    Sun Mon Tue Wed Thu Frid 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
    . . .
    December 2007
    Sun Mon Tue Wed Thu Frid 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


    Guide: User inputs are the year and the first day of the year (ie: 1 for Sunday, 2
    for Monday, 3 for Tuesday, so on and so forth). Refer Liang textbook on
    Calendar class.




    actually got this program but stuck and dont know how to go abt it
    import javax.swing.*;
    Code:
    public class Calender{
    	public static void main(String arngs[]){
    		
    		String year = JOptionPane.showInputDialog("Enter full year (eg.2002):");
    		int year1= Integer.parseInt(year);
    		
    		String month = JOptionPane.showInputDialog("Enther month in number between 1 an 12:");
    		int month1 = Integer.parseInt(month);
    		
    		Print_month(year1,month1);
    	}
    	
    	static void Print_month(int year1,int month1){
    		
    		Print_monthTitle(year1,month1);
    		Print_monthBody(year1,month1);
    		
    	}
    	static void Print_monthTitle(int year1,int month1){
    		System.out.println("             "+getMonth_name(month1)+" "+year1);
    		System.out.println("-------------------------------------------");
    		System.out.println(" SUN MON TUE WED THU FRI SAT");
    	}
    	
    	static String getMonth_name(int month){
    		
    		String Month_name= null;
    		
    		switch (month){
    			
    			case 1: Month_name = "JANUARY"; 
    					break;
    			case 2: Month_name = "FEBRUARY"; 
    					break;
    			case 3: Month_name = "MARCH"; 
    					break;
    			case 4: Month_name = "APRIL"; 
    					break;		
    			case 5: Month_name = "MAY"; 
    					break;
    			case 6: Month_name = "JUNE"; 
    					break;		
    			case 7: Month_name = "JULY"; 
    					break;
    			case 8: Month_name = "AUGUAST"; 
    					break;		
    			case 9: Month_name = "SEPTEMBER"; 
    					break;
    			case 10: Month_name = "OCTOBER"; 
    					break;		
    			case 11: Month_name = "NOVEMBER"; 
    					break;
    			case 12: Month_name = "DECEMBER"; 
    					break;
    		}
    		
    		return Month_name;
    	}
    	
    	static void Print_monthBody(int year1,int month1){
    		
    		int Start_day=getStart_day(year1,month1);
    		int TotalDaysInMonth=getTotalDaysInMonth(year1,month1);
    		
    		for(int i=0;i<Start_day;i++)
    			System.out.print("    ");
    		for(int j=1;j<=TotalDaysInMonth;j++){
    			
    			if(j<10)
    				System.out.print("   "+j);
    			else
    				System.out.print("  "+j);
    				
    			if((j+Start_day)%7==0)
    				System.out.println();	
    		}
    		
    	}
    	static int getStart_day(int year1,int month1){
    		
    		int Start_day1800=3;
    		int Total_numberOfdays= getTotal_numberOfdays(year1,month1);
    		
    		return(Total_numberOfdays+Start_day1800)%7;
    	}
    	static int getTotal_numberOfdays(int year1, int month1){
    		
    		int total=0;
    		
    		for(int i=1800;i<year1;i++)
    		if(isLeapYear(i))
    			 total=total+366;
    		else
    			total=total+365;
    			
    		for(int j=0;j<month1;j++)
    		total= total+ getTotalDaysInMonth(year1,j);
    		
    		return total;
    	}
    	static int getTotalDaysInMonth(int year1,int month1){
    		
    		if(month1==1||month1==3||month1==5||month1==7||month1==8||month1==10||month1==12)
    			return 31;
    		if(month1==4||month1==6||month1==9||month1==11)
    			return 30;
    		if(month1==2) return isLeapYear(year1) ? 29:28;
    		
    		
    		return 0;
    	}
    	
    	static boolean isLeapYear(int year1){
    		
    		return year1%400==0||(year1%400==0 && year1%10 !=0);
    	}
    }

    , plz help thnx
     
    Last edited by a moderator: Mar 1, 2007

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice