Leap Year: Design a program that asks the user to enter a year and then determin
ID: 3629845 • Letter: L
Question
Leap Year: Design a program that asks the user to enter a year and then determines if it is a leap year. Be sure to tell the user what the program does. You program should have a loop that continues to repeat the leap year calculation for various years depending on the user's wishes. It asks the user to enter a 'y' or 'n' if s/he wants to process another year.
Your program must also contain at least the following four methods according to the following descriptions: (Note that your main method will consist primarily of calls to these four methods which will be imbedded in a loop.
Methods:
displayInstructions will tell the user what the program does, what to input, and what will be displayed as a result of running the program. It will not return any value nor will it need any formal parameters.
getYear has no formal parameters. It asks the user to input a year and then returns that year as an integer value.
isLeap has a formal integer parameter, year. It determines if a year is a leap year, and then returns a Boolean value (true or false) depending on whether the year is a leap year or not. How to determine if a year is a leap year? A year is a leap year if it is divisible by 4, but not divisible by 100, except when it is divisible by 400. (Hint: think about using the modulus operator here.)
displayResults has two formal parameters, the year and a boolean variable containing the value returned from method isLeap. This method will not need to return a value. It will simply display the year and display whether or not that year is a leap year.
Explanation / Answer
please rate - thanks
import java.util.*;
public class main
{public static void main(String[] args)
{int year;
boolean result;
char yes;
Scanner in=new Scanner(System.in);
displayInstructions( );
do
{year=getYear();
result=isLeap(year);
displayResults(year,result);
System.out.print("Enter another year(y/n)?");
yes=in.next().charAt(0);
}while(yes=='y'||yes=='Y');
}
public static void displayInstructions( )
{System.out.println("enter instructions");
System.out.println("here ");
}
public static int getYear()
{int y;
Scanner in=new Scanner(System.in);
System.out.print("Enter a year: ");
y=in.nextInt();
return y;
}
public static boolean isLeap(int year)
{ if((year%4==0&&year%100!=0)|| year%400==0)
return true;
return false;
}
public static void displayResults(int year, boolean result)
{if(result)
System.out.println(year+" is a leapyear");
else
System.out.println(year+" is not a leapyear");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.