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'; } }
I'm looking for a program, written in vPython, that simulates the movement of a spherical mass,...
I'm looking for a program, written in vPython, that simulates the movement of a spherical mass, suspended from a ceiling, tied to a spring. We are supposed to take the gravitaional force into account but are allowed to ignore the effects of air-resistance (for now). I am a newbie at vPython so comments within the program to explain what certain parts of the code do would be appreciated!
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...
Circle Class (This is in JAVA) /** * Defines a basic shape with just area *...
Circle Class (This is in JAVA) /** * Defines a basic shape with just area * * @author Jo Belle * @version 0.2 (10/05/2020) */ public class Shape{ private double area; public Shape(){ area = 0.0; } public Shape( double a ){ this.area = a; } public void setArea( double a ){ area = a; } public double getArea(){ return area; } public String toString(){ return "Shape:\n\tarea: " + area; } } /** * Create a simple Circle object *...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT