Question

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 one static class method (isLeapYear). The method isLeapYear should take one integer argument and will return a boolean. The method should compare the input (a year) with the qualifications for determining whether a given year is a leap year or not, and return the appropriate true or false value on completing these checks. The rule for leap years is as follows: if the year is evenly divisible by 4, it is a leap year, except in the case where it is also evenly divisible by 100 but not evenly divisible by 400. Several example method calls appear below.

LeapYear.isLeapYear(2016) // returns true, divisible evenly by 4

LeapYear.isLeapYear(2015) // returns false, not divisible evenly by 4

LeapYear.isLeapYear(1900) // returns false,

LeapYear.isLeapYear(2000) // returns true, divisible evenly by 4 and 100, but also by 400

IV. Complete the definition for main. Your program should create a new Scanner object, prompt the user to type in a year, and then collect an integer. The main method should then call the static method isLeapYear in order to determine whether or not the year in question fits the characteristics of a leap year. From this point, the results of the method call should be used to print a statement that expresses the results. For example, if the user enters 2015 as input: “The year 2015 is not a leap year.”

Homework Answers

Answer #1
import java.util.Scanner;

public class LeapYear {

    public static boolean isLeapYear(int year) {
        return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter an year: ");
        int year = in.nextInt();
        if (isLeapYear(year)) {
            System.out.println("The year " + year + " is a leap year.");
        } else {
            System.out.println("The year " + year + " is not a leap year.");
        }
    }
}

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
/* This program should check if the given integer number is prime. Reminder, an integer number...
/* This program should check if the given integer number is prime. Reminder, an integer number greater than 1 is prime if it divisible only by itself and by 1. In other words a prime number divided by any other natural number (besides 1 and itself) will have a non-zero remainder. Your task: Write a method called checkPrime(n) that will take an integer greater than 1 as an input, and return true if that integer is prime; otherwise, it should...
Need this program in Java Develop (using C++ or Java language) the software application described below....
Need this program in Java Develop (using C++ or Java language) the software application described below. Write a program that prints the day number of the year, given the date is in the form month-day-year. For example, if the input is 1-1-09, the day number is 1; if the input is 12-25-09, the day number is 359. The program should check for a leap year. A year is a leap year if it is divisible by 4 but not divisible...
Script #2 – Leap Year Description:  Given a year, report if it is a leap year. Purpose:...
Script #2 – Leap Year Description:  Given a year, report if it is a leap year. Purpose: Learn the use of if statements. Instructions:  Complete this assignment to make sure that you know how to use if statements in Powershell. A leap year in the Gregorian calendar occurs: on every year that is evenly divisible by 4 except every year that is evenly divisible by 100 unless the year is also evenly divisible by 400 Write a script that prompts a user...
is there anything wrong with the solution. the question are from java course Write a main...
is there anything wrong with the solution. the question are from java course Write a main method that will request the user to enter Strings using a JOptionPane input dialog. The method should continue accepting strings until the user types “STOP”.       Then, using a JOptionPane message dialog, tell the user how many of the strings begin and end with a digit. Answer: import javax.swing.*; public class IsAllLetters {     public static void main(String[] args) {         String input;         int count =...
We may expand the general strategy established in the prior question to determine whether a given...
We may expand the general strategy established in the prior question to determine whether a given number is divisible by another. Modify the program from the prior question or write one from scratch which determines whether 12345 is divisible by 15. public class Main {   public static void main( String[] args ) {     int myNumber = 12345; //15     int divisor = 15; //4     System.out.print( "(" + myNumber + ") is " );     if( myNumber%divisor==0) {       // Do nothing     }     else...
4. Programming Problem: In this problem we will keep the Mechanism interface and the two classes...
4. Programming Problem: In this problem we will keep the Mechanism interface and the two classes Car and Computer from the previous problem. In addition, we will add a new class called Mechanic. We will also create a new TestMechanic class that will contain the main method. Create a class called Mechanic with following instance variable and constants: private int cost; public static int TEST_PRICE = 10; public static int REPAIR_PRICE = 50; Have a default constructor that will set...
Queues, Lists, and Stacks Assignment: Write client methods to do the following Assume that there exists...
Queues, Lists, and Stacks Assignment: Write client methods to do the following Assume that there exists in main a list   declared as : List<Integer> aList   =   new   List<Integer>(50); Write a client method called bubbleSort which given a list as a parameter uses the bubble sort algorithm to sort the contents of the list. YOU MAY ASSUME that there exists a method called swap (with the following signature) which swaps the contents of two elements: // swap public static void swap(List<Integer>...
Please answer in JAVA IDS 401 Assignment 4 Deadline In order to receive full credit, this...
Please answer in JAVA IDS 401 Assignment 4 Deadline In order to receive full credit, this assignment must be submitted by the deadline on Blackboard. Submitting your assignment early is recommended, in case problems arise with the submission process. Late submissions will be accepted (but penalized 10pts for each day late) up to one week after the submission deadline. After that, assignments will not be accepted. Assignment The object of this assignment is to construct a mini-banking system that helps...
Instructions PowerShell Assignment #1 Objective: Learn basic Powershell scripting in Windows Setup: A Windows computer with...
Instructions PowerShell Assignment #1 Objective: Learn basic Powershell scripting in Windows Setup: A Windows computer with the latest Powershell environment installed Script #1 – Hello World! Description:  The classical introductory exercise: Just say "Hello, World!". Purpose: A "Hello, World!" program is traditionally used to introduce novice programmers to a programming language. It is also to make sure that the interpreter is installed correctly, and that the user understands how to use it. Instructions:  Complete this assignment to make sure that you know...
Please answer quickly! Thank you. Java: Write a non-static method to be added to a class...
Please answer quickly! Thank you. Java: Write a non-static method to be added to a class that implements a singly linked list of integers. The method is called make_partition() and takes an integer x as input. The method should modify this list by moving all the elements less than x to the front of the list, and all the elements greater than x to the back of the list. It is not the important for the elements to be kept...