Question

In: Computer Science

This is just a very basic program/problem in Java I'm looking for some second opinions on....

This is just a very basic program/problem in Java I'm looking for some second opinions on.

1. Ask the user for a year input (positive integer - should be between 1500 and 2019, inclusive).

2. If the year input is not between 1500 and 2019, do not check for leap year, rather print that this year cannot be checked.

3. Take a year input from the user (should be between 1500 and 2019)

4. If the input year is a leap year, print that year is a leap year; otherwise, print that the year is not a leap year.

Solutions

Expert Solution

Dear Student ,

As per the requirement submitted above , kindly find the below solution.

Here a new java program with name "LeapYear.java" is created, which contains following code.

LeapYear.java :

import java.util.*;//import package
//Java class
public class LeapYear {
   // entry point , main() method
   public static void main(String[] args) {
       // creating object of Scanner class
       Scanner sc = new Scanner(System.in);
       // asking user to enter year
       System.out.print("Enter year between 1500 and 2019 : ");
       int year = sc.nextInt();// reading year
       // checking year
       if (year > 1500 && year < 2019) {
           // checking leap year
           if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) {
               // print message when year is leap year
               System.out.println(year + " is leap year.");
           } else {
               // print message when year is not leap year
               System.out.println(year + " is not leap year.");
           }
       } else {
           // print message if invalid year is entered
System.out.println("Year cannot be checked.Enter year between 1500 and 2019");
       }

   }

}

======================================================

Output : Compile and Run LeapYear.java to get the screen as shown below

Screen 1 :LeapYear.java, Screen when year is not in the range

Screen 2 :LeapYear.java, Screen showing when year is leap

Screen 3 :LeapYear.java, Screen showing when year is not leap

NOTE : PLEASE FEEL FREE TO PROVIDE FEEDBACK ABOUT THE SOLUTION.


Related Solutions

Have a pretty good idea on how to solve this program, I'm just not very accustomed...
Have a pretty good idea on how to solve this program, I'm just not very accustomed to formatting well in Java for these types of problems. In java, you have full control of what's printed, I just need to get better at it, so I thought I'd post it here to receive an answer to learn from. Thanks for everything guys. This program must use the Exclusive OR Operator ^ in java, it must also use a while loop. The...
I'm trying to write a feet to meters and centimeters program and I'm looking for a...
I'm trying to write a feet to meters and centimeters program and I'm looking for a little help so I can see how close I got. It should do the following: Write a convertToMetric class that has the following field: standard - holds a double, standard length value in feet. The class should have the following methods: Constructor - that accepts a length in feet (as a double) and stores it in the standard field. setStandard - accepts a standard...
Basic JAVA Question When we try to run this program, a problem occurs. What is this...
Basic JAVA Question When we try to run this program, a problem occurs. What is this problem called? And why does this problem occur here? public class Main {    public static void main(String[] args) {    PhoneNumber pn = new PhoneNumber();        System.out.println(pn.isTollFree());    } } class PhoneNumber { String number;    public boolean isTollFree() { return number.charAt(1) == '8'; } }
We are doing a lab on Grignard Reactions, i'm just looking to verify the system by...
We are doing a lab on Grignard Reactions, i'm just looking to verify the system by which to find this information: Add about 2 mmol Mg powder, recording the mass to the nearest milligram. In this experiment, magnesium will be the limiting reactant because a 5% excess of bromobenzene will be used. Using your mass of Mg, determine the mass of bromobenzene to be used, being sure to calculate a 5% molar excess.
I'm just looking for a better understanding of the process of what happens in each part...
I'm just looking for a better understanding of the process of what happens in each part that follows. What information should I include to help with understanding the question? In Part A, I'm asking about how a neutrophil chasing a bacterium detects and moves towards the bacterium. Then, how does the neutrophil change its course. Processes that I need help understanding and would like included in the explanation if possible are listed after the question. Part B asks how a...
(Intro/Basic) JAVA Write a small program that gets some numbers as command-line arguments and finds the...
(Intro/Basic) JAVA Write a small program that gets some numbers as command-line arguments and finds the maximum of those numbers. You can assume that the user will provide some command-line arguments when running the program (so you don’t have to worry about what to do if the user doesn’t provide any arguments -- that won’t happen). Also, you can assume that all the command line arguments will be floating-point numbers, i.e., numbers with a decimal point, like 2.95.
Needs to be in basic JAVA Write a program that does basic encrypting of text. You...
Needs to be in basic JAVA Write a program that does basic encrypting of text. You will ask the user the filename of a text file that contains a few sentences of text. You will read this text file one character at a time, and change each letter to another one and write out to an output file. The conversion should be done a -> b, b->c , … z->a, A->B, B->C, …. Z->A. This means just shift each letter...
Write the program in java Write a program that does basic encrypting of text. You will...
Write the program in java Write a program that does basic encrypting of text. You will ask the user the filename of a text file which contains a few sentences of text. You will read this text file one character at a time, and change each letter to another one and write out to an output file. The conversion should be done a -> b, b->c , … z->a, A->B, B->C, …. Z->A. This means just shift each letter by...
Making a program in Python with the following code types. I'm very confused and would love...
Making a program in Python with the following code types. I'm very confused and would love the actual code that creates this prompt and a brief explanation. Thanks! A comment on the top line of your program containing your name. A comment on the second line containing your section number. A comment on the third line containing the date. A comment on the fourth line containing your email address. A comment with the lab number and purpose of this lab....
In the second task, you will write a Java program that validates an input to see...
In the second task, you will write a Java program that validates an input to see whether it matches a specified pattern. You will have to read a bit about regular expressions for this Lab. 1. Prompt the user to enter the email address. 2. Read the user input as a String. 3. A regular expression that specifies the pattern for an email address is given by “[a-zA-Z0- 9.]++@[a-zA-Z]++.(com|ca|biz)”. Use this as the input to the matches method in the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT