Question

In: Computer Science

Instructions JAVA PROGRAMMING 1. Ask the user for a year input (positive integer - should be...

Instructions JAVA PROGRAMMING

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.

5. Point of thought: how will you handle centurial years, i.e., years ending in 00, e.g., 1900, 1800, 1700, 2000 etc.?

Goals

  1. Gain experience in input taking using the scanner class in Java.
  2. Gain experience in if/else selection statement in Java.

File/Project Naming

Your project/file should be named “Main.java”.

Test Your Program

  • Once the code is typed, you will compile+run (execute) your code.
  • You will input the year integer.
  • See the output given by your code.

Sample Output

Your output should look like this:

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

This year cannot be checked. Try again!

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

   This year cannot be checked. Try again!

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

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

Solutions

Expert Solution

import java.util.Scanner;

public class MyClass {
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 2017): ");
int year = sc.nextInt();
  
if(year < 1500 || year > 2017)
System.out.println("This year cannot be checked. Try again!");
else
{
if(year % 400 == 0)
System.out.println("Yes, " + year + " is a leap year!");
else if(year % 100 != 0 && year % 4 == 0)
System.out.println("Yes, " + year + " is a leap year!");
else
System.out.println("No, " + year + " is NOT a leap year!");
}
}
}

// Hit the thumbs up if you are fine with the answer. Happy Learning!


Related Solutions

IN JAVA Write a MAIN METHOD that asks for user input of a positive integer and...
IN JAVA Write a MAIN METHOD that asks for user input of a positive integer and a negative integer validates the inputs using a loop and then calls the METHOD from Question 3 and prints the result. The MAIN should have two variables (appropriately typed), get the input from the user and store them in the variables after validating them, then call the method from question 3 (sending parameters, if necessary) and print the returned value with an appropriate descriptive...
Write a program to find the prime numbers IN JAVA Ask user to input the integer...
Write a program to find the prime numbers IN JAVA Ask user to input the integer number test the number whether it is a prime number or not Then, print “true” or “false” depending on whether the number is prime or isn’t. Hint: number is prime when is has exactly 2 factors: one and itself. By this definition, number 1 is a special case and is NOT a prime. Use idea of user input, cumulative sum, and loop to solve...
1. Write a Java program that asks the user to input a positive integer n first,...
1. Write a Java program that asks the user to input a positive integer n first, then create an array of size n. Fill n random integers between 5 and 555, inclusively, into the created array. Output the sum of all the integers in the array and the average of all the integers in the array. 2 .Find the output of the following Java program and explain your answer
1.Prompts the user for a positive integer >= 0 2.Validates the user input to ensure it...
1.Prompts the user for a positive integer >= 0 2.Validates the user input to ensure it is a positive integer >= 0 3.Allocate (dynamically) an array big enough for the data. 4.Load the array with random numbers ranging in value from1 to 100 5.Display the elements of the array (unsorted) 6.Display the elements of the array (sorted) 7. Display the average 8.Display the median 9.Display the mode, if none, display appropriate message #include <iostream> #include <stdlib.h> /* srand, rand */...
JAVA Take in a user input. if user input is "Leap Year" your program should run...
JAVA Take in a user input. if user input is "Leap Year" your program should run exercise 1 if user input is "word count" your program should run exercise 2 Both exercises should run in separate methods Exercise 1: write a java method to check whether a year(integer) entered by the user is a leap year or not. Exercise 2: Write a java method to count all words in a string.
Design a program that will ask the user to input two integer numbers and then perform...
Design a program that will ask the user to input two integer numbers and then perform the basic arithmetic operations such as addition and subtraction. Each calculation is done by a separate function. The main function gets the input from the user, then calls the addition function and the subtraction function one at a time to perform the calculations. Each calculation function (addition or subtraction function) performs an arithmetic operation and then returns the calculation results back to where it...
python: ask the user to input a sequence of positive numbers. the end of the sequence...
python: ask the user to input a sequence of positive numbers. the end of the sequence is determined when the user enters a negative number. print out the maximum number from the sequence. output: keep entering positive numbers. to quit, input a negative number. enter a number: 67 enter a number: 5 enter a number: 8 enter a number: -3 largest number entered: 67 (note: i do not want to ask the user how many numbers they will input)
Java Programming Create a program that prompts the user for an integer number and searches for...
Java Programming Create a program that prompts the user for an integer number and searches for it within an array of 10 elements. What is the average number of comparisons required to find an element in the array? Your program should print the number of comparisons required to find the number or determine that the number does not exist. Try finding the first and last numbers stored in the array. Run your program several times before computing the average.
Name the script while.sh. Ask the user to enter a positive integer. You can assume that...
Name the script while.sh. Ask the user to enter a positive integer. You can assume that the user will enter a positive integer (input validation for a positive number not required). Use a while loop to print all integers from 0 up to and including the integer entered. Each integer should be printed on a line by itself and nothing else should print. This is for Linux. I have not done anything on this script. Please help with simplistic commands...
Write java program that will ask for the user for 2 input lines and print out...
Write java program that will ask for the user for 2 input lines and print out all words that occur 1 or more times on both lines (case sensitive). Write this without arrays and method. Here is a sample run: <Output> Enter two lines to process. The quick brown fox jumps over a lazy dog The fox hound outruns the lazy dog The words that occur on both lines are: The fox lazy dog
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT