Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

A is question about java public class Q1Furthest { /** * Given an array of integers and a special value target, * return the value

A is question about java

public class Q1Furthest { /** * Given an array of integers and a special value target, * return the value in the array that is numerically furthest from * target. If the array contains two different values equally * close to target, return the larger value. If the array is * empty, return target. * * @param in An array of integers * @param target a target value to search for in the array * @return the value in the array that is numerically furthest from * target, returning the larger of equally close values, and * returning target if the array * has no entries. */ public static int findFurthest(int[] in, int target) { // FIXME complete this method return Integer.MAX_VALUE; } } 

This is the test file

import org.junit.FixMethodOrder; import org.junit.Rule; import org.junit.Test; import org.junit.rules.Timeout; import org.junit.runners.MethodSorters; import java.util.Arrays; import java.util.Random; import static junit.framework.TestCase.assertTrue; @FixMethodOrder(MethodSorters.NAME_ASCENDING) public class Q1FurthestTest { @Rule public Timeout globalTimeout = Timeout.millis(500); public static int ITERATIONS = 10; Random r = new Random(); @Test public void testEmpty() { test(new int[]{}, 2, 2); test(new int[]{}, 0, 0); } @Test public void testOne() { test(new int[]{1}, 0, 1); test(new int[]{2}, 2, 2); test(new int[]{2}, 1, 2); test(new int[]{3}, 2, 3); test(new int[]{-1}, 0, -1); } @Test public void testMultiple() { test(new int[]{1, 2}, 1, 2); test(new int[]{1, 2, 3, 4, 5, 6, 7, 8}, 1, 8); test(new int[]{2, 1}, 2, 1); test(new int[]{3, 2}, 1, 3); test(new int[]{8, 3, -5, 2, 1, -8}, -2, 8); test(new int[]{8, 3, -5, 2, 1, 0}, -2, 8); test(new int[]{3, 1}, 1, 3); test(new int[]{0, 9}, 5, 0); test(new int[]{3, 1, 5, -4, 0}, 1, -4); } @Test public void testDuplicate() { test(new int[]{2, 2}, 1, 2); test(new int[]{3, 2, -2, 2}, 1, -2); test(new int[]{1, 3, 1}, 2, 3); test(new int[]{3, 1, 3}, 2, 3); } @Test public void testOther() { test(new int[]{2, 2}, 2, 2); test(new int[]{-4, 2, 1}, 3, -4); test(new int[]{1, 3, 2}, 3, 1); test(new int[]{9, 7, 8}, 8, 9); test(new int[]{1, 0, 1}, 0, 1); test(new int[]{-7, 0, 7}, 0, 7); test(new int[]{2, 2}, 2, 2); test(new int[]{12, 2, 2}, 7, 12); test(new int[]{12, 2, 2}, 8, 2); } void test(int[] values, int target, int expected) { int result = Q1Furthest.findFurthest(values, target); assertTrue("Q1Furthest.findFurthest(" + Arrays.toString(values) + ", " + target + ") returned " + result + ", expected " + expected, result == expected); } } 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions