Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

--------------------------------------------DATAEXCEPTION.JAVA-------------------------------------------- public class DateException extends Exception { public DateException(String msg) { super(msg); } } -------------------------------------------------BIRTHDAY.JAVA------------------------------------------------- public class Birthday { int day; int month; int year;

image text in transcribed

image text in transcribed

--------------------------------------------DATAEXCEPTION.JAVA--------------------------------------------

public class DateException extends Exception

{

public DateException(String msg)

{

super(msg);

}

}

-------------------------------------------------BIRTHDAY.JAVA-------------------------------------------------

public class Birthday {

int day;

int month;

int year;

int[] daysInMonths = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

public Birthday(int day, int month, int year) throws DateException

{

if (year > 2018)

{

throw new DateException("Impossible!");

}

this.day = day;

this.month = month;

this.year = year;

System.out.println("Birthday successfully created!");

}

public String toString()

{

return month+"/"+day+"/"+year;

}

}

Lab 9: Exceptions and Exception Handling Overview: In this lab, you will be asked to create two new exception classes as part of an exception inheritance hierarchy. You will then demonstrate use of the exception classes by throwing these exceptions in the appropriate place, and handling them in a tester class. A. The Birthday class You are given a class representing Birthdays. There are three int instance fields, representing day, month and year. The class is extremely simple, containing just a constructor method and a toString() method You will notice that the Birthday constructor throws an exception if the year is greater than 2018 Because it throws a DateException, we have to declare in the constructor's header that it throws a DateException. Notice that DateException is a custom exception class. It is very simple. In fact, we can show it in its entirety here public class DateException extends Exception public DateException (String msg) super (msg); All it does is extend Exception, and have its constructor parameter- a String representing the exception- passed on to the superclass constructor. This may seem a bit pointless, but a simple class like this can nonetheless be very useful as part of an inheritance hierarchy allowing us to handle exceptions polymorphically B. New DateException subclasses Step 1. You should create two subclasses of DateException: one called MonthException and one called DayException. They will have the same basic structure as DateException, i.e. they will just consist of constructors that call their superclass constructor with a String parameter Step 2. In the Birthday constructor, you should throw a MonthException if the month parameter is greater than 12 or less than 1. Include a relevant message (e.g. "month out of range"). You should throw a DayException if there are more days than the specified month actually has (e.g. April 31st should be invalid). You can use the provided array dayslnMonths to determine how many days each month has. Index 0 in the array is January, Index 1 is February etc. Again, include a relevant message such as "day out of range Notice that since MonthException and DayException are both subclasses of DateException (the "Is-a relationship), we do not have to modify the method header "throws" statement. If they did not have that inheritance relationship, we would need to specify all of the types that can be thrown. Step 3. In a tester class main() method, try creating a Birthday object within a try block. You should have 3 catch statements after the try block, for DateException, MonthException and DayException. Pay special attention to how the catch blocks are ordered. Create a Birthday object that deliberately causes an exception. Be sure to test all of your 3 exception types

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

Database Processing

Authors: David M. Kroenke

12th Edition International Edition

1292023422, 978-1292023427

More Books

Students also viewed these Databases questions

Question

=+ construct an experience curve and explain its significance.

Answered: 1 week ago