Purchase Solution

A custom Java exception

Not what you're looking for?

Ask Custom Question

Now, let me introduce the exercise first.
Textbook has given a .java file named "date.java", but it is not perfect;
What I have done is the "ExtendedDate.java", which improved the "data.java" that the program will firstly check if the date input from users is valid then store it with the setDate method. Additionally, the "ExtendedDate.java" has the function including of:
1. Set the month.
2. Set the day.
3. Set the year.
4. Return the month.
5. Return the day.
6. Return the year.
7. Test whether the year is a leap year.
8. Return the number of days in the month.
9. Return the numbers of the days passed in the year.
10. Return the number of the days remaining in the year.
11. Calculate the new date by adding a fixed number of days to the date.
"TestExtendedDate.java" will call the methods of "ExtendedDate.java", than print out all the results.
I attached the three java files already;
1. Date.java
2. ExtendedDate.java
3. TestExtendedDate.java

Now what I need you help is to finish an "exception" part of my program. It is to add creating and handling of Exceptions when an attempt is made to create an invalid date. The Exception object must return information to the user about the nature of the problem that occurred.
Please do not change any method's name and the return type, thanks.
Here is a sample run result of my program:
1.
Please enter Month, Day and Year with spaces(MM/M DD/D YYYY):

15 15 1950
Invalid date and don't see the following jugement which will be also invalid.
Given Date is:1-1-1900
1950 is leap year:false
Current Date is 1-1-1900 and Days in current Month is 31
Current Date is 1-1-1900 and Days Passed is 1
Current Date is 1-1-1900 and Days Remained is 364
Enter num of days to be added:

2.
Please enter Month, Day and Year with spaces(MM/M DD/D YYYY):

12 11 1999
Given Date is:12-11-1999
1999 is leap year:false
Current Date is 12-11-1999 and Days in current Month is 31
Current Date is 12-11-1999 and Days Passed is 345
Current Date is 12-11-1999 and Days Remained is 20
Enter num of days to be added:
100
The new date is 3-20-2000

3.
Please enter Month, Day and Year with spaces(MM/M DD/D YYYY):

12 12 2998
Given Date is:12-12-2998
2998 is leap year:false
Current Date is 12-12-2998 and Days in current Month is 31
Current Date is 12-12-2998 and Days Passed is 346
Current Date is 12-12-2998 and Days Remained is 19
Enter num of days to be added:
500
New Date is out of range!

Attachments
Purchase this Solution

Solution Summary

This solution shows how to create and use a custom exception in a Java program.

Solution Preview

In order to add exceptions to this program we need to first create a custom exception class. After that has been created we can modify the ExtendedDate class to throw exceptions as needed and then modify the test class to acknowledge the exception.

Creating a custom exception is simple. The new class needs to extend the Java Exception class. In our case we also want to store an error message. The result is the following:

<pre>
public class DateException extends Exception {

String ...

Purchase this Solution


Free BrainMass Quizzes
C# variables and classes

This quiz contains questions about C# classes and variables.

Java loops

This quiz checks your knowledge of for and while loops in Java. For and while loops are essential building blocks for all Java programs. Having a solid understanding of these constructs is critical for success in programming Java.

Inserting and deleting in a linked list

This quiz tests your understanding of how to insert and delete elements in a linked list. Understanding of the use of linked lists, and the related performance aspects, is an important fundamental skill of computer science data structures.

Basic UNIX commands

Use this quiz to check your knowledge of a few common UNIX commands. The quiz covers some of the most essential UNIX commands and their basic usage. If you can pass this quiz then you are clearly on your way to becoming an effective UNIX command line user.

Javscript Basics

Quiz on basics of javascript programming language.