Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

while(!done) try f System.out.print(Enter the month as an integer: ); month-scan.nextInt); System.out.print( Enter the day as an integer: ); day-scan.nextInt (); System.out.print(Enter the year as

image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
while(!done) try f System.out.print("Enter the month as an integer: "); month-scan.nextInt); System.out.print(" Enter the day as an integer: "); day-scan.nextInt (); System.out.print("Enter the year as an integer: ") year scan.nextInt); aDate new Date (month, day, year); done-true; catch (InputMismatchException ime) { System.out.println("Invalid input entered. Enter an integer"); Execute your program again and enter "w" for month or day or year. When entering w" for day you will enter a valid integer for month: when entering "w" for year you will enter valid integers for month and day. You are entering invalid data to test your code: you will have to enter valid data at some point in order for your program to continue executing When the scanner throws an InputMismatchException, the scanner will not pass the token that caused the exception, so that it may be retrieved or skipped via some other method. The nextLine method can be used to bypass the erroneous input. Add the nextLine statement to the catch block, and execute the program again Answer question #4 Note that this is not the most elegant solution for example, if you enter a valid month and a valid day, but enter a non-numeric character for year, you are forced to enter all values again. 12. Execute your program and enter a "13" for month. Since "13" is a valid integer your program does not detect an error. Include the following code (the code in bold) in the setMonth method defined in the Date class if (month1 && month12) dMonth month; else throw new RuntimeException( Invalid Month: month out of range"): 13. Execute your program: enter 13, 12, 2016 for month, day, and year respectively Answer question #5 14. Currently your program does not contain a handler for RuntimeExceptions. Include an appropriate catch block to handle a RuntimeException. Place this catch block above the existing catch blocks for InputMismatchException. Answer question #6 15. Execute your program: enter 13, 12, 2016 for month, day, and year respectively Answer question #7 16. Since many types of exception are considered RuntimeExceptions, it is sometimes desirable to create your own exception class. Create a new class called DateException and include the following code: public class DateException extends Exceptiont public DateException) super("Invalid value for Date; public DateException(String message) super(message); 17. Add a comment at the beginning to include a title, a description, and your names as the authors. You should include appropriate JavaDoc for both constructors. 18. Modify the setMonth method in the Date class by including the following code (the code in bold). public void setMonth(int month) throws DateException if (month> 1 && month

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases Demystified

Authors: Andrew Oppel

1st Edition

0072253649, 9780072253641

More Books

Students also viewed these Databases questions

Question

What is the Advantages of Mobile Apps for Consumers?

Answered: 1 week ago

Question

the student find other ways to meet his needs?

Answered: 1 week ago