Author Topic: Need Java Help  (Read 509 times)

Hey so I'm clinically handicapped and 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: [Select]
/**
 *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";
}
}
}

StudentCode.java
Code: [Select]
/*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();
}
}

StudentTests.java
Code: [Select]
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));
}
}
}



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.

C'mon don't like a solid portion of the population here know what the hell to do :'(