• Need Help with Basic Java try-catch
    13 replies, posted
Hello all, I'm trying to do this lab for my intro to object oriented programming class. •I figure a good majority of you are familiar with code including Java, so I came to you guys for advice because most if not all of my friends are clueless as well. There are three .java files in this project. •ProvidedCode was given and is not modified in any way, shape, or form. •StudentCode is where we write our methods. •StudentTests is provided and unmodified like ProvidedCode and runs Junit tests. ProvidedCode.java [code]/** *This code is provided to you *YOU MAY NOT CHANGE ANYTHING IN THIS FILE * * @author •Jan Plane */ public class ProvidedCode { private final static int HIGH_VAL = 100; private final static int LOW_VAL = 10; private int myVal; /** Constructs an object with the value 0 */ public ProvidedCode(){ this(0); } /** Constructs an object with the value passed */ public ProvidedCode(int inVal){ myVal = inVal; } /** Returns "yes" if the value of the instance variable is * between the HIGH and the LOW or the value "no" if it is not. * @throws ArithmeticException if it matches the HIGH_VAL * @throws NullPointerException if it matches the LOW_VAL * @return true or false */ public String between(){ if (myVal == HIGH_VAL){ throw new ArithmeticException("matches"); } if (myVal == LOW_VAL){ throw new NullPointerException("matches"); } if (myVal < HIGH_VAL && myVal > LOW_VAL){ return "yes"; }else{ return "no"; } } /** Returns "yes" if the value of the parameter is * between the HIGH and the LOW or the value "no" if it is not. * @throws RuntimeException if it matches the instance variable * @throws ArithmeticException if it matches the HIGH_VAL * @throws NullPointerException if it matches the LOW_VAL * @return "yes" or "no" */ public String between(int inVal){ if (inVal == myVal){ throw new RuntimeException("matches"); } if (inVal == HIGH_VAL){ throw new ArithmeticException("matches"); } if (inVal == LOW_VAL){ throw new NullPointerException("matches"); } if (inVal < HIGH_VAL && inVal > LOW_VAL){ return "yes"; }else{ return "no"; } } }[/code] StudentCode.java [code]/*Students must implement these methods * to do the task described WITHOUT USING * EVEN ONE CONDITIONAL STATEMENT OF ANY KIND * (no loops, no ifs, no switches, no * conditional operators)!!! * You must get the correct result in the method * by catching an exception thrown in the code provided. */ public class StudentCode { /*Returns one of the following Strings after * comparing the instance data member * from the class passed to its High and * Low Values: * - "yes" if the value of the instance data member * is between the HIGH and LOW * - "no" if it is not between the HIGH and LOW * - "matches Higher Value" if it is the same the high * - •or "matches Lower Value" if it is the same as the low */ public static String getBetween(ProvidedCode myParam){ ProvidedCode checkCode = new ProvidedCode(); String result = ""; try { result = checkCode.between(); System.out.println(result); } catch (ArithmeticException e) { System.out.println(e.getMessage() + " Higher Value"); } catch (NullPointerException e) { System.out.println(e.getMessage() + " Lower Value"); } return result; } /*Returns one of the following Strings after * comparing the instance data member * from the class passed to its High and * Low Values: * - "yes" if the value of the parameter * is between the HIGH and LOW * - "no" if it is not between the HIGH and LOW * - "matches Higher Value" if it is the same the high * - "matches Lower Value" if it is the same as the low * - "matches Parameter" it the instance value matches the parameter */ public static String getBetween(ProvidedCode myParam, int param){ ProvidedCode checkCode = new ProvidedCode(); try { System.out.println(checkCode.between(param)); } catch (ArithmeticException e) { System.out.println(e.getMessage() + " Higher Value"); } catch (NullPointerException e) { System.out.println(e.getMessage() + " Lower Value"); } catch (RuntimeException e) { System.out.println(e.getMessage() + " Parameter"); } return checkCode.between(); } }[/code] StudentTests.java [code]import static org.junit.Assert.*; import org.junit.Test; public class StudentTests { @Test public void testInstanceBetween() { { ProvidedCode myObj = new ProvidedCode(4); String actualOutput = StudentCode.getBetween(myObj); String expectedOutput = "no"; assertTrue(actualOutput.equals(expectedOutput)); } { ProvidedCode myObj = new ProvidedCode(50); String actualOutput = StudentCode.getBetween(myObj); String expectedOutput = "yes"; assertTrue(actualOutput.equals(expectedOutput)); } { ProvidedCode myObj = new ProvidedCode(10); String actualOutput = StudentCode.getBetween(myObj); String expectedOutput = "matches Lower Value"; assertTrue(actualOutput.equals(expectedOutput)); } { ProvidedCode myObj = new ProvidedCode(100); String actualOutput = StudentCode.getBetween(myObj); String expectedOutput = "matches Higher Value"; assertTrue(actualOutput.equals(expectedOutput)); } } @Test public void testParamBetween() { { ProvidedCode myObj = new ProvidedCode(4); String actualOutput = StudentCode.getBetween(myObj,7); String expectedOutput = "no"; assertTrue(actualOutput.equals(expectedOutput)); } { ProvidedCode myObj = new ProvidedCode(4); String actualOutput = StudentCode.getBetween(myObj,50); String expectedOutput = "yes"; assertTrue(actualOutput.equals(expectedOutput)); } { ProvidedCode myObj = new ProvidedCode(4); String actualOutput = StudentCode.getBetween(myObj,10); String expectedOutput = "matches Lower Value"; assertTrue(actualOutput.equals(expectedOutput)); } { ProvidedCode myObj = new ProvidedCode(4); String actualOutput = StudentCode.getBetween(myObj,100); String expectedOutput = "matches Higher Value"; assertTrue(actualOutput.equals(expectedOutput)); } { ProvidedCode myObj = new ProvidedCode(4); String actualOutput = StudentCode.getBetween(myObj,4); String expectedOutput = "matches Parameter"; assertTrue(actualOutput.equals(expectedOutput)); } } } [/code] For some reason when I run it (running StudentTests.java), I run into failures on the second test for both testInstanceBetween() and testParamBetween(). What am I doing wrong here? •I'm open to hear advice and learn.
I think when an exception is thrown the return value is the default value of string, aka null (i think, haven't used java much), inside the between methods. You should try setting the result variable to its correct value depending on what exception you're catching. [editline].[/editline] Actually now that i've looked a bit closer I don't think that's what's causing it to fail... i think. :v:
Very simple mistake you've made. In StudentCode.java, the first line in your getBetween() methods... [code]ProvidedCode checkCode = new ProvidedCode();[/code] should be... [code]ProvidedCode checkCode = myParam;[/code] because you want to test the object you passed as argument. Get acquainted with your debugger. It's your best friend.
[QUOTE=ktr;47440976]Very simple mistake you've made. In StudentCode.java, the first line in your getBetween() methods... [code]ProvidedCode checkCode = new ProvidedCode();[/code] should be... [code]ProvidedCode checkCode = myParam;[/code] because you want to test the object you passed as argument. Get acquainted with your debugger. It's your best friend.[/QUOTE] I tried this, yet it gave me errors in the junit tests in Eclipse as opposed to failures as before. What am I doing wrong?
You will have to be more specific than that. What are the errors exactly?
[QUOTE=ktr;47441679]You will have to be more specific than that. What are the errors exactly?[/QUOTE] Here are the errors I'm running into in junit tests: [url]http://i.gyazo.com/3027402bb974a1f0aa0a0e75fd1b976e.png[/url] [url]http://i.gyazo.com/c3589a4ecba77c633d8d992a14897432.png[/url]
Your getBetween() methods are suppose to return a string, but you're directly printing to console. This is why it if failing the JUnit tests.
[QUOTE=ktr;47441788]Your getBetween() methods are suppose to return a string, but you're directly printing to console.[/QUOTE] So instead of System.out.println(...);, I should be doing a return?
[QUOTE=NickelobUltra;47441806]So instead of System.out.println(...);, I should be doing a return?[/QUOTE] Try it out. ;)
[QUOTE=ktr;47441810]Try it out. ;)[/QUOTE] Still running into the same errors :(
Paste your code here. I have your code on my end working just fine.
[QUOTE=ktr;47441874]Paste your code here. I have your code on my end working just fine.[/QUOTE] StudentCode.java [code]/*Students must implement these methods * to do the task described WITHOUT USING * EVEN ONE CONDITIONAL STATEMENT OF ANY KIND * (no loops, no ifs, no switches, no * conditional operators)!!! * You must get the correct result in the method * by catching an exception thrown in the code provided. */ public class StudentCode { /*Returns one of the following Strings after * comparing the instance data member * from the class passed to its High and * Low Values: * - "yes" if the value of the instance data member * is between the HIGH and LOW * - "no" if it is not between the HIGH and LOW * - "matches Higher Value" if it is the same the high * - or "matches Lower Value" if it is the same as the low */ public static String getBetween(ProvidedCode myParam){ ProvidedCode checkCode = myParam; try { //System.out.println(checkCode.between()); return checkCode.between(); } catch (ArithmeticException e) { //System.out.println(e.getMessage() + " Higher Value"); return checkCode.between(); } catch (NullPointerException e) { //System.out.println(e.getMessage() + " Lower Value"); return checkCode.between(); } } /*Returns one of the following Strings after * comparing the instance data member * from the class passed to its High and * Low Values: * - "yes" if the value of the parameter * is between the HIGH and LOW * - "no" if it is not between the HIGH and LOW * - "matches Higher Value" if it is the same the high * - "matches Lower Value" if it is the same as the low * - "matches Parameter" it the instance value matches the parameter */ public static String getBetween(ProvidedCode myParam, int param){ ProvidedCode checkCode = myParam; try { //System.out.println(checkCode.between(param)); return checkCode.between(param); } catch (ArithmeticException e) { //System.out.println(e.getMessage() + " Higher Value"); return checkCode.between(param); } catch (NullPointerException e) { //System.out.println(e.getMessage() + " Lower Value"); return checkCode.between(param); } catch (RuntimeException e) { //System.out.println(e.getMessage() + " Parameter"); return checkCode.between(param); } } }[/code]
Think what you're doing. You are returning the same string for all conditions, regardless if a specific exception was thrown or not. Where you had System.out.println( <some string> );, it should be return <some string>;. Being that you're at the cusp of getting it right, here is the corrected code. [code]/*Students must implement these methods * to do the task described WITHOUT USING * EVEN ONE CONDITIONAL STATEMENT OF ANY KIND * (no loops, no ifs, no switches, no * conditional operators)!!! * You must get the correct result in the method * by catching an exception thrown in the code provided. */ public class StudentCode { /*Returns one of the following Strings after * comparing the instance data member * from the class passed to its High and * Low Values: * - "yes" if the value of the instance data member * is between the HIGH and LOW * - "no" if it is not between the HIGH and LOW * - "matches Higher Value" if it is the same the high * - •or "matches Lower Value" if it is the same as the low */ public static String getBetween(ProvidedCode myParam) { try { return myParam.between(); } catch (ArithmeticException e) { return e.getMessage() + " Higher Value"; } catch (NullPointerException e) { return e.getMessage() + " Lower Value"; } } /*Returns one of the following Strings after * comparing the instance data member * from the class passed to its High and * Low Values: * - "yes" if the value of the parameter * is between the HIGH and LOW * - "no" if it is not between the HIGH and LOW * - "matches Higher Value" if it is the same the high * - "matches Lower Value" if it is the same as the low * - "matches Parameter" it the instance value matches the parameter */ public static String getBetween(ProvidedCode myParam, int param) { try { return myParam.between(param); } catch (ArithmeticException e) { return e.getMessage() + " Higher Value"; } catch (NullPointerException e) { return e.getMessage() + " Lower Value"; } catch (RuntimeException e) { return e.getMessage() + " Parameter"; } } }[/code]
[QUOTE=ktr;47441905]Think what you're doing. You are returning the same string for all conditions, regardless if a specific exception was thrown or not. Where you had System.out.println( <some string> );, it should be return <some string>;. Being that you're at the cusp of getting it right, here is the corrected code. [code]/*Students must implement these methods * to do the task described WITHOUT USING * EVEN ONE CONDITIONAL STATEMENT OF ANY KIND * (no loops, no ifs, no switches, no * conditional operators)!!! * You must get the correct result in the method * by catching an exception thrown in the code provided. */ public class StudentCode { /*Returns one of the following Strings after * comparing the instance data member * from the class passed to its High and * Low Values: * - "yes" if the value of the instance data member * is between the HIGH and LOW * - "no" if it is not between the HIGH and LOW * - "matches Higher Value" if it is the same the high * - •or "matches Lower Value" if it is the same as the low */ public static String getBetween(ProvidedCode myParam) { try { return myParam.between(); } catch (ArithmeticException e) { return e.getMessage() + " Higher Value"; } catch (NullPointerException e) { return e.getMessage() + " Lower Value"; } } /*Returns one of the following Strings after * comparing the instance data member * from the class passed to its High and * Low Values: * - "yes" if the value of the parameter * is between the HIGH and LOW * - "no" if it is not between the HIGH and LOW * - "matches Higher Value" if it is the same the high * - "matches Lower Value" if it is the same as the low * - "matches Parameter" it the instance value matches the parameter */ public static String getBetween(ProvidedCode myParam, int param) { try { return myParam.between(param); } catch (ArithmeticException e) { return e.getMessage() + " Higher Value"; } catch (NullPointerException e) { return e.getMessage() + " Lower Value"; } catch (RuntimeException e) { return e.getMessage() + " Parameter"; } } }[/code][/QUOTE] This makes a lot more sense than what my TA ever explained, considering they never really went over returning the exception message with a string added on the end. Thank you so much for your help!
Sorry, you need to Log In to post a reply to this thread.