Question

Design an application with a single class and two static methods: method main and method isLeap....

Design an application with a single class and two static methods: method main and method isLeap. Static method isLeap accepts year as integer and returns boolean value true or false depending whether year is leap or not.

  • public static boolean isLeap ( int year )

The year is leap year if it is divisible by 4, but not divisible by 100 , except if it is divisible by 400.

Examples

  • 2003 is not a leap year
  • 2020 is leap year
  • 1700 is not leap year
  • 1600 is leap year.

In the main method, allow user to repeatedly enter any year equal to or after 1582 to be tested whether year is leap or not. Sentinel value of 0 entered by the user should mean that user wants to stop testing. For each year >= 1582 report whether year is leap or not. If user enters year before 1582 inform user that program only tests whether year is leap or not for years >= 1582. Do not invoke method isLeap for year before 1582 , but allow user to continue testing next year(s).

At the end thank user for using the program.

When running the program test each of above 4 years, plus year 1200, and three additional years of your choice before quitting.

in java. I believe I need a while loop in this program as well I know I need If statement

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.util.Scanner;

public class LeapYears {

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

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int year;
        while (true) {
            System.out.print("Enter an year(0 to exit): ");
            year = in.nextInt();
            if (year == 0)
                break;
            if (year >= 1582) {
                if (isLeap(year)) {
                    System.out.println(year + " is a leap year");
                } else {
                    System.out.println(year + " is not a leap year");
                }
            }
        }
        System.out.println("Thanks for using the program!");
    }
}

Add a comment
Know the answer?
Add Answer to:
Design an application with a single class and two static methods: method main and method isLeap....
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • CIS22A Homework 6 Topic: Chapter 4 & 5 Purpose: Determine if a year is a leap...

    CIS22A Homework 6 Topic: Chapter 4 & 5 Purpose: Determine if a year is a leap year Create a new C++ project titled “HW 6” in the CodeBlocks IDE. Use the “Console Application” project option. Over time, the calendar we use has changed a lot. Currently, we use a system called the Gregorian Calendar and it was introduced in 1582. The algorithm to determine if a year is a leap year is as follows: Every year that is exactly divisible...

  • Java Program 4: Methods Purpose: To practice using both void and value-returning methods and understand when,...

    Java Program 4: Methods Purpose: To practice using both void and value-returning methods and understand when, where, and how to use them. The following problem employs the use of both void and value-returning methods: (Be sure to start with an algorithm to help you organize your ideas and provide a blueprint as to how to approach the problem. Think about what kind of return type your method will need. Carefully examine and follow ALL the program specifications (for full credit)....

  • Write a class named MonthDays. The class’s constructor should accept two arguments: l An integer for...

    Write a class named MonthDays. The class’s constructor should accept two arguments: l An integer for the month (1 = January, 2 February, etc). l An integer for the year The class should have a method named getNumberOfDays that returns the number of days in the specified month. The method should use the following criteria to identify leap years: 1. Determine whether the year is divisible by 100. If it is, then it is a leap year if and if...

  • Utilizing the class diagram above, implement the voting system. You will need a main method to...

    Utilizing the class diagram above, implement the voting system. You will need a main method to allow a voter to register and vote (you need to take in user input). You need to assume getters, setters, constructors, and toString methods; and you need to take in the user input and validate it as well. Comment the code well and submit screenshot testing within your PDF. Hint: you can research regular expressions for the validation functions. Just need help with the...

  • Take your MonthDay class from Assignment #6 and modify it to throw an IllegalArgumentException when the user of the class attempts to pass an invalid value for month and year. Make sure you have your...

    Take your MonthDay class from Assignment #6 and modify it to throw an IllegalArgumentException when the user of the class attempts to pass an invalid value for month and year. Make sure you have your main method(function) in a separate source file demonstrating use of the try/catch block to catch the IllegalArgumentException that the MonthDay class may throw in the event of an invalid month, or year. class MonthDays {    private int month;    private int year;    public MonthDays(int aMonth, int...

  • Java Programming USE ONLY 1 CLASS. Class main is all that is required 1. Ask the...

    Java Programming USE ONLY 1 CLASS. Class main is all that is required 1. Ask the user for a year as an input (positive integer - should be between 1500 and 2020, and can include 1500 and 2020). 2. If the year input is not between 1500 and 2020, do not check for leap year. Instead, print that this year cannot be checked. 3. If the input year provided by the user is a leap year, print that year is...

  • leap year C++

    the instructions are: A year in the modern Gregorian Calendar consists of 365 days. In reality, the earth takes longer to rotate around the sun. To account for the difference in time, every 4 years, a leap year takes place. A leap year is when a year has 366 days: An extra day, February 29th. The requirements for a given year to be a leap year are:1) The year must be divisible by 42) If the year is a century year...

  • Write code in static void main method class, which creates a boolean array called flags, size...

    Write code in static void main method class, which creates a boolean array called flags, size 10, and using a for loop sets each element to alternating values (true at index zero, false at index one Write code in a static void main method class, which creates a float array called sales, size 5, uses an initializer list to instantiate its elements with positive values having two decimal places (example 7.25), and using a for loop reads those values of...

  • Need Help....Java MyProgram.java: public class MyProgram { public static void main(String[] args) { } } House.java:...

    Need Help....Java MyProgram.java: public class MyProgram { public static void main(String[] args) { } } House.java: public class House { private String address; private double cost; private double interst; private int mortgageTime; private double monthPayment; } As you consider your life before you, one thing you may consider is buying a house in the future. In this assignment, you will explore concepts of this amazing opportunity and create a program that may be of benefit to you in the future!...

  • Java CSC252   Programming II    Static Methods, Enums, Constants, Random Rock Paper Scissors Write a program that...

    Java CSC252   Programming II    Static Methods, Enums, Constants, Random Rock Paper Scissors Write a program that allows the user to play "Rock, Paper, Scissors". Write the RPS class. All code should be in one file. main() will be part of the RPS class, fairly small, and contain only a loop that asks the user if they want to play "Rock, Paper, Scissors". If they say yes, it calls the static method play() of the RPS class. If not, the program...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT