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

1. public static boolean isLeapYear (int year) Returns whether or not the given

ID: 3732227 • Letter: 1

Question

1. public static boolean isLeapYear (int year) Returns whether or not the given year is a leap year. 2. public static int dayofYear (int month, int day, int year) Return the day of the year. Return -1 if the date is not valid 3. public static int daysRemaining(int month, int day, int year) Return the number of days remaining in the year. Return -1 the date is not valid. 4. public static boolean isDatevalid(int month, nt day, int year) Return whether or not the date given is valid.(Valid years are 1800 to 3000, inclusive.) 5. public static short daysInNonth(int month, int year) Given a month number, return the number of days in that month for that year. Return 0 if the month is not valid. 6. public static void dateReport (int month, int day, int year) Print out a date report for the given date as shown in the sample runs. 7. public static short monthNameToNumber (String month) Given a String of the full month "January" or the first three letters "Jan" . return the month's position in the year. For example, " jUL" would return 7 ·Invalid months should return -1 . (case insensitive) 8. public static String monthNumberToName (int month) Given a month of the year, return the full name of the month as a string or INVALID otherwise. 9. public static int daysBetween(int ml, int d, int yl. int m2, int d2, int y2) Return the number of days between two dates in the same year. If either of the dates are invalid or the years are not the same, return-1 10. public static long millisecLeftInYear (int month, int day, int year) Return the number of milliseconds left in a year. You may assume 24-hour days and work in whole-day increments. 11 public static void printMenu) Print out the choice menu as shown in the sample runs. 12. public static int getPosNum(int max) Prompt the user to "Enter a positive number up to [max]· " until a valid number is read in. See the sample runs for an example. If max is not a positive number, return 0

Explanation / Answer

import java.util.Scanner;

