Sync with qintian's homewrok and add new sample
This commit is contained in:
parent
191212ec5d
commit
2eb89779b6
56
LeapYear.java
Normal file
56
LeapYear.java
Normal file
@ -0,0 +1,56 @@
|
||||
/** Class that determines whether or not a year is a leap year.
|
||||
* @author YOUR NAME HERE
|
||||
*/
|
||||
public class LeapYear {
|
||||
|
||||
/** Calls isLeapYear to print correct statement.
|
||||
* @param year to be analyzed
|
||||
*/
|
||||
private static void checkLeapYear(int year) {
|
||||
if (isLeapYear(year)) {
|
||||
System.out.printf("%d is a leap year.\n", year);
|
||||
} else {
|
||||
System.out.printf("%d is not a leap year.\n", year);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method check if year is leap
|
||||
*/
|
||||
public static boolean isLeapYear(int year) {
|
||||
if (year % 400 == 0) {
|
||||
return true;
|
||||
} else if (year % 100 != 0 && year % 4 == 0) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static void countDaysInYear(int year) {
|
||||
if (isLeapYear(year)) {
|
||||
System.out.println("366 days");
|
||||
} else {
|
||||
System.out.println("365 days");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/** Must be provided an integer as a command line argument ARGS. */
|
||||
public static void main(String[] args) {
|
||||
if (args.length < 1) {
|
||||
System.out.println("Please enter command line arguments.");
|
||||
System.out.println("e.g. java Year 2000");
|
||||
}
|
||||
for (int i = 0; i < args.length; i++) {
|
||||
try {
|
||||
int year = Integer.parseInt(args[i]);
|
||||
checkLeapYear(year);
|
||||
countDaysInYear(year);
|
||||
} catch (NumberFormatException e) {
|
||||
System.out.printf("%s is not a valid number.\n", args[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user