import java.util.Scanner;
public class InRange {
private int min;
private int max;
public InRange(int initialMin, int initialMax) {
min = initialMin;
max = initialMax;
}
// You need to write two instance methods:
// 1.) A method named inRange, which takes an int.
// This returns true if the int is within the given range
// (inclusive), else false.
//
// 2.) A method named outOfRange which takes an int.
// This returns false if the int is within the given range
// (inclusive), else true. This method calls inRange.
//
// outOfRange always does the exact opposite of inRange.
// FOR FULL CREDIT, outOfRange MUST CALL inRange and negate the
result!
//
// TODO - write your code below this comment.
public boolean inRange(int a){
return a >= min && a <= max;
}
public boolean outOfRange(int a){
return !inRange(a);
}
// Do not Modify main()
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Low end of range: ");
int min = input.nextInt();
System.out.print("High end of range: ");
int max = input.nextInt();
System.out.print("Number: ");
int num = input.nextInt();
InRange range = new InRange(min, max);
System.out.println("In range: " + range.inRange(num));
System.out.println("Out of range: " + range.outOfRange(num));
}
}
NEED HELP WITH TEST
import static org.junit.Assert.assertEquals; import org.junit.Test; public class InRangeTest { @Test public void testInRangeAtLowEndOfRange() { InRange range = new InRange(5, 10); assertEquals(true, range.inRange(5)); // TODO: add an assertEquals statement below which // checks that InRange.outOfRange returns false for the // same parameter above. } @Test public void testInRangeBetweenRange() { InRange range = new InRange(1, 10); assertEquals(true, range.inRange(5)); // TODO: add an assertEquals statement below which // checks that InRange.outOfRange returns false for the // same parameter above. } @Test public void testInRangeAtHighEndOfRange() { InRange range = new InRange(1, 5); assertEquals(true, range.inRange(5)); // TODO: add an assertEquals statement below which // checks that InRange.outOfRange returns false for the // same parameter above. } @Test public void testInRangeOutOfRangeLeft() { InRange range = new InRange(2, 10); assertEquals(false, range.inRange(1)); // TODO: add an assertEquals statement below which // checks that InRange.outOfRange returns true for the // same parameter above. } @Test public void testInRangeOutOfRangeRight() { InRange range = new InRange(1, 9); assertEquals(false, range.inRange(10)); // TODO: add an assertEquals statement below which // checks that InRange.outOfRange returns true for the // same parameter above. } }
***Please upvote or thumbsup if you liked the answer***
Screenshot of the JUnit code:-
Output:-
In Eclipse Right Click on Junit File -> Run As -> Junit Test
JUnit code to copy:-
package junit.RangeCheck;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class InRangeTest {
@Test
public void testInRangeAtLowEndOfRange() {
InRange range = new InRange(5, 10);
assertEquals(true, range.inRange(5));
assertEquals(false, range.outOfRange(5));
}
@Test
public void testInRangeBetweenRange() {
InRange range = new InRange(1, 10);
assertEquals(true, range.inRange(5));
assertEquals(false, range.outOfRange(5));
}
@Test
public void testInRangeAtHighEndOfRange() {
InRange range = new InRange(1, 5);
assertEquals(true, range.inRange(5));
assertEquals(false, range.outOfRange(5));
}
@Test
public void testInRangeOutOfRangeLeft() {
InRange range = new InRange(2, 10);
assertEquals(false, range.inRange(1));
assertEquals(true, range.outOfRange(1));
}
@Test
public void testInRangeOutOfRangeRight() {
InRange range = new InRange(1, 9);
assertEquals(false, range.inRange(10));
assertEquals(true, range.outOfRange(10));
}
}
Get Answers For Free
Most questions answered within 1 hours.