Created
November 23, 2019 04:40
-
-
Save lbattaglioli2000/cf7fda0857d14ef7ca6ed3450dacf758 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
/** | |
* Luigi Battaglioli | |
* Year Assignment | |
*/ | |
public class Year { | |
public static void main(String[] args) | |
{ | |
// Create a new Scanner object to get user input. | |
Scanner s = new Scanner(System.in); | |
// Set aside some space in memory to hold the year, month, and day | |
int year, month, day; | |
// Parse the users input for the integers representing the year, month, and day. | |
month = Integer.parseInt(s.next()); | |
day = Integer.parseInt(s.next()); | |
year = Integer.parseInt(s.next()); | |
// Convert the inputted date to a single long number | |
long date = convertDays(year, month, day); | |
// Print out that converted long date | |
System.out.println("Date: " + date); | |
// Check if the year is a leap year | |
boolean isLeapYear = isLeapYear(year); | |
// If it is a leap year, print out a message saying so | |
if(isLeapYear) { | |
System.out.println("This is a leap year!"); | |
} else { | |
System.out.println("This is not a leap year."); | |
} | |
} | |
/** | |
* Converts the inputted year, month, and day to a single long number. | |
* | |
* @param year | |
* @param month | |
* @param day | |
* | |
* @return (long) date | |
*/ | |
private static long convertDays(int year, int month, int day) | |
{ | |
// your long year will be the: | |
// - sum of the year multiplied by 10000 | |
// - the month multiplied by 100 | |
// - and the day. | |
return (year * 10000) + (month * 100) + (day); | |
} | |
/** | |
* Checks if the year is a leap year. | |
* | |
* @param year | |
* @return boolean | |
*/ | |
private static boolean isLeapYear(int year) | |
{ | |
// Checks if the year is: | |
// - Divisible by 400. | |
// - Or, divisible by 4 and not 100. | |
if(year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) { | |
return true; | |
} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment