Thursday, November 25, 2010

BlueJ Program on displaying day name from the entered date in specific format

Here is the BlueJ Program for ISC Students.

Write a program to accept a date in a specific string format (dd/mm/yyyy) and accept the name of the day on 1st January of the corresponding year. Find the day for the given date.

Example:
Input:
Enter the date ( in dd/mm/yyyy) format: 5/7/2001
Enter the Day on 1st January in this year: Monday

Output:
5/7/2001 : Thursday

import java.io.*;
class Date1
{
int arr[]={31,28,31,30,31,30,31,31,30,31,30,31};
BufferedReader br;
String str1,day,day1;
int x,i,dayno,mon,yr,leap1;
public static void main(String args[])throws IOException
{
Date1 ob=new Date1 ();
ob.take();
ob.show();
}
Date1 ()
{
br=new BufferedReader(new InputStreamReader(System.in));
}
public void take()throws IOException
{
System.out.println("Enter the date ( in dd/mm/yyyy) format:");
day=br.readLine().trim();
day1=day;
i=day.indexOf("/");
dayno=Integer.parseInt(day.substring(0,i));
day=day.substring(i+1);
i=day.indexOf("/");
mon=Integer.parseInt(day.substring(0,i));
day=day.substring(i+1);
yr=Integer.parseInt(day);
leap1=0;
if(mon>2)
leap1=leap(yr);
System.out.println("Enter the Day on 1st January in this year:");
str1=br.readLine().trim();
}
int leap(int p)
{
if(p%100==0 && p%400==0)
return 1;
else if(p%100!=0 && p%4==0)
return 1;
else
return 0;
}
void show ()
{
if (str1.equalsIgnoreCase("Sunday"))
x=1;
else if (str1.equalsIgnoreCase("Monday"))
x=2;
else if (str1.equalsIgnoreCase("Tuesday"))
x=3;
else if (str1.equalsIgnoreCase("Wednesday"))
x=4;
else if (str1.equalsIgnoreCase("Thursday"))
x=5;
else if (str1.equalsIgnoreCase("Friday"))
x=6;
else if (str1.equalsIgnoreCase("Saturday"))
x=7;

for(i=0;i< mon-1;i++)
dayno=dayno+arr[i];
dayno=dayno+leap1;
for(i=0;i< dayno-1;i++)
{
 x++;
 if(x==8)
 x=1;
 }
 System.out.println(day1+ ":");
switch(x)
{
 case 1:
 System.out.println("Sunday");
 break;
 case 2:
 System.out.println("Monday");
 break;
 case 3:
 System.out.println("Tuesday");
 break;
 case 4:
 System.out.println("Wednesday");
 break;
 case 5:
 System.out.println("Thursday");
 break;
 case 6:
 System.out.println("Friday");
 break;
 case 7:
 System.out.println("Saturday");
 break;
}
 }
 }


 Here in this BlueJ program the array ‘arr’ holds the number of days of each month. ‘take()’ function takes the date and day name of 1st January of the specific year. It also breaks the date into day, month and year. If the month is greater than 2 ( Means minimum March), from the end of the body of the function it calls a function ‘leap()’. It returns ‘1’ if the year is leap year other wise returns ‘0’.

Within ‘show()’ function we initialize a variable ‘x’ with 1 if the day name is Sunday, 2 if the day name is Monday and so on. That means in our BlueJ program ‘x’ holds the day number of the 1st January of the specific year. Then using a for loop, count the total number of days from 1st January to the specified date and ‘x’ is utilized to hold the day number of the specific date.

Related Post: BlueJ Programs on Numeric Array
.

