Complete two methods in the provided outline of a code: In preliminaryRound() yo
ID: 3588936 • Letter: C
Question
Complete two methods in the provided outline of a code:
In preliminaryRound() 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".
grandFinale() will consider what can happen when finding the ultimate winner. If the throwChecked parameter is true, your code should raise the checked exception If the throwUnchecked parameter is true instead, your code should raise an unchecked exception. If both parameters are false, nothing else needs to be done.
The methods must pass the junit test cases provided below;
package edu.buffalo.cse116;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import org.junit.Before;
import org.junit.Test;
public class SpartanExceptionChallengeTest {
private SpartanExceptionChallenge ninja;
@Before
public void setUp() {
ninja = new SpartanExceptionChallenge();
}
@Test
public final void testPreliminaryRoundChecked() {
try {
OutputStream outStr = new ByteArrayOutputStream();
PrintStream sysOut = new PrintStream(outStr);
System.setOut(sysOut);
QualifyingCourse qc = new QualifyingCourse(2);
ninja.preliminaryRound(qc);
String response = outStr.toString();
response = response.trim();
assertEquals("preliminaryRound() must print out a mesage stating "Caught checked exception" when it catches the checked exception.",
"Caught checked exception", response);
} catch (Exception e) {
fail("preliminaryRound() must catch the checked exception and print out a mesage stating "Caught checked exception". Your code threw the exception: " +
e.toString());
}
}
@Test
public final void testCityFinalUnchecked() {
try {
OutputStream outStr = new ByteArrayOutputStream();
PrintStream sysOut = new PrintStream(outStr);
System.setOut(sysOut);
QualifyingCourse qc = new QualifyingCourse(1);
ninja.preliminaryRound(qc);
String response = outStr.toString();
response = response.trim();
assertEquals("preliminaryRound() must print out a mesage stating "Caught unchecked exception" when it catches the unchecked exception.",
"Caught unchecked exception", response);
} catch (Exception e) {
fail("preliminaryRound() must catch the unchecked exception and print out a mesage stating "Caught unchecked exception". Your code threw the exception: " +
e.toString());
}
}
@Test
public final void testPreliminaryRoundNoException() {
try {
OutputStream outStr = new ByteArrayOutputStream();
PrintStream sysOut = new PrintStream(outStr);
System.setOut(sysOut);
QualifyingCourse qc = new QualifyingCourse(0);
ninja.preliminaryRound(qc);
String response = outStr.toString();
response = response.trim();
assertEquals("preliminaryRound() must print out a mesage stating "No exception thrown" when no exception is thrown from runCourse()",
"No exception thrown", response);
} catch (Exception e) {
fail("preliminaryRound() must print out a mesage stating "No exception thrown" when no exception is thrown. Your code threw the exception: " +
e.toString());
}
}
@Test
public final void testGrandFinaleChecked() {
try {
ninja.grandFinale(true, false);
fail("grandFinale() must throw (and not catch) the checked exception when the first parameter is true.");
} catch (TimeException e) {
} catch (Exception e) {
fail("grandFinale() must throw (and not catch) a TimeException when the first parameter is true. Your code threw the exception: " +
e.toString());
}
}
@Test
public final void testGrandFinaleUnchecked() {
try {
ninja.grandFinale(false, true);
fail("grandFinale() must throw (and not catch) the unchecked exception when the second parameter is true.");
} catch (RopeClimbException e) {
} catch (Exception e) {
fail("grandFinale() must throw (and not catch) a RopeClimbException when the second parameter is true. Your code threw the exception: " +
e.toString());
}
}
@Test
public final void testGrandFinaleNone() {
try {
ninja.grandFinale(false, false);
} catch (Exception e) {
fail("grandFinale() should not be throwing any exceptions when neither parameter is true. Your code threw the exception: " +
e.toString());
}
}
}
Explanation / Answer
Below code can be used to catch the specified exceptions:
/**
* This class was inspired by the TV show "Spartan Ultimate Team Challenge". Actually, it was based upon the show's name (I have
* never seen the show) and what I read as the local media followed the UB-based team (https://www.buffalo.edu/ubnow/stories/2017/06/mathletes.html)
* So the story makes no sense, but it sounds like a good excuse for a homework assignment on Exceptions.
*
*/
public class SpartanExceptionChallenge {
/**
* Method which attempts to complete the preliminary by conquering the Qualifying Course. QualifyingCourse has a
* single method: {@code runCourse()}. When this method is called, it could throw a checked exception
* ({@code TimeException}), throw an unchecked exception ({@code RopeClimbException}), 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 preliminaryRound(QualifyingCourse exceptionGenerator) {
//Subclass of RuntimeException and Error are called as unchecked exception
//Any other child of Throwable which does not extend RuntimeException or Error is known as checked exception
try {
exceptionGenerator.runCourse();
//If above line doesn't throw any exception then need to print "No exception thrown"
System.out.println("No exception thrown");
}catch(RuntimeException|Error ex) {
System.out.println("Caught unchecked exception");
}catch(Throwable th) {
System.out.println("Caught checked exception");
}
}
/**
* Method to conquer the course at the grand finale -- throwing exceptions on your own. This has two parameters:
* throwChecked and throwUnchecked. These determine which "flavor" of exception should be raised. When both
* parameters are false, the method should not raise any exceptions.
*
* @param throwChecked When true, the method should raise a checked exception
* @param throwUnchecked When true (and throwChecked is false), the method should raise an unchecked exception
*/
public void grandFinale(boolean throwChecked, boolean throwUnchecked) throws TimeException, RopeClimbException {
//Need to throw checked exception if throwChecked is true
//Else if throwUnchecked is true then need to throw unchecked exception
if(throwChecked) {
throw new TimeException();
}else if(throwUnchecked) {
throw new RopeClimbException();
}
}
}
As QualifyingCourse and exception classes were not given so not able to test with your things but tested by dummy classes and it is working as expected. Please test in your project and can ask any doubts in comment section.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.