Question: You have to write a static class that has operations on arrays of integers. Each method does one specific action. All of the methods take

You have to write a static class that has operations on arrays of integers. Each method does one specific action. All of the methods take at least one argument (nums), an array of integers and return some value. All methods should check that the array argument nums is not null before accessing it. In addition, the nums argument might be an array of 0 elements.
The methods you must implement are:
int findLargest(int nums[])
returns the index to the largest number in nums or -1 if the array is either empty or null
int findSmallest(int nums[])
returns the index to the smallest number in nums or -1 if the array is either empty or null
boolean isContained(int nums[], int x)
returns true if x is found in the nums array or false if the element x is not found or the array is either empty or null
boolean appearsMultipleTimes(int nums[], int x)
returns true if x is found multiple times in the nums array. Returns false if x is not found multiple times or the array is either empty or null
boolean hasDuplicates(int nums[])
returns true if the array nums contains at least one element that appears more than once in the array (i.e., there is a duplicate). Should return false otherwise or if the array is either empty or null
int sumAll(int nums[])
returns the sum of all values in the nums array. Should return 0 if the array is either empty or null
double average(int nums[])
returns the sum of all numbers in the array divided by the number of elements in the array (i.e., the average). Should return Double.NaN (not a number) if the array is null or has 0 elements (in which case you can't divide by 0).
Example of how to use ArrayLibrary
// Driver method, use to make sure things compile.
public static void main(String args[])
{
int[] nums ={10,4,50,128,-1,49};
int result;
result = ArrayLibrary.findLargest(nums);
System.out.println("Largest value is "+result);
if (ArrayLibrary.hasDuplicates(nums))
System.out.println("Array has duplicate values");
else
System.out.println("Array values are all unique, no duplicates here");
}
Testing
Consider the following code:
int[] nums ={10,4,50,128,4,-1,49};
What would the following calls to the various methods return? If you can fill in the black in the rightmost column, then you can create test cases to make sure that the code is working correctly.
Call Returns...
ArrayLibrary.findLargest(nums); 128
ArrayLibrary.findLargest(null);
ArrayLibrary.findLargest(new int[0]);
ArrayLibrary.findSmallest(nums);
ArrayLibrary.isContained(nums,10);
ArrayLibrary.isContained(nums,4);
ArrayLibrary.appearsMultipleTimes(nums,4);
ArrayLibrary.appearsMultipleTimes(nums,10);
ArrayLibrary.appearsMultipleTimes(nums,77);
Solution
You have been given two files (ArrayLibrary.java Download ArrayLibrary.javaand ArrayLibraryTest.java Download ArrayLibraryTest.java) that have the beginnings of the code for the project.
Add comments to the code so that you gain full points for Style/Coding.
Complete the code for the methods above (and included in the ArrayLibrary.java Download ArrayLibrary.java).
Write test cases to exercise all lines of code in the methods.
As you add more test cases, you will recharge your submission energy. Submit until you get full points for Style/Coding (45.0/45.0) and get close to full points for Correctness/Testing (45.0/45.0).
/**
* ArrayLibrary.java
* @author Aurora B
*/
public class ArrayLibrary
{
static int findLargest(int [])
{
}
static int findSmallest(int [])
{
}
static boolean isContained(int [], int x)
{
}
static boolean appearsMultipleTimes(int [], int x)
{
}
static boolean hasDuplicates(int [])
{
}
static int sumAll(int [])
{
}
static double average(int [])
{
}
}
import org.junit.*;
import static org.junit.Assert.*;
//-------------------------------------------------------------------------
/**
* Test class for ArrayLibrary.
* @author Manuel Perez-Quinones
* @version 2024.01.23
*/
public class ArrayLibraryTest
{
//~ Setup ..............................................
//~ Instance/static variables .............................................
private int nums[];
//----------------------------------------------------------
/**
* Creates a new Calculator object with zero in the accumulator.
*/
@Before
public void setUp()
{
nums = new int[]{100,45,149,250,128,-5,49,45};
}
//~ Public Methods ........................................................
//----------------------------------------------------------
/**
* Check that findLargest() does indeed find the largest value.
*/
@Test
public void checkFindLargest()
{
// leave this next line here and ignore it.
new ArrayLibrary();
// Your testing should call ArrayLibrary as
// shown below...
int found = ArrayLibrary.findLargest(nums);
// and then add an assert
fail("Write your own test cases here");
}
//----------------------------------------------------------
/**
* Check
*/
@Test

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!