26 comments:

  1. there is some code missing in void show() function
    the for statement seems to be incomplete !!

    ReplyDelete
  2. I think you have doubt in these statements


    for(i=0;i<mon-1;i++)
    dayno=dayno+arr[i];
    dayno=dayno+leap1;


    Here in the program, the above for loop has only one statement - 'dayno=dayno+arr[i];', thats why the loop body is not defined. Statement 'dayno=dayno+leap1;' is placed outside the loop body.


    Thanks for going through the program in details.

    ReplyDelete
    Replies
    1. when i complied ok but when i inputtd as23051997 it showed out of bounds exception

      Delete
    2. Please check the program again - the date to be entered in dd/mm/yyyy format.

      Delete
    3. Why p variable is taken??

      Delete
    4. 'int p' is taken as formal argument of 'int leap() function. Year is passed as argument to the function and 'int p' holds the year value to check whether it is leap year or not.

      Delete
  3. sir could you please help me out with this program...
    eg:-
    input:4
    output:4+0=4
    3+1=4
    2+1+1=4
    1+1+1+1=4

    ReplyDelete
  4. Please Check my post on February 22, 2011

    ReplyDelete
  5. sir why do you always post programs written by using functions sir please post programs using " BufferedReader".is"BufferedReader" a class?reply soon

    ReplyDelete
  6. BufferedReader is a class of java.io package.

    ReplyDelete
  7. i want 50 java programs 4 my project.pleade update it

    ReplyDelete
  8. You can collect more than 50 java programs from this blog

    ReplyDelete
    Replies
    1. sir u r very generous,thanx for helping all of us wid every program that we ask for......i wish i had known abt ur blog earlier so that,wid ur help i would have been able to clear my doubts ,m in 12th n so m answering my boards this year but i am really a looser wen it comes to programming but anyways, i hope that ur progs would help me very much ......thanx a lot sir :)

      Delete
  9. sir plz post program on string changing u[pppercase to lowercase using recursive technique

    ReplyDelete
  10. Program posted on 11 January 2012

    ReplyDelete
  11. sir in the above prgrm why u have taken condn in for loop 'i<dayno-1' why not i<dayno

    for(i=0;i< dayno-1;i++)
    {
    x++;
    if(x==8)
    x=1;
    }

    ReplyDelete
    Replies
    1. Our counting is started from '0', that's why it is dayno-1

      Delete
  12. sir what is the significance of import java.io.*??

    ReplyDelete
    Replies
    1. If you are using any class of io package, like BufferedReader, you have to import the package.

      Delete
  13. Sir, Is java.util.Scanner in ISC course?

    ReplyDelete
  14. Sir can you post a program of lucky number..i need it for monday...plz sir...!! :)

    ReplyDelete
    Replies
    1. I don't think lucky number program will come in ISC practical, anyway I will post the program by Monday.

      Delete
  15. Do you think in 2013, the date program is gonna come? I have noticed it alternates every odd year.

    ReplyDelete
  16. Wap to input a number and calculate the date.
    Example: 01011942 output: 1 january,1943

    and wap to input a number and print it in dd/mm/yyyy format

    ReplyDelete
  17. import java.util.*;
    /**
    Write a program to accept a date in the string format dd/mm/yyyy and accept
    the name of the day on 1st of January of the corresponding year. Find the day for the given date.

    Example: INPUT
    Date : 5/7/2001
    Day on 1st January of that year.
    MONDAY
    OUTPUT
    Day on 5/7/2001 : THURSDAY

    */

    public class Question_1
    {
    public static void main(String args[])
    {
    Scanner sc=new Scanner(System.in);
    System.out.println("Enter the date");
    String date=sc.nextLine();
    System.out.println("Enter the day on 1st January of that year");
    String day=sc.next();
    String d=date.substring(0,date.indexOf('/'));
    String m=date.substring(date.indexOf('/')+1,date.lastIndexOf('/'));
    String y=date.substring(date.lastIndexOf('/')+1);
    int d1=Integer.parseInt(d);
    int m1=Integer.parseInt(m);
    int y1=Integer.parseInt(y);
    int arr[]={31,28,31,30,31,30,31,31,30,31,30,31};
    String s[]={"MONDAY","TUESDAY","WEDNESDAY","THURSDAY","FRIDAY","SATURDAY","SUNDAY"};
    if((y1%400==0)||(y1%4==0 && y1%100!=0))
    {
    arr[1]=29;
    }
    int sum=0;
    for(int i=0;i6)
    {
    pos=pos-7;
    }
    pr=s[pos];
    }
    System.out.println("The day is "+pr);
    }
    }

    ReplyDelete

Subscribe via email

Enter your email address:

Delivered by FeedBurner