Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

INSTRUCTIONS Complete the requirements of this assignment on your own. When you

ID: 3753336 • Letter: I

Question

INSTRUCTIONS Complete the requirements of this assignment on your own. When you have finished, write your name on the board to get your instructor to mark your assignment. You are encouraged to create a flowchart; without a flowchart your instructor may not provide any assistance. Step 1: Create a program named CPSC1012-Ex04-YorName Step 2: In the Main() method, write the code that will do the following: Ask the user to enter a year (i.e. 2018) . Determine if the year entered is a leap year. Use the logic below to determine if a year is a leap year o o If the year is evenly divisible by 4 (i.e. 1980 is a leap year, but 1981 is not) If the year is evenly divisible by 100, then it must also be evenly divisible by 400 (i.e. 1900 is not a leap year, but 2000 is a leap year) Display whether the year entered is a leap year

Explanation / Answer

CPSC1012_Ex04_Suresh.java

import java.util.Scanner;

public class CPSC1012_Ex04_Suresh {

private static final int GREGORIAN_START_YEAR = 1582;

public static void main(String[] args) {

Scanner scan = new Scanner(System.in);

System.out.print("Enter the year: ");

int year = scan.nextInt();

if(year < GREGORIAN_START_YEAR){

System.out.println("Invalid Input. Year must be greater than or equals to "+GREGORIAN_START_YEAR);

}

else {

if(isLeapYear(year)){

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

}

else{

System.out.println(year+" is a not leap year.");

}

}

}

public static boolean isLeapYear(int y) {

if (y % 4 != 0) return false;

if (y < GREGORIAN_START_YEAR) return true;

return (y % 100 != 0) || (y % 400 == 0);

}

}

Output:

Enter the year: 2018
2018 is a not leap year.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote