29, ఫిబ్రవరి 2012, బుధవారం

. LEAPYEAR - ONE OF THE BIG ISSUES IN THE Y2K PROBLEM AND HERE I AM INTRODUCING DIFFERENT TECHNIQUES IN JAVA FOR THE FINDING THE GIVEN YEAR IS LEAP OR NOT

 code snippet
public class lr {

    public static void main(String[] args) {
//u can add here any input mechanisim like Scanner or concole or DataInputStream or 
//BufferedReader to make this program as interactive   
   
   
   
   
   
   
   
   
  

DataInputStream keyboard = new DataInputStream (System.in);
       System.out.println ("Calculator Ready: enter a value");
          year = keyboard.readInt ();
int year  = 2011;
        if(year%400==0){
            System.out.println("is a leap year");
        }else if(year%100==0){
            System.out.println("is not a leap year");
        }else if(year%4==0){
            System.out.println("is a leap year");
        }else{
            System.out.println("is not a leap year");
        }
    }
}


code snippet
The following program uses the java.util.Calendar class and its methods to 
determine whether a year is leap year or not


import java.util.Calendar;

public class JavaCalendarLeapYearExample {
    public static void main(String[] args) {
        // this variable holds the value of year in question
        int year = 2011;
        // gets the instance of Calendar
        Calendar calendar = Calendar.getInstance();
        // sets the calendar to 31st December of the Year
        calendar.set(year,Calendar.DECEMBER,31);
        // check the no of days of the year
        if(calendar.get(Calendar.DAY_OF_YEAR)==366)
            System.out.println("is a leap year");
        else
            System.out.println("is not a leap year");
    }
}




 
function isLeapYear (year):
 if ((year modulo 4 is 0) and (year modulo 100 is not 0)) or (year modulo 400 is 0)
  then true
 else false
  
This program ignores changes in the Gregorian calendar and only
 applies correctly to dates after 1582.
//DOC  @author Mr.RamuDayinaboyin @author free lancer  @version 9/17/2011
 import java.io.*;
public class LeapYear {
    public static void main (String[] args) {

 // The year to check for leapiness.  
int Yr;

 // Get the year. For now we'll assume they enter a reasonable value. 
System.out.print("Enter the year: ");
 Yr = Console.in.readInt(); 
// Check for a 2 digit year and adjust the year using a windowing technique. 
If the date has more than 2 digits we'll assume it is a complete year. 
if (Yr < 100) {
 // If the year is greater than 40 assume it is from 1900's.  If the year is less 
than 40 assume it is from 2000's.      
if (Yr > 40) {
  Yr = Yr + 1900;
     }
     else {
  Yr = Yr + 2000;
     }
 }
 // Is theYear Divisible by 4?  
if (Yr % 4 == 0) {
     // Is theYear Divisible by 4 but not 100?      
if (Yr % 100 != 0) {
  System.out.println(Yr + " is a leap year.");
     }
     // Is theYear Divisible by 4 and 100 and 400?      
else if (Yr % 400 == 0) {
  System.out.println(Yr + " is a leap year.");}
// It is Divisible by 4 and 100 but not 400!      
else {
  System.out.println(Yr + " is not a leap year.");}}
// It is not divisible by 4. 
else {
     System.out.println(Yr + " is not a leap year.");}}}
 
 
 
 
 
Another Logic   
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.logging.Level;
import java.util.logging.Logger;
public class LeapYrDemo {
public static void main(String[] args) {
        try {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter num");
int yr = br.read();
int num = yr;
if(num/100== 0){
    if(num/400==0){
        System.out.println("Leap year");
        }
    else
        System.out.println("not leap yr");
}
else if(num/4==0){
    System.out.println("leap yr");
}
else
    System.out.println("Not a leap yr");
} catch (IOException ex) {
Logger.getLogger(LeapYrDemo.class.getName()).log(Level.SEVERE, null, ex);
        }
}
}
The output for the above program is
Enter num2012
Leap year
BUILD SUCCESSFUL (total time: 2 seconds)
but in the above code some bug is inside whenever i am free i will rectify it 
 
another snippet for the leap year logic is by using assertions  put this code in
a class  
public static boolean isleap(int y)
{
assert y>=1583
 return((y%4==0)&&(y%100!=0)) ||(year % 400==0);
} 
call like below
assertTrue(isleap(2012)); 

కామెంట్‌లు లేవు:

కామెంట్‌ను పోస్ట్ చేయండి