Question:
I wrote a leap year program and need a little help with it?
2010-10-22 07:25:43 UTC
A year with 366 days is callled a leap year. A year is a leap year if it divisible by 4(for example, 1980). However, since the intoduction of the Gregorian calendar on october 15, 1582, a year is not a leap year if it divisible by 100(for example, 1900); however, it is a leapyear if it is divisible by 400(for example, 2000). write a program that asks the user for a year and compiutes whether that year is a leap year. write a program that asks the user for a year and computes whether that year is a leap
year.
Implement a class Year with the predicate method boolean isLeapyear().


public class Leapyear
{
private int Year;
private boolean isLeapyear;


/**
Constructs no Leapyear
*/

public Leapyear()
{
}

/**
Set the year.
@param the newYear of the Leapyear.
*/

public void setYear(int newYear)
{
Year = newYear;
}

/**
return the Leapyear
@return Year
*/
public int getYear()
{
return Year;
}
/**
Method for proving if it is a leap year or not
@return isLeapyear if it is leap year or not.
*/
public boolean isLeapyear(int Year)
{
if (Year % 4!=0)
{isLeapyear = false;
}

if((Year % 400 != 0 ) && (Year % 100 == 0))
{
isLeapyear = false;
}
else
{ isLeapyear = true;
}
}
return isLeapyear;
}
this is class that i created and i still dont know how to create the tester..
Im a heading to this. But still lost . Can you give me a feedback on what should be done inorder to run the program.

import java.util.Scanner;
public class Year
{
public static void main( String[] args);

int Year;
boolean isLeapyear;

System.out.print("Enter the year: ");

Year theLeapyear = new Year();
theLeapyear.setYear(2005);
theLeapyear.getisLeapyear();



System.out.println("it is a leapyear");
System.out.println("its is not a leapyear");

}
}
Three answers:
Gardner
2010-10-22 08:13:50 UTC
To test for leap year



1 If the year is evenly divisible by 4, go to step 2. Otherwise, go to step 5.

2 If the year is evenly divisible by 100, go to step 3. Otherwise, go to step 4.

3 If the year is evenly divisible by 400, go to step 4. Otherwise, go to step 5.

4 The year is a leap year (it has 366 days).

5 The year is not a leap year (it has 365 days).
Lucien
2010-10-22 14:43:09 UTC
I won't bother with writing code, but to test, your main method must do the following :



1 - create a Year object (let's call it testYear)

2 - get user input and assign the value for testYear

3 - check if testYear is a leap year

4 - give output according to 3.



I didn't check your class (which should be called Year, not LeapYear, by the way), but your main should look something like this :



Year testYear;



System.out.println("Give a year number to test :");

BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

String lineIn = in.readLine();



testYear.setYear(lineIn.toInteger());



if (testYear.isLeapYear()) {

System.out.println("Leap year");

}

else {

System.out.println("Not a leap year");

}



hope this puts you on the right track. Don't use this code as is, I didn't check it...
deonejuan
2010-10-22 16:04:21 UTC
here is the algerbra math



boolean isLeap( int yyyy ) {

return ( (yyyy & 3) == 0 && yyyy % 100 != 0 || yyyy % 400 == 0 );

}



//>----------------------

import java.util.Scanner;

public class YearCheckLeap

{

public static void main( String[] args);



Scanner sc = new Scanner( System.in );

System.out.print("Enter the year: => ");

String input = sc.nextLine();



int year = Integer.parseInt( input );



if( isLeap( year ) {

System.out.println("it is a leapyear");

} else {

System.out.println("its is not a leapyear");

}

} // end main()



private static boolean isLeap( int yyyy ) {

return ( (yyyy & 3) == 0 && yyyy % 100 != 0 || yyyy % 400 == 0 );

} // end method isLeap()

}// end YearCheckLeap class


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...