1. I enjoyed writing this sample code. With every DQ and weekly readings I learn more
about Java programming and I am finally starting to see the light. Some aspects of Java
programming are still a struggle for me like class and method, but I am working through
my understanding of these concepts. I do like the fact that NetBeans implies where the
errors exist and what is expected. I was unsure how to write the pseudocode for this
sample program because it appeared to be straightforward. With that said, I just used a
simple and short pseudocode that states exactly what the program will do when input of
a four-digit year is entered by the user.
Pseudocode
*** Program LeapYear
1 prompt user to input a four-digit year using the IF ELSE conditional statement
2 if user inputs a valid year, display “is a leap year”
3 ELSE
4 if user inputs an invalid year, display “is NOT leap year”
End Program
Here is my code and it is working.
package LeapYear;
/*
* Prompt the user to enter a four-digit year. Using the IF ELSE conditional statement
print if it is a leap year or not.
*
* For example:
* If the user enters 1990, Display year 1990 is not a leap year
* If user input is 2000, display year 2000 is a leap year, etc.
*
*/
import java.util.Scanner;
class leapyear {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
//
// Read string input for a four-digit year
//
System.out.println("Please input a four-digit year");
int input = in.nextInt();
2. int remainder = (input % 4);
int century = (input % 100);
if (remainder == 0) {
if (century % 100 == 0) {
System.out.println("The year: " + input + " is a leap year");
}
else {
System.out.println("The year: " + input + " is NOT leapyear");
}
}
Output 1:
Please input a year
1990
The year: 1990 is NOT leapyear
BUILD SUCCESSFUL (total time: 11 seconds)
Output 2:
Please input a year
2000
The year: 2000 is a leapyear
BUILD SUCCESSFUL (total time: 4 seconds)