in Java Write a program that accepts a date (month, day, year) and a number. cal
ID: 3623819 • Letter: I
Question
in Java
Write a program that accepts a date (month, day, year) and a number.
calculate the date, that number of days in the future, from the input date that will be.
don't forget leap year
http://www.timeanddate.com/date/leapyear.html
You must have at least 2 methods. 1 must return a value, 1 must receive parameters. It could be 1 method that does both of these, but you still must have at least 2 methods
keep going until a sentinel is entered for the month (if a sentinel is entered for the month there is no need to enter a day and year)
check the validity of the date entered (example: 1/32 is no good, 9/31 is no good
2/29/2001 is no good, remember 2/29/2000 is good)
Explanation / Answer
import java.util.Scanner;
public class date
{
public static void main(String[] args)
{
int day, month, year, dayNumber;
char ch;
Scanner input = new Scanner(System.in);
System.out.println("Enter a day");
day = input.nextInt();
System.out.println("Enter a month");
month = input.nextInt();
System.out.println("Enter a year");
year = input.nextInt();
dayNumber = 0;
while ( month > 1 )
{
switch( month - 1 )
{
case 1 :
case 3 :
case 5 :
case 7 :
case 8 :
case 10 :
case 12 :
dayNumber += 31;
break;
case 4 :
case 6 :
case 9 :
case 11 :
dayNumber += 30;
break;
case 2 :
dayNumber += 28;
if ( isLeapYear( year ) )
dayNumber++;
break;
}
month--;
}
dayNumber += day;
System.out.println(" The day number is "+dayNumber);
}
}
public class sLeapYear
{
if (( ( year % 4 == 0 ) && ( year % 100 != 0 ) ) ||( ( year % 100 == 0 ) && ( year % 400 == 0 ) ) )
return true;
return false;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.