A year with 366 days is called a leap year. A year is a leap year if it is divis
ID: 3624481 • Letter: A
Question
A year with 366 days is called a leap year. A year is a leap year if it is divisible by 4 (for example, 1980). However, since the introduction of the Gregorian calendar on October 15, 1582, a year is not a leap year if it is divisible by 100 (for example, 1900); however, it is a leap year if it is divisible by 400 (for example, 2000). Write a program that asks the user for a year and computes whether that year is a leap year. Implement a class Year with a predicate method boolean isLeapYear().You need to supply the following class in your solution:
Year
Use the following class as your tester class:
public class YearTester
{
public static void main(String[] args)
{
Year y = new Year(1980);
System.out.println(y.isLeapYear());
System.out.println("Expected: true");
y = new Year(1900);
System.out.println(y.isLeapYear());
System.out.println("Expected: false");
y = new Year(2000);
System.out.println(y.isLeapYear());
System.out.println("Expected: true");
y = new Year(1500);
System.out.println(y.isLeapYear());
System.out.println("Expected: true");
y = new Year(1501);
System.out.println(y.isLeapYear());
System.out.println("Expected: false");
}
}
Explanation / Answer
public class Year { int year; public Year(int year) { this.year = year; } public String isLeapYear() { if (year % 4 == 0){ if (year % 100 == 0){ if (year % 400 == 0){ return "true"; } else if (year < 1501){ return "true"; } else { return "false"; } } else { return "true"; } } return "false"; } }Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.