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 elements.
The methods you must implement are:
int findLargestint nums
returns the index to the largest number in nums or if the array is either empty or null
int findSmallestint nums
returns the index to the smallest number in nums or if the array is either empty or null
boolean isContainedint 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 appearsMultipleTimesint 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 hasDuplicatesint nums
returns true if the array nums contains at least one element that appears more than once in the array ie there is a duplicate Should return false otherwise or if the array is either empty or null
int sumAllint nums
returns the sum of all values in the nums array. Should return if the array is either empty or null
double averageint nums
returns the sum of all numbers in the array divided by the number of elements in the array ie the average Should return Double.NaN not a number if the array is null or has elements in which case you can't divide by
Example of how to use ArrayLibrary
Driver method, use to make sure things compile.
public static void mainString args
int nums ;
int result;
result ArrayLibrary.findLargestnums;
System.out.printlnLargest value is result;
if ArrayLibraryhasDuplicatesnums
System.out.printlnArray has duplicate values";
else
System.out.printlnArray values are all unique, no duplicates here";
Testing
Consider the following code:
int nums ;
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.findLargestnums;
ArrayLibrary.findLargestnull;
ArrayLibrary.findLargestnew int;
ArrayLibrary.findSmallestnums;
ArrayLibrary.isContainednums;
ArrayLibrary.isContainednums;
ArrayLibrary.appearsMultipleTimesnums;
ArrayLibrary.appearsMultipleTimesnums;
ArrayLibrary.appearsMultipleTimesnums;
Solution
You have been given two files ArrayLibraryjava 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 StyleCoding
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 StyleCoding and get close to full points for CorrectnessTesting
ArrayLibrary.java
@author Aurora B
public class ArrayLibrary
static int findLargestint
static int findSmallestint
static boolean isContainedint int x
static boolean appearsMultipleTimesint int x
static boolean hasDuplicatesint
static int sumAllint
static double averageint
import org.junit.;
import static org.junit.Assert.;
Test class for ArrayLibrary.
@author Manuel PerezQuinones
@version
public class ArrayLibraryTest
~ Setup
~ Instancestatic variables
private int nums;
Creates a new Calculator object with zero in the accumulator.
@Before
public void setUp
nums new int;
~ 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.findLargestnums;
and then add an assert
failWrite 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
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
