Answer to Question #274196 in Java | JSP | JSF for Teray

Question #274196

write a program thats prompts the user to enter a person's date of birth in numeric form such as 8-27-1980. the program then outputs the date of birth in the form: august 27, 1980. Your program then outputs the date of birth in the form: august 27, 1980. Your program must contain to classes: invalidDayException and invalidMonthExcep. if users enter an invalid value for a day, then the program should throw and catch and InvalidDayExcept object. similar conventions for the values of month and year. (noted that your program must handle a leap year)



1
Expert's answer
2021-12-01T12:12:32-0500
import java.util.Scanner;


class InvalidDayExcept extends Exception {


	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;


	public InvalidDayExcept() {
		super("Invalid day");
	}
}


class InvalidMonthExcept extends Exception {


	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;


	public InvalidMonthExcept() {
		super("Invalid month");
	}
}


class InvalidYearExcept extends Exception {


	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;


	public InvalidYearExcept() {
		super("Invalid year");
	}
}


public class App {


	static boolean isLeapYear(int year) {
		return (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0));
	}


	@SuppressWarnings("resource")
	static void displayDate() throws InvalidMonthExcept, InvalidDayExcept, InvalidYearExcept {
		Scanner keyBoard = new Scanner(System.in);
		String[] months = { "January", "February", "March", "April", "May", "June", "July", "August", "September",
				"October", "November", "December" };


		System.out.print("Enter the date: ");
		String strDateValues[] = keyBoard.nextLine().split("-");
		if (strDateValues.length == 3) {
			int month = Integer.parseInt(strDateValues[0]);
			int day = Integer.parseInt(strDateValues[1]);
			int year = Integer.parseInt(strDateValues[2]);// 8-27-1980


			if (day < 1 || day > 31) {
				throw new InvalidDayExcept();
			}
			if (month < 1 || month > 12) {
				throw new InvalidMonthExcept();
			}
			if (month == 2 && !isLeapYear(year) && day != 28) {
				throw new InvalidMonthExcept();
			}
			if (month == 2 && isLeapYear(year) && day != 29) {
				throw new InvalidMonthExcept();
			}
			if (year < 1 || year > 2200) {
				throw new InvalidYearExcept();
			}
			System.out.printf("%s %d, %d.\n", months[month - 1], day, year);
		} else {
			throw new InvalidYearExcept();
		}


		keyBoard.close();
	}


	/**
	 * The start point of the program
	 * 
	 * @param args
	 * @throws InvalidMonthExcept
	 * @throws InvalidDayExcept
	 */
	public static void main(String[] args) {
		try {
			displayDate();
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}


	}


}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS