JAVA****You must create - InvalidDayExcep and InvalidMonthExcep classes in addit
ID: 3675724 • Letter: J
Question
JAVA****You must create - InvalidDayExcep and InvalidMonthExcep classes in addition to the test driver program. Use try/catch in your code.
OUTPUT shown below.....
Sample Run 1: Enter birth year: 1966 Enter month number 14 The value of day must be between 1 and 12. Enter month number: 8 Enter birth day: 45 Day must be between 1 and 31 Enter birth day: 30 Birth Day: August 30, 1966 Sample Run 2: not a leap year Enter birth year: 2001 Enter month number: 2 Enter birth day: 29 Day must be between 1 and 28 Enter birth day: 18 Birth Day: February 18, 2001Explanation / Answer
import java.util.*;
import java.text.DateFormatSymbols;
class A10_Ex1
{
public static void main(String atgs[])
{
int year=0, mon=0, day=0;
Scanner scan = new Scanner(System.in);
System.out.println("Enter birth Year");
year=scan.nextInt();
while(true)
{
try
{
System.out.println("Enter month number");
mon=scan.nextInt();
}
catch(Exception e){}
if(mon>0 && mon<13)
break;
else
System.out.println("Value of
month must be between 1 to 12");
}
while(true)
{
try
{
System.out.println("Birth Day");
day=scan.nextInt();
}
catch(Exception e){}
if(year%4==0)
{
if(day>0)
if(day<30)
break;
else
System.out.println("Value of day must be between 1 to 29");
else
System.out.println
("Value of day must be between 1 to 29");
}
else
{
if(day>0)
if(day<29)
break;
else
System.out.println("Value of day must be between 1 to 28");
else
System.out.println
("Value of day must be between 1 to 28" );
}
}
String m=new DateFormatSymbols().getMonths()
[mon-1];
System.out.println(m+" "+day+", "+year);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.