Question: Problem #3 -- Exceptions Download this file that contains the outline for the class helping run the newest hit game show: American Exception Warrior. You

Problem #3 -- Exceptions

Download this file that contains the outline for the class helping run the newest hit game show: American Exception Warrior. You will be writing 2 methods that help the TV producers pick which athletes to highlight on the show. Your code will depend on the AEWCompetitor class included in the problem's JAR file. (Because the source is not important/needed, I only provided the .class file for AEWCompetitor) AEWCompetitor specifies 1 method which you will be calling: public void runCourse() throws RopeClimbException, TimeException. // RopeClimbException is checked; TimeException is unchecked

For this homework problem, you will need to complete the following 2 methods in AmericanExceptionWarrior (including adding any throws clauses that are required for compilation):

cityFinals() calls athlete.runCourse(); and return true when runCourse() completes normally. If a RopeClimbException gets raised by runCourse(), allow the exception to continue to the calling method WITHOUT handling it. When the called method raises a TimeException, handle the exception and return false .

mountMidoriyama calls athlete.runCourse(); and return true when runCourse() completes normally. If a TimeException gets raised by runCourse(), allow the exception to continue to the calling method WITHOUT handling it. When the called method raises a RopeClimbException, handle the exception and return false .

You can check your cityFinals() code using the AmericanExceptionWarriorTest class in this JAR file. The test cases for mountMidoriyama() are only on AutoLab, so you will want to test this code on your own.

AMERICANEXCEPTIONWARRIOR CLASS

package edu.buffalo.cse116;

/** * This class was inspired by the TV show "American Ninja Warrior". Actually, it was based upon the show's name (I have * never seen the show), but from what I learned of through Wikipedia it seems like some people find this form of torture to be fun. * At the least, it provides structure for a coding with Exceptions. * * @author Matthew Hertz */ public class AmericanExceptionWarrior {

/** * AEWCompetitor includes a single method whose signature is:
* {@code public void runCourse() throws RopeClimbException, TimeException}. // RopeClimbException is checked; TimeException is unchecked.
* This method calls {@code athlete.runCourse();} and return true when {@code runCourse()} completes normally (this is the city finals winner). * The rope climb is the first obstacle and the producers need to know about the athletes who fail at this. * If a {@code RopeClimbException} gets raised by {@code runCourse()}, allow the exception to continue to the calling method WITHOUT handling it. * The {@code TimeException} is raised by {@code runCourse()} when the athlete is not the fastest. When this happens, handle the exception and * return false. * * @param athlete Instance whose runCourse() method is used to see if they advance from the city finals. * @return True, if the athlete wins the city finals; false otherwise. * @throws RopeClimbException Exception that gets passed on from those athletes unable to complete the first obstacle. */ public boolean cityFinals(AEWCompetitor athlete) { }

/** * AEWCompetitor includes a single method whose signature is:
* {@code public void runCourse() throws RopeClimbException, TimeException}. // RopeClimbException is checked; TimeException is unchecked.
* This method calls {@code athlete.runCourse();} and return true when {@code runCourse()} completes normally (this will be the overall champion). * On this year's Mount Midoriyama, the rope climb is the LAST obstacle and so does not require the producers attention. * If a {@code RopeClimbException} gets raised by {@code runCourse()}, handle the exception and return false. * Losing athletes may get invited back for a future season. If a {@code TimeException} is raised by {@code runCourse()}, allow the exception to continue to the calling method WITHOUT handling it. * * @param athlete Instance whose runCourse() method is used to see if they are grand champion. * @return True, if the athlete wins the grand championship; false otherwise. * @throws TimeException Exception that gets passed on from losing athletes who complete the course. */ public boolean mountMidoriyama(AEWCompetitor athlete) { } }

AMERICANEXCEPTIONWARRIOR TEST CLASS

package edu.buffalo.cse116;

import static org.junit.Assert.*;

import java.lang.reflect.Method;

import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.Timeout;

import edu.buffalo.correctness.Scorable;

/* * Homework test cases
* (C) 2018 by Matthew Hertz
* Posting this file to a website without the explicit written permission of the author is a violation of their * copyright. */ @Scorable(problem = "Homework05Problem3") public class AmericanExceptionWarriorTest {

@Test public final void testClassAndMethodDefinitions() throws Exception { Class aewKlass = AmericanExceptionWarrior.class; try { Method cityFinalsMethod = aewKlass.getMethod("cityFinals", AEWCompetitor.class); Class[] throwsArr = cityFinalsMethod.getExceptionTypes(); assertEquals("cityFinals() should only list 1 method in its throws clause!", 1, throwsArr.length); assertEquals("cityFinals() should only list the specific exception that might get raised!", RopeClimbException.class, throwsArr[0]); } catch (NoSuchMethodException e) { fail("Your AmericanExceptionWarrior class should still define the cityFinals method!"); } try { Method mountMidoriyamaMethod = aewKlass.getMethod("mountMidoriyama", AEWCompetitor.class); Class[] throwsArr = mountMidoriyamaMethod.getExceptionTypes(); assertEquals("Methods rarely list unchecked exceptions in their throws clause (and mountMidoriyama() should not either)!", 0, throwsArr.length); } catch (NoSuchMethodException e) { fail("Your AmericanExceptionWarrior class should still define the mountMidoriyama method!"); } } @Test public final void testCityFinalsCompleted() throws Exception { AEWCompetitor qc = new AEWCompetitor(2); boolean retVal = ninja.cityFinals(qc); assertFalse("cityFinals() should return false when a TimeException is raised.", retVal);

AEWCompetitor qc2 = new AEWCompetitor(0); retVal = ninja.cityFinals(qc2); assertTrue("cityFinals() should return true when no exeption was raised.", retVal); }

@Test() public final void testCityFinalsFailed() { AEWCompetitor qc = new AEWCompetitor(3); try { ninja.cityFinals(qc); fail("cityFinals() should pass along the RopeClimbException instance and not make it disappear."); } catch (RopeClimbException rce) { StackTraceElement[] steArr = rce.getStackTrace(); assertEquals("cityFinals() should pass along the RopeClimbException instance WITHOUT handling it", "runCourse", steArr[0].getMethodName()); assertEquals("cityFinals() should pass along the RopeClimbException instance WITHOUT handling it", "edu.buffalo.cse116.AEWCompetitor", steArr[0].getClassName()); } }

private AmericanExceptionWarrior ninja;

@Before public void setUp() { ninja = new AmericanExceptionWarrior(); } }

ROPECLIMBEXCEPTION CLASS

package edu.buffalo.cse116;

public class RopeClimbException extends Exception {

}

TIMEEXCEPTION CLASS

package edu.buffalo.cse116;

public class TimeException extends RuntimeException {

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!