In the code contains the barest outline the code needed to e
In the code contains the barest outline the code needed to establish the class representing the latest reality show, AmericaExceptionWarrior. For this homework, you will need to complete the methods needed for the city finals and then the grand finale.
There is a additional jar file for this problem . Please check link: http://www.cse.buffalo.edu/~mhertz/courses/cse116/Weeks/week04.html
In cityFinals() you should call exceptionGenerator.runCourse(). If no exception is thrown, print out \"No exception thrown\". If the call to runCourse() resulted in a checked exception, print out \"Caught checked exception\". If runCourse() resulted in an unchecked exception, print out \"Caught unchecked exception\".
In mountMidoriyama() you will handle the results of the grand finale. If the throwChecked parameter is true, your code should raise a RopeClimbException. If the throwUnchecked parameter is true instead, your code should raise a TimeException. If both parameters are false, nothing else needs to be done.
Code:
Solution
The complete source code is:
Source-Code:
public class AmericanExceptionWarrior {
/**
* Method which attempts to complete the city finals by conquering the
* Qualifying Course. QualifyingCourse has a single method:
* {@code runCourse()}. When this method is called, it could throw a checked
* exception ({@code RopeClimbException}), throw an unchecked exception
* ({@code TimeException}), or not throw any exception. If an exception is
* thrown, the method should catch the exception and print \"Caught checked
* exception\" or \"Caught unchecked exception\" (as appropriate). If no
* exception is thrown, the method should print \"No exception thrown\"
*
* @param exceptionGenerator Class whose method, runCourse(), will be used
* to help test this Exception Warrior.
*/
public void cityFinals(QualifyingCourse exceptionGenerator) {
try {
exceptionGenerator.runCourse();
System.out.println(\"No exception thrown\");
} catch (RopeClimbException rce) {
System.out.println(\"Caught checked exception\");
} catch (TimeException te) {
System.out.println(\"Caught unchecked exception\");
}
}
/**
* Method to conquer the course at the grand finale -- throwing exceptions
* on your own. This has two parameters: throwChecked and throwUnchecked. If
* throwChecked is true, the method should raise a
* {@code RopeClimbException}. When throwUnchecked is true, the method
* should raise a {@code TimeException}. When both parameters are false, the
* method should not raise any exceptions.
*
* @param throwChecked When true, the method should raise a {
* @RopeClimbException}.
* @param throwUnchecked When true, the method should raise a {
* @TimeException}.
*/
public void mountMidoriyama(boolean throwChecked, boolean throwUnchecked) throws RopeClimbException {
if(throwChecked == true)
throw new RopeClimbException();
if(throwUnchecked == true)
throw new TimeException();
}
public static void main(String[] args) {
new AmericanExceptionWarrior().cityFinals(new QualifyingCourse(0));
System.out.println(\"\");
new AmericanExceptionWarrior().cityFinals(new QualifyingCourse(1));
System.out.println(\"\");
new AmericanExceptionWarrior().cityFinals(new QualifyingCourse(2));
}
}
Description:
cityFinals() method:
As per described in the comments, we have to call the runCourse() method of exceptionGenerator and if this call throws any exception, we need to catch it in appropriate catch block. If there is no exception caught, the method prints \"no exception caught\". This should not be written in finally or else every time it will be printed.
So, I have called that method in try block and catching it appropriate catch and printing whether it is checked or unchecked exception as per the definition given. If it does not throw any exception, it will print the next line stating no exception.
mountMidoriyama() method:
In this method, it throws an exception based on boolean variable values. It throws RopeClimbException for throwChecked == true and TimerException for throwUnchecked == true.
If both are false, no exception should be thrown, So, I had just written the both if conditions for true part. If both are false, no if statement will get executed and no exception will be thrown.
Now, to test the methods, you need to use them in your code. The code is not mentioned here, but I myself created some demo of how cityFinals() work when calling from main().
Here, I saw the runCourse() method from the JAR file. It is throwing RopeClimbException for odd number value for flags variable and TimeException for even value (except 0) of flags variable in QualifyingCourse class.
So, I created the 3 objects as per stated above in main() and got the following output for flags = 0, 1 and 2 respectively:
No exception thrown
Caught checked exception
Caught unchecked exception
This is just a sample to know the method is running properly. Wherever in your code you will need mountMidoriyama() method to be called, use it inside try block and check for the appropriate exception if thrown any.
Do comment if there is any query. I will solve it for sure. Thank you. :)

