Question

import java.util.Scanner; public class InRange { private int min; private int max; public InRange(int initialMin, int...

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.
    }
}

Homework Answers

Answer #1

***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));
}
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
import java.util.Scanner; import java.util.Random; public class ScoreDice { private Random r; public ScoreDice(long seed) { r...
import java.util.Scanner; import java.util.Random; public class ScoreDice { private Random r; public ScoreDice(long seed) { r = new Random(seed); } public int rollD6() { return r.nextInt(6) + 1; } public int score() { int roll1 = rollD6(); int roll2 = rollD6(); return scoreWithNumbers(roll1, roll2); }    // You will need to write a static method named scoreWithNumbers. // scoreWithNumbers returns a score based on the values // of its inputs, as such: // // - If both inputs are 1...
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {       ...
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {        Stack<Integer> new_stack = new Stack<>();/* Start with the empty stack */        Scanner scan = new Scanner(System.in);        int num;        for (int i=0; i<10; i++){//Read values            num = scan.nextInt();            new_stack.push(num);        }        int new_k = scan.nextInt(); System.out.println(""+smallerK(new_stack, new_k));    }     public static int smallerK(Stack s, int k) {       ...
Fix the program: what if the input is three? import java.util.Scanner public class Test { public...
Fix the program: what if the input is three? import java.util.Scanner public class Test { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter an integer: "); int m = in.nextInt(); System.out.print("Enter another integer: "); int n = in.nextInt(); System.out.println(m + " " + n); } }
Covert the following Java program to a Python program: import java.util.Scanner; /* Calculates and displays the...
Covert the following Java program to a Python program: import java.util.Scanner; /* Calculates and displays the area of a rectangle * based on the width and length entered by the user. */ public class RectangleArea2 {             public static void main(String[] args) { int length; //longer side of rectangle             int width; //shorter side of rectangle int area; //calculated area of rectangle Scanner input = new Scanner(System.in);                               System.out.print("Enter the length: ");            length = input.nextInt(); System.out.print("Enter...
import java.util.Scanner; public class ListDriver {    /*    * main    *    * An...
import java.util.Scanner; public class ListDriver {    /*    * main    *    * An array based list is populated with data that is stored    * in a string array. User input is retrieved from that std in.    * A text based menu is displayed that contains a number of options.    * The user is prompted to choose one of the available options:    * (1) Build List, (2) Add item, (3) Remove item, (4) Remove...
import java.util.Scanner; public class FindMinLength { public static int minLength(String[] array) { int minLength = array[0].length();...
import java.util.Scanner; public class FindMinLength { public static int minLength(String[] array) { int minLength = array[0].length(); for (int i = 0; i < array.length; i++) { if (array[i].length() < minLength) minLength = array[i].length(); } return minLength; } public static void main(String[] args) { Scanner in = new Scanner(System.in); String[] strings = new String[50]; for (int i = 0; i < strings.length; i++) { System.out.print("Enter string " + (i + 1) + ": "); strings[i] = in.nextLine(); } System.out.println("Length of smallest...
Covert the following Java program to a Python program: import java.util.Scanner; /** * Displays the average...
Covert the following Java program to a Python program: import java.util.Scanner; /** * Displays the average of a set of numbers */ public class AverageValue { public static void main(String[] args) { final int SENTINEL = 0; int newValue; int numValues = 0;                         int sumOfValues = 0; double avg; Scanner input = new Scanner(System.in); /* Get a set of numbers from user */ System.out.println("Calculate Average Program"); System.out.print("Enter a value (" + SENTINEL + " to quit): "); newValue =...
In the attached FlexArray Java class, implement a method public int delete (int location) { }...
In the attached FlexArray Java class, implement a method public int delete (int location) { } that deletes the integer value stored at location in the array, returns it, and ensures that the array values are contiguous.  Make sure to handle the array empty situation.  What is the time-complexity of the method, if the array size is n. ***************************************************************************************************************************** public class FlexArray { int [] array; private int size; private int capacity; public FlexArray() { capacity=10; size=0; array=new int[10]; } public FlexArray(int...
Stack2540Array import java .io .*; import java . util .*; public class Stack2540Array { int CAPACITY...
Stack2540Array import java .io .*; import java . util .*; public class Stack2540Array { int CAPACITY = 128; int top ; String [] stack ; public Stack2540Array () { stack = new String [ CAPACITY ]; top = -1; } 1 3.1 Implement the stack ADT using array 3 TASKS public int size () { return top + 1; } public boolean isEmpty () { return (top == -1); } public String top () { if ( top == -1)...
public class Date {    private int month;    private int day;    private int year;...
public class Date {    private int month;    private int day;    private int year;    public Date( int monthValue, int dayValue, int yearValue )    {       month = monthValue;       day = dayValue;       year = yearValue;    }    public void setMonth( int monthValue )    {       month = monthValue;    }    public int getMonth()    {       return month;    }    public void setDay( int dayValue )    {       day =...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT