CSCI 201 Short Homework 3 -- Exceptions in Java
Introduction
So far this semester, we have learned quite a lot about computer
programming and Java. We have learned basic Java program structure,
primitive data types, expressions, objects, class libraries, control
statements (conditional and iteration statements), instance variables,
scope, parameters, returns, object references, interfaces, arrays, and
inheritance. This has been a lot of difficult material and you should
be proud of yourself---congratulations!
In this final assignment, we will learn a little about writing
programs that use Exceptions.
Your Task
- Begin your program by typing (or cutting and pasting) the following
class outline into a java file named Exceptional.java.
public class Exceptional
{
public static void main (String[] args)
{
}
public static void exceptionGenerator (int num) throws GettingUpThereException
{
}
}
-
After the code you have already typed (i.e., after the definition of
the Exceptional class), enter the following:
class GettingUpThereException extends Exception
{
GettingUpThereException (String message)
{
super (message);
}
}
What you have done is create a new type of exception that is derived
from the parent (i.e., super) class Exception. The
definition of the new derived class,
GettingUpThereException, is placed within the
Exceptional file, but not within the Exceptional
class.
- Now, complete the exceptionGenerator() method as
follows:
- First, create an object of type GettingUpThereException
in the exceptionGenerator() method. The class
constructor has
a String parameter therefore you will need to pass a String message
such as: "Young at heart is a wonderful thing" to the constructor when
creating the GettingUpThereException object.
- Next, use an if-statement to test whether the argument
passed to exceptionGenerator() is greater than the number
that you consider to be the breaking point to old age. The
responsibility of this if-statement is to throw
a GettingUpThereException exception if that number is
"old".
- Finally, write a System.out.println() statement with a
message such as: "You're pretty young" at the end of the
exceptionGenerator() method.
- The final portion of this assignment is to call the
exceptionGenerator() method from main().
- In main(), declare an integer variable.
- Then, set up a for-loop that will execute four times.
Within this for-loop, the first statement of the
for-loop needs to prompt the user to enter an integer age.
- Next, use the Keyboard
class to read an integer into the variable you created above.
This read statement will be the second statement in the
for-loop.
- Now for the hard part. As the third statement of the
for-loop, you need to call the exceptionGenerator()
method giving it the number entered by the user as its argument. Since
that method throws a GettingUpThereException
exception, this will need to be done with a try-catch block.
The catch block must print the exception's message
using its getMessage() method.
-
When you have completed the program, test it using numbers that are
both above and below your old age cutoff. Make sure that you
understand its output.
Submitting Your Assignment
The program written for this assignment must be turned in by
transferring the file Exceptional.java to your ftp
directory for this class. You were shown how to transfer files to
your class ftp directory in lab
1