public class Date {

public static void main(String[] args) {

Scanner in = new Scanner(System.in);

int choice;

do{

printMenu();

choice = getPosNum(9);

int day, month, year;

switch (choice) {

case 1:

System.out.print("Enter the month as a number: ");

month = in.nextInt();

System.out.print("Enter the day as a number: ");

day = in.nextInt();

System.out.print("Enter the year as a number: ");

year = in.nextInt();

dateReport(month, day, year);

break;

case 2: {

int m, d, y;

System.out.print("Enter the month1 as a number: ");

month = in.nextInt();

System.out.print("Enter the day1 as a number: ");

day = in.nextInt();

System.out.print("Enter the year1 as a number: ");

year = in.nextInt();

System.out.print("Enter the month2 as a number: ");

m = in.nextInt();

System.out.print("Enter the day2 as a number: ");

d = in.nextInt();

System.out.print("Enter the year2 as a number: ");

y = in.nextInt();

int days = daysBetween(month, day, year, m, d, y);

if(days!=-1)

System.out.println(

"Days between " + month + "-" + day + "-" + year + " to " + m + "-" + d + "-" + y + " is: " + days);

else

System.out.println("Invalid Dates");

break;

}

case 3:

System.out.print("Enter the month as a number: ");

month = in.nextInt();

System.out.print("Enter the day as a number: ");

day = in.nextInt();

System.out.print("Enter the year as a number: ");

year = in.nextInt();

System.out.println("Day of Year is: " + dayOfYear(month, day, year));

break;

case 4:

System.out.print("Enter the month as a number: ");

month = in.nextInt();

System.out.print("Enter the day as a number: ");

day = in.nextInt();

System.out.print("Enter the year as a number: ");

year = in.nextInt();

System.out.println("Days remaining: " + daysRemaining(month, day, year));

break;

case 5:

System.out.print("Enter the month as a number: ");

month = in.nextInt();

System.out.print("Enter the year as a number: ");

year = in.nextInt();

System.out

.println("Days in " + monthNumberToName(month) + ", " + year + " is: " + daysInMonth(month, year));

break;

case 6: {

String monthName;

System.out.print("Enter the month name: ");

monthName = in.next();

int number = monthNameToNumber(monthName);

if(number!=-1)

System.out.println(monthName + " to number is: " + number);

else

System.out.println("Invalid Month");

}

break;

case 7:

System.out.print("Enter the month as a number: ");

month = in.nextInt();

System.out.print("Enter the day as a number: ");

day = in.nextInt();

System.out.print("Enter the year as a number: ");

year = in.nextInt();

if (isDateValid(month, day, year))

System.out.println("VALID");

else

System.out.println("INVALID");

break;

case 8:

System.out.print("Enter the month as a number: ");

month = in.nextInt();

System.out.print("Enter the day as a number: ");

day = in.nextInt();

System.out.print("Enter the year as a number: ");

year = in.nextInt();

System.out.println("There are " + (millisecLeftInyear(month, day, year)*1000) + " milliseconds remaining after "

+ month + "/" + day + "/" + year);

break;

case 9:

System.out.println("Farewell!");

System.exit(1);

break;

}

}while(choice!=9);

}

public static boolean isLeapYear(int year) {

if (year % 4 == 0 || year % 100 == 0 || year % 400 == 0)

return true;

return false;

}

public static int dayOfYear(int month, int day, int year) {

int days = 0;

for (int i = 1; i < month; i++) {

days += daysInMonth(i, year);

}

days += day;

return days;

}

public static int daysRemaining(int month, int day, int year) {

int days = 365;

if (isLeapYear(year))

days = 366;

return days - dayOfYear(month, day, year);

}

public static boolean isDateValid(int month, int day, int year) {

if (year < 1800 || year > 3000)

return false;

if (month <= 0 || month > 12)

return false;

if (day <= 0 || day > daysInMonth(month, year))

return false;

return true;

}

public static short daysInMonth(int month, int year) {

short days[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

if (isLeapYear(year))

days[1] = 29;

return days[month - 1];

}

public static void dateReport(int month, int day, int year) {

if (isDateValid(month, day, year))

System.out.println(" "+day + ", " + monthNumberToName(month) + " " + year+" ");

else

System.out.println("INVALID");

}

public static short monthNameToNumber(String month) {

String months[] = { "January", "February", "March", "April", "May", "June", "July", "August", "September",

"October", "November", "December" };

for (int i = 0; i < 12; i++) {

if (months[i].startsWith(month))

return (short)(i + 1);

}

return -1;

}

public static String monthNumberToName(int month) {

String months[] = { "Janurary", "February", "March", "April", "May", "June", "July", "August", "September",

"October", "November", "December" };

if (month > 0 && month <= 12)

return months[month - 1];

return "INVALID";

}

public static int daysBetween(int m1, int d1, int y1, int m2, int d2, int y2) {

if(!isDateValid(m1, d1, y1) || !isDateValid(m1, d1, y1))

return -1;

long n1 = y1 * 365 + d1;

for (int i = 0; i < m1 - 1; i++)

n1 += daysInMonth(m1, y1);

;

int y = y1;

if (m1 <= 2)

y--;

n1 += (y / 4) - (y / 100) + (y / 400);

long n2 = y2 * 365 + d2;

for (int i = 0; i < m2 - 1; i++)

n2 += daysInMonth(m2, y2);

y = y2;

if (m2 <= 2)

y--;

n2 += (y / 4) - (y / 100) + (y / 400);

return (int)(n2 - n1);

}

public static long millisecLeftInyear(int month, int day, int year) {

int days = daysRemaining(month, day, year);

System.out.println(days);

return (days * 24 * 60 * 60);

}

public static void printMenu() {

System.out.println("*** Date / Time Menu ***");

System.out.println("===========================");

System.out.println(

"1. Date report 2. Days between 3. Day of year 4. Days remaining 5. Days in month 6. Month name to number 7. Check date validity 8. Milliseconds left 9. Quit ");

}

public static int getPosNum(int max) {

if (max <= 0)

return 0;

Scanner in = new Scanner(System.in);

int n;

while (true) {

System.out.print("Enter a positive number upto (" + max + "):");

n = in.nextInt();

if (n <= 9 && n>0)

break;

}

return n;

}

}