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

A year with 366 days is called a leap year. A year is a leap year if it is divis

ID: 3851018 • Letter: A

Question

A year with 366 days is called a leap year. A year is a leap year if it is divisible by 4 (for e.g., 1980), except that it is not a leap year if it is also divisible by 100 (for e.g., 1900); however, it is a leap year if it is further divisible by 400 (for e.g., 2000).

Write a program that prompts the user (using JOptionPane) to enter a year and displays (using JOptionPane) whether that year is a leap year. The program should repeatedly prompt the user for a year until the user enters a sentinel value of -99 to quit the program. Note that you will need to use the mod function (%) to determine divisibility by a number.

Explanation / Answer

JAVA CODE:

import javax.swing.*;

public class LeapYear {
  
JFrame f;
int dialog(){
f=new JFrame();   
int yr=Integer.parseInt(JOptionPane.showInputDialog(f,"Enter a year "));
return yr;
}
void leapYearFind(int y){
   if((y % 400 == 0) || ((y % 4 == 0) && (y % 100 != 0)))
System.out.println("Year " + y + " is a leap year");
else
System.out.println("Year " + y + " is not a leap year");
}

public static void main(String[] args) {
LeapYear leapYear = new LeapYear();
  
while(true){
int year =leapYear.dialog();
  
if(year == -99){
   System.out.println("Proagram is exited");
   System.exit(0);
}else{
   leapYear.leapYearFind(year);
  
}
}
}
}

OUTPUT :

Year 2000 is a leap year
Year 2004 is a leap year
Year 1900 is not a leap year
Year 2005 is not a leap year
Proagram is exited

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