Java Programs


Due date calculation in a Library


  • The librarian in a library wants an application that will calculate the due date for a book given the issue date. The no. of days in which the book is due can be decided by the librarian at the time of issuing a book. For e.g. If the librarian enters the current date as 14-01-99 and the no of days in which the book is due as 15, then your program should calculate the due date and give the output as 29-01-99.
 Program

import java.util.Scanner;
  
public class Library {

            private static Scanner sc;
            public void duedate(int day,int month,int year) {
                        int due;
                        System.out.print("Enter the no. of due days:");
                        due=sc.nextInt();
                        day=day+due;
                        System.out.print("The due date is ");
                        //making the value of "day" less than or equal to 31 days
                        while(day>31) {
                                    if(month==1||month==3||month==5||month==7||month==8||month==10) {
                                                day=day-31;
                                                month++;
                                    }
                                    else if(month==4||month==6||month==9||month==11) {
                                                day=day-30;
                                                month++;
                                    }
                                    else if(month==12) {
                                                day=day-31;
                                                month=1;
                                    }
                                    else if(month==2) {
                                                if((year%400==0)||(year%4==0&&year%100!=0)) {
                                                            day=day-29;
                                                            month++;
                                                }
                                                else {
                                                            day=day-28;
                                                            month++;
                                                }
                                    }
                        }
                        //Calculating the due date
                        if(month==1||month==3||month==5||month==7||month==8||month==10) {
                                    if(day>31) {
                                                System.out.println(+(day-31)+"-"+(month+1)+"-"+(year));
                                    }
                                    else {
                                                System.out.println(+day+"-"+month+"-"+year);
                                    }
                        }
                        else if(month==4||month==6||month==9||month==11) {
                                    if(day>30) {
                                                System.out.println(+(day-30)+"-"+(month+1)+"-"+(year));
                                    }
                                    else {
                                                System.out.println(+day+"-"+month+"-"+year);
                                    }

                        }
                        else if(month==12) {
                                    if(day>31) {
                                                System.out.println(+(day-31)+"-"+(month-11)+"-"+(year+1));
                                    }
                                    else {
                                                System.out.println(+day+"-"+month+"-"+year);
                                    }

                        }
                        else if(month==2) {
                                    if((year%400==0)||(year%4==0&&year%100!=0)) {
                                                if(day>29) {
                                                            System.out.println(+(day-29)+"-"+(month+1)+"-"+(year));
                                                }
                                                else {
                                                            System.out.println(+day+"-"+month+"-"+year);
                                                }
                                    }
                                    else {
                                                if(day>28) {
                                                            System.out.println(+(day-28)+"-"+(month+1)+"-"+(year));
                                                }
                                                else {
                                                            System.out.println(+day+"-"+month+"-"+year);
                                                }
                                    }
                        }
            }
            public static void main(String[] args) {
                        int day,month,year;
                        String str;
                        System.out.println("Enter the issue date:(DD-MM-YYYY)");
                        sc=new Scanner(System.in);
                        str=sc.next();
                        //Checking whether the date format is valid or not
                        if(str.length()!=10) {
                                    System.out.println("Date format is invalid");
                                    return;
                        }
                        day=Integer.valueOf(str.substring(0, 2));
                        month=Integer.valueOf(str.substring(3, 5));
                        year=Integer.valueOf(str.substring(6, 10));
                        //Checking whether issue date is valid or not
                        if((month==1||month==3||month==5||month==7||month==8||month==10||month==12)&&day>31) {
                                    System.out.println("Invalid date");
                                    return;
                        }
                        else if((month==4||month==6||month==9||month==11)&&day>30) {
                                    System.out.println("Invalid date");
                                    return;
                        }
                        else if(month==2) {
                                    if((year%400==0)||(year%4==0&&year%100!=0)) {
                                                if(day>29) {
                                                System.out.println("Invalid date");
                                                return;
                                                }
                                    }
                                    else if(day>28) {
                                                System.out.println("Invalid date");
                                                return;
                                    }
                        }
                        Library ob=new Library();
                        ob.duedate(day,month,year);
                       
            }

}

Output

Enter the issue date:(DD-MM-YYYY)
30-11-2013
Enter the no. of due days:90
The due date is 28-2-2013

----------------------------------------------------------------------------------------------

Valid Sides of a Triangle

  • Given three numbers, determine whether they can form the sides of a triangle.
 Program

import java.util.Scanner;

public class Triangle {

            private static Scanner sc;
            public static void main(String[] args) {
                        int a,b,c;
                        sc=new Scanner(System.in);
                        System.out.println("Enter three numbers:");
                        a=sc.nextInt();
                        b=sc.nextInt();
                        c=sc.nextInt();
                        if((a<b+c)&&(b<a+c)&&(c<a+b)) {
                                    System.out.println(a+", "+b+" and "+c+" forms the valid sides of a triangle");
                        }
                        else {
                                    System.out.println(a+","+b+" and "+c+" does not form the valid sides of a triangle");
                        }
            }

}

Output

Enter three numbers:
23
30
45
23, 30 and 45 forms the valid sides of a triangle

------------------------------------------------------------------------------------------------

No comments:

Post a Comment