Question

Problem 2: Leap Years Print out all the leap years between a given starting year and...

Problem 2: Leap Years Print out all the leap years between a given starting year and ending year. If the starting year is greater than the ending year or one of the inputs is negative, an appropriate error message should be printed. .java = LeapYears.java Examples (a) printLeapYears(2000, 2010) prints out: 2000 2004 2008 (b) printLeapYears(2000, 2000) prints out: 2000 (c) printLeapYears(

Homework Answers

Answer #1
public class LeapYears {
    public static boolean isLeap(int year){
        if(year % 4 == 0 && year % 100 != 0){
            return true;
        }
        else if(year % 400 == 0){
            return true;
        }
        return false;
    }

    public static void printLeapYears(int year1, int year2){
        for(int i = year1;i<=year2;i++){
            if(isLeap(i)){
                System.out.print(i+" ");
            }
        }
        System.out.println();
    }

    public static void main(String[] args) {
        // Testing
        printLeapYears(2000, 2010);
        printLeapYears(2000, 2000);
    }
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Part1. Write a program that reads an integer value from the user representing a year. The...
Part1. Write a program that reads an integer value from the user representing a year. The purpose of the program is to determine if the year is a leap year (has 29 days in February) in the Gregorian calendar. A year is a leap year if it is divisible by 4, unless it is also divisible by 100 but not 400. For example, the year 2003 is not a leap year, but 2004 is. The year 1900 is not a...
write a script named print_lines.sh that uses head and tail together to print out a specific...
write a script named print_lines.sh that uses head and tail together to print out a specific set of lines from a file. The script should take three arguments: the line number to start at, the line number to stop at, and the file to use. Here's an example run: [user@localhost ~]$ print_lines.sh 7 10 /etc/passwd shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt mail:x:8:12:mail:/var/spool/mail:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin [user@localhost ~]$ In this example, the script prints line 7 through 10 (inclusive) of the /etc/passwd file. Your script must do...
In this Java programming assignment, you will practice using selection statements to determine whether a given...
In this Java programming assignment, you will practice using selection statements to determine whether a given year in the past or future qualifies as a “Leap Year”. I. Design a class called LeapYear in a file called LeapYear.java. This class will hold the main method and the class method that we will write in this assignment. II. Write an empty public static void main(String[] args) method. This method should appear inside the curly braces of the LeapYear class. III. Write...
Problem a (PA5a.java) Write a program that deals with inflation, which is essentially the rising cost...
Problem a (PA5a.java) Write a program that deals with inflation, which is essentially the rising cost of general goods over time. That is, the price of goods, such as a packet of peanuts, goes up as time goes by. So, you will write a program to gauge the expected cost of an item in a specified number of years. The program asks for the cost of the item, the number of years, and the rate of inflation. The output is...
Python Programming Assignment 3 For this assignment you have been given a starting file to use...
Python Programming Assignment 3 For this assignment you have been given a starting file to use for the assignment. Some code is already in this file, and it provides functionality you’ll be using to complete this assignment. Your code needs to go in a specific place, marked #### Your Code Here You code must go under this comment, and you should not modify the other parts of the file, except to add a block comment at the top with your...
Year   Years.since.1985   Fires   Acres   Acres/Fire 2015   32   68151   10125149   148.56934 2014   31   63312   3595613   56.791967 2
Year   Years.since.1985   Fires   Acres   Acres/Fire 2015   32   68151   10125149   148.56934 2014   31   63312   3595613   56.791967 2013   30   47579   4319546   90.786818 2012   29   67774   9326238   137.6079 2011   28   74126   8711367   117.52107 2010   27   71971   3422724   47.556988 2009   26   78792   5921786   75.157199 2008   25   78979   5292468   67.011079 2007   24   85705   9328045   108.83898 2006   23   96385   9873745   102.44068 2005   22   66753   8689389   130.17226 2004   21   65461   8097880   123.70541 2003   20   63629   3960842   62.249006 2002   19   73457   7184712   97.808405 2001   18   84079   3570911   42.470902...
You will write a program that loops until the user selects 0 to exit. In the...
You will write a program that loops until the user selects 0 to exit. In the loop the user interactively selects a menu choice to compress or decompress a file. There are three menu options: Option 0: allows the user to exit the program. Option 1: allows the user to compress the specified input file and store the result in an output file. Option 2: allows the user to decompress the specified input file and store the result in an...
Part A. Input Validation (Name your C program yourLastName_yourFirstName_Lab4a.c) 1. Place the code you developed in...
Part A. Input Validation (Name your C program yourLastName_yourFirstName_Lab4a.c) 1. Place the code you developed in Lab 2 to obtain a diameter value from the user and compute the volume of a sphere (we assumed that to be the shape of a balloon) in a new program, and implement the following restriction on the user’s input: the user should enter a value for the diameter which is at least 8 inches but not larger than 60 inches. Using an if-else...
I did already posted this question before, I did get the answer but i am not...
I did already posted this question before, I did get the answer but i am not satisfied with the answer i did the code as a solution not the description as my solution, so i am reposting this question again. Please send me the code as my solution not the description In this project, build a simple Unix shell. The shell is the heart of the command-line interface, and thus is central to the Unix/C programming environment. Mastering use of...
STRICT DOWNVOTE IF NOT DONE FULLY, WILL REPORT ALSO IF COPY PASTED OR MODIFIED ANSWER Develop...
STRICT DOWNVOTE IF NOT DONE FULLY, WILL REPORT ALSO IF COPY PASTED OR MODIFIED ANSWER Develop a class, using templates, to provide functionality for a set of recursive functions. The functions specified as recursive must be written recursively (not iterativly). The UML class specifications are provided below. A main will be provided. Additionally, a make file will need to be developed and submitted. ● Recursion Set Class The recursion set template class will implement the template functions. recursionSet -length: int...