Question

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 a leap year. Otherwise, print that the year is not a leap year.

how to check if its a leap year

There are a few things to check:

  1. First, if the year can be divided by 4 evenly, it is a leap year.
  2. HOWEVER! If the year can be divided by 100 evenly, it is NOT a leap year! (If this occurs, check if the year can divided by 400 evenly. If it is divisible by both 100 evenly, and 400 evenly, it will be considered a leap year.) You'll need to do this to handle centurial years, i.e., years ending in 00, e.g., 1900, 1800, 1700, 2000 etc.

Sample output

The output should look like this:

  • What year do you want to test? (make sure it's between 1500 and 2020): 1400

This year cannot be checked. Try again!

  • What year do you want to test? (make sure it's between 1500 and 2020): 2021

   This year cannot be checked. Try again!

  • What year do you want to test? (make sure it's between 1500 and 2020): 2016
  • Yes, 2016 is a leap year!

  • What year do you want to test? (make sure it's between 1500 and 2020): 1900
  • Nope, 1900 is NOT a leap year!

0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE:

import java.util.Scanner;

class Main {

    public static void main(String args[]) {

        Scanner sc = new Scanner(System.in);

        System.out.print("What year do you want to test? (make sure it's between 1500 and 2020): ");

        int year = sc.nextInt();

        if (year > 2020 || year < 1500) {

            System.out.println("This year cannot be checked. Try again!");

        } else {

            boolean leap = false;

            if (year % 4 == 0) {

                if (year % 100 == 0) {

                    if (year % 400 == 0)

                        leap = true;

                    else

                        leap = false;

                } else

                    leap = true;

            } else

                leap = false;

            if (leap)

                System.out.println("Yes, " + year + " is a leap year!");

            else

                System.out.println("Nope, " + year + " is NOT a leap year!");

        }

    }

}

OUTPUT:

What year do you want to test? (make sure it's between 1500 and 2020): 1400

This year cannot be checked. Try again!

What year do you want to test? (make sure it's between 1500 and 2020): 2021

This year cannot be checked. Try again!

What year do you want to test? (make sure it's between 1500 and 2020): 2016

Yes, 2016 is a leap year!

What year do you want to test? (make sure it's between 1500 and 2020): 1900

Nope, 1900 is NOT a leap year!

Add a comment
Know the answer?
Add Answer to:
Java Programming USE ONLY 1 CLASS. Class main is all that is required 1. Ask the...
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
  • This question deals with extending already existing Java code via a while loop. Ask the user...

    This question deals with extending already existing Java code via a while loop. Ask the user how many year inputs he wants to check - an integer. Assume that the user always gives an input which is between 1 and 5 (inclusive). Next, ask the user for K number of year inputs where K = the number user has given as input before (inside a while loop). Check for each year input whether that year is a leap year or...

  • Instructions ASSIGNMENT 2 is at bottom of the question. Very simple program and these intructions are...

    Instructions ASSIGNMENT 2 is at bottom of the question. Very simple program and these intructions are only to modify it slightly, should only take about 10 minutes says my proffessor. Your file should be named “Main.java”. Ask the user how many year inputs he wants to check - an integer. Assume that the user always gives an input which is between 1 and 5 (inclusive). Next, ask the user for K number of year inputs where K = the number...

  • JAVA programing Question 1 (Date Class):                                   &nbsp

    JAVA programing Question 1 (Date Class):                                                                                                     5 Points Create a class Date with the day, month and year fields (attributes). Provide constructors that: A constructor that takes three input arguments to initialize the day, month and year fields. Note 1: Make sure that the initialization values for the day, month and year fields are valid where the day must be between 1 and 31 inclusive, the month between 1 and 12 inclusive and the year a positive number. Note 2:...

  • Python 3 Can anyone make my Python code more readable, by breaking up the code into...

    Python 3 Can anyone make my Python code more readable, by breaking up the code into smaller chunks using functions. If a piece of code tries to do several things, it should be broken up into several different functions. print("chatbot: Hello, I am chatbot. What is your name?") name = input("") print("chatbot: Nice to meet you "+name+". Tell me now, what is your age?") age = int(input("")) if age >= 16 and age <= 25: print("We are the same age!...

  • Required in JAVA language Write a program that enables a user to play number guessing games....

    Required in JAVA language Write a program that enables a user to play number guessing games. The following is a nutshell description of a number guessing game. + a random number is generated + loop prompting the user to enter guesses until the user guesses the number or hits the maximum number of allowed guesses or enters the "quit" sentinel value The rest of this specification documents number guessing games in more detail. The documentation uses manifest constants that can...

  • 12p I need help this is Python EXCEPTIONS: It's easier to ask forgiveness than permission. Try...

    12p I need help this is Python EXCEPTIONS: It's easier to ask forgiveness than permission. Try the code and catch the errors. The other paradigm is 'Look before you leap' which means test conditions to avoid errors. This can cause race conditions. 1.Write the output of the code here: class MyError(Exception): pass def notZero(num): if num == 0: raise MyError def run(f): try: exec(f) except TypeError: print("Wrong type, Programmer error") except ValueError: print("We value only integers.") except Zero Division Error:...

  •    Lab/HW 3   Write a program that reads in the following data, all entered on one...

       Lab/HW 3   Write a program that reads in the following data, all entered on one line with at least one space separating them: a) an integer representing a month b) an integer representing the day of the month c) an integer representing a year 1. Check that the month is between 1 and 12. If it’s not print an error message. 2. Day cannot be 0 or less nor can it be more than 31 for all months. Print...

  • Hi, this is an intro to C Programming Class. Please do not use arrays, functions, math.h,...

    Hi, this is an intro to C Programming Class. Please do not use arrays, functions, math.h, or struct to store any data except to declare strings char strVar[20]. Furthermore, can you please do a good check to make sure the final program does not have any problems because when I copy and paste them the app I use says there are some problems and it won't run the program. So if you can double check to make sure that everything...

  • FINDING THE LEAP YEARS (14 points) From Wikipedia, "A leap year (also known as an intercalary...

    FINDING THE LEAP YEARS (14 points) From Wikipedia, "A leap year (also known as an intercalary year or bissextile year) is a calendar year containing one additional day (or, in the case of lunisolar calendars, a month) added to keep the calendar year synchronized with the astronomical or seasonal year." In the Gregorian calendar, which is in use today, to determine leap year: A year is a leap year if it is divisible by 400 Or it is divisible by...

  • c++ please :) First, ask the user to enter a year (4-digit) and what week day...

    c++ please :) First, ask the user to enter a year (4-digit) and what week day does this year starts with (String) For example, the year 2018 starts with the week day "Friday". In this case, the calendar should start from January 1 which is Friday, 2018 Your program should produce a formatted calendar for the specified year and there should be a blank line after each month. Moreover, the month and the year should be centered over each month....

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