Answered step by step
Verified Expert Solution
Link Copied!
Question
1 Approved Answer

I need help with a few errors the code was working before but I have to create testing codes so I don't know whether its

I need help with a few errors the code was working before but I have to create testing codes so I don't know whether its the main or the testing

Here is the testing code

import static org.junit.jupiter.api.Assertions.*;


 

import java.util.ArrayList;


 

import org.junit.jupiter.api.Test;


 

/**
* This class has tests for the methods
* in SimpleStaticMethods. Those methods
* must pass the tests in this class, some
* of which must be written by you. Base
* your tests on the examples given, but
* expand your tests to incorporate a more
* diverse range of interesting values and
* special cases.
*
*
*/
class SimpleStaticMethodsTest {


// Allowable error in floating-point calculations
public static final double EPSILON = 0.000001;


/**
* 1 point
*/
@Test
void testSumStandardLoop() {
assertEquals(45, SimpleStaticMethods.sumStandardLoop(new int[]{1,2,3,4,5,6,7,8,9}));
assertEquals(1116257, SimpleStaticMethods.sumStandardLoop(new int[]{11,3,20,-62,100,5023,-32,83,1111111}));
assertEquals(72074, SimpleStaticMethods.sumStandardLoop(new int[]{1341,23,-420,-6234,12400,53023,-3,833,11111}));
// TODO: At least one more test case
assertEquals(450, SimpleStaticMethods.sumStandardLoop(new int[]{10,20,30,40,50,60,70,80,90}));
}


/**
* 1 point
*/
@Test
void testSumForEach() {
assertEquals(45, SimpleStaticMethods.sumForEach(new int[]{1,2,3,4,5,6,7,8,9}));
assertEquals(1116257, SimpleStaticMethods.sumForEach(new int[]{11,3,20,-62,100,5023,-32,83,1111111}));
assertEquals(72074, SimpleStaticMethods.sumForEach(new int[]{1341,23,-420,-6234,12400,53023,-3,833,11111}));
// TODO: At least one more test case
assertEquals(450, SimpleStaticMethods.sumForEach(new int[]{10,20,30,40,50,60,70,80,90}));
}



/**
* 3 points
*/
@Test
void testProduct() {
assertEquals(120, SimpleStaticMethods.product(new double[] {1,2,3,4,5}), EPSILON);
assertEquals(0.25533999999999996, SimpleStaticMethods.product(new double[] {0.5, 150.2, 0.0034}), EPSILON);
// TODO: At least 3 more tests. Need one test for empty array, and two for long arrays with many interesting values (big, small, negative, positive)
assertEquals(1008.2, SimpleStaticMethods.product(new double[] {0.70,2.50,50,470,90,97,60,67,78,68,25}), EPSILON);
assertEquals(1, SimpleStaticMethods.product(new double[] {}), EPSILON);
assertEquals(290.75, SimpleStaticMethods.product(new double[] {70,250,0.50,-400,50,77,50,57,88,98,0.25}), EPSILON);
}


/**
* 4 points
*/
@Test
void testZipMultiply() {
double[] result1and2 = SimpleStaticMethods.zipMultiply(new double[] {1, 2, 0.5, 100, 12.56}, new double[] {77, 3, 100, 34.5311, 10.23, 7});
assertArrayEquals(new double[] {77, 6, 50, 3453.11, 128.4888}, result1and2, EPSILON);
// TODO: At least 4 more tests. Be sure to cover a broad range of input/output possibilities
assertArrayEquals(new double[] {67, 88, 57, 53.11, 8.4899}, result1and2, EPSILON);
assertArrayEquals(new double[] {77, 6, 50, 3453.11, 1.4888}, result1and2, EPSILON);
assertArrayEquals(new double[] {56, 86, 0.50, 453.11, 28.4888}, result1and2, EPSILON);
assertArrayEquals(new double[] {67, 66, 50, 34,53.11, 12,48.88}, result1and2, EPSILON);
}


/**
* 5 points
*/
@Test
void testSubStringAfter() {
assertEquals("tion", SimpleStaticMethods.subStringAfter('c', "action"));
// TODO: At least 10 more tests. Be sure to cover a broad range of input/output possibilities, including cases where the character is not in the string
assertEquals("", SimpleStaticMethods.subStringAfter('b', "acttion"));
assertEquals("arty", SimpleStaticMethods.subStringAfter('p', "party"));
assertEquals("ing", SimpleStaticMethods.subStringAfter('t', "acting"));
assertEquals("mate", SimpleStaticMethods.subStringAfter('m', "roommate"));
assertEquals("son", SimpleStaticMethods.subStringAfter('r', "person"));
assertEquals("bed", SimpleStaticMethods.subStringAfter('e', "bedroom"));
assertEquals("ode", SimpleStaticMethods.subStringAfter('c', "code"));
assertEquals("use", SimpleStaticMethods.subStringAfter('u', "house"));
assertEquals("lege", SimpleStaticMethods.subStringAfter('l', "college"));
assertEquals("and", SimpleStaticMethods.subStringAfter('n', "candy"));

}


/**
* 4 points
*/
@Test
void testSameStart() {
// You can directly fill these lists with values to conduct your tests
ArrayList list1 = new ArrayList();
list1.add(5);
list1.add(6);
ArrayList list2 = new ArrayList();
list2.add(5);
list2.add(6);
list2.add(7);
// [5,6]
// [5,6,7]
// Result is true because both start with [5,6], which is the entirety of the shorter list
assertTrue(SimpleStaticMethods.sameStart(list1, list2));


ArrayList list3 = new ArrayList();
list3.add(5);
list3.add(6);
list3.add(8);
// [5,6,7]
// [5,6,8]
// Result is false because both differ at the third position (index 2)
assertFalse(SimpleStaticMethods.sameStart(list2, list3));


// TODO: At least 4 more tests. Be sure to cover a broad range of input/output possibilities
// Each test should create new ArrayLists from scratch and add elements to them before
// using either assertTrue or assertFalse to check. Have two assertTrue checks and two
// assertFalse checks.
ArrayList list4 = new ArrayList();
list4.add(4);
list4.add(6);
list4.add(8);



ArrayList list5 = new ArrayList();
list5.add(1);
list5.add(9);
list5.add(4);

assertFalse(SimpleStaticMethods.sameStart(list4, list5));


ArrayList list6 = new ArrayList();
list6.add(4);
list6.add(6);



ArrayList list7 = new ArrayList();
list7.add(7);
list7.add(9);
list7.add(8);

assertFalse(SimpleStaticMethods.sameStart(list6, list7));

ArrayList list8 = new ArrayList();
list8.add(6);
list8.add(8);



ArrayList list9 = new ArrayList();
list9.add(6);
list9.add(8);
list9.add(8);

assertTrue(SimpleStaticMethods.sameStart(list8, list9));


ArrayList list10 = new ArrayList();
list10.add(1);
list10.add(8);
list10.add(8);



ArrayList list11 = new ArrayList();
list11.add(1);
list11.add(8);
list11.add(8);


assertTrue(SimpleStaticMethods.sameStart(list10, list11));




}


/**
* 6 points
*/
@Test
void testAverages() {
double[][] case1 = new double[][]{new double[] {1,2,3,4,5}, new double[] {100,35.324,-342.7}};
assertArrayEquals(new double[] {3, -69.12533333333333}, SimpleStaticMethods.averages(case1), EPSILON);


// TODO: Write two additional tests for the averages method.
// Be sure to cover a broad range of input/output possibilities.
// Each test consists of the declaration and initialization of
// a 2D array of expected results, followed by assertArrayEquals.
// Note that your additional test arrays should not all be the same size.
assertArrayEquals(new double[] {3,-70,100,33}, SimpleStaticMethods.averages(case1), EPSILON);
assertArrayEquals(new double[] {7,-69,125,78,-50,88,98}, SimpleStaticMethods.averages(case1), EPSILON);


}


}

Here is the main And the ones I I'm getting an error in

testSubStringAfter

testZipMultiply

testProduct

testAverages

import java.util.ArrayList;



/**
* Most of the following methods are incomplete.
*  methods according to the provided
* specifications. Your methods must also pass the
* provided tests in SimpleStaticMethodsTest,
* along with other tests you will write.
*
*
*/
public class SimpleStaticMethods {


public static void main(String[] args) {
// Put method calls here if you want to test code using run-simple.bash
}


/**
* This method is given, but you must write unit tests for it.
*
* Compute the sum of elements in an array using
* a standard for loop
*
* @param array integers
* @return sum of input array
*/
public static int sumStandardLoop(int[] array) {
int total = 0;
for(int i = 0; i < array.length; i++) {
total += array[i];
}
return total;
}


/**
* 3 points
*
* Compute the sum of elements in an array using
* a for-each loop
*
* @param array integers
* @return sum of input array
*/
public static int sumForEach(int[] array) {
int total = 0;
for(int x : array) {
total += x;
}
return total;
}



/**
* 3 points
*
* Given an array of doubles, return the product of all
* the numbers in the array.
* (if the array has length 0, a value of 1 will be returned)
*
* @param array of doubles
* @return product of all array values
*/
public static double product(double[] array) {
double prod = 1.0; // Initialize with 1.0 for an emty array
for (double num : array ){
prod *= num;// Multiply each number to the product
}

return prod; // Return the product value
}


/**
* 4 points
*
* Takes two arrays and multiplies pairs of doubles from
* each array together to new array containing
* the products. Specifically, the i-th index of the
* result array contains a1[i]*a2[i]. The result array
* length equals that of the smaller input array. Extra
* elements in the larger input array (if sizes differ)
* are ignored.
*
* @param a1 array of doubles
* @param a2 array of doubles
* @return array of products of doubles from two input arrays
*/
public static double[] zipMultiply(double[] a1, double[] a2) {
// result has length of shorter input array
double[] result = new double[(int) Math.min(a1.length, a2.length)];
for(int i = 0; i < result.length; i++){
result[i] = a1[i] * a2[i];
}
return result;
}


/**
* 4 points
*
* If a given character x is in a given string, then
* return the substring of characters after
* the first occurrence of x. Otherwise return the
* empty string (which is just "").
* This can be accomplished without any loops at
* all if you use methods of the String class.
* Check the Java API for String.
*
* @param x character to search for
* @param s string to get a substring from (not null)
* @return substring after first x, if it exists, else ""
*/
public static String subStringAfter(char x, String s) {
int ind = s.indexOf(x);// find the index of the character
if (ind != -1){// if the character is found
return s.substring(ind + 1);// return the substring after the character
}else{
return ""; // TODO
}

}


/**
* 5 points
*
* Indicate if two ArrayLists of Integers start with the same
* sequence of elements. Specifically, the length of the shorter
* list is used, and only those elements in the two lists are
* compared. In other words, check if the beginning portion of
* one list matches the entirety of another list. If either list
* is empty, then a result of true is returned. Note that you cannot
* know that you should return a result of true until you have looped
* as many iterations as the length of the shorter list. Also be aware
* that non-primitive Integers should be compared using the .equals method.
*
* IMPORTANT: Do not modify the contents of either parameter list.
*
* @param list1 First ArrayList of Integers
* @param list2 Second ArrayList of Integers
* @return Whether elements in shorter list match the
* first elements of the longer list in the same order.
*/
public static boolean sameStart(ArrayList list1, ArrayList list2) {
int shorterLength = Math.min(list1.size(), list2.size());
for(int i = 0; i < shorterLength; i++){// for each index in the shorter list
if (!list1.get(i).equals(list2.get(i))){// if the elements at the index are not equal
return false; // TODO
}
}

return true; // TODO
}


/**
* 5 points
*
* Given a (potentially jagged) 2D array, treat each sub-array
* as a collection of numbers, and determine the average across
* those numbers. The final result is an array with one element for
* each sub-array in the input, which equals the average of the
* values in that sub-array. Simply put, given an array of arrays,
* return an array of averages.
*
* Note: arrayOfArrays[i][j] represents the j-th element of the i-th array.
* If you want to know the length of the i-th array, use arrayOfArrays[i].length.
*
* @param arrayOfArrays array of arrays of numbers, where each sub-array has a length of at least 1
* @return array of averages of the sub-arrays of the input
*/
public static double[] averages(double[][] arrayOfArrays) {
double[] result = new double[arrayOfArrays.length]; // arrayOfArrays.length is the number of sub-arrays
// TODO (Hint: loop through all of the arrays with an outer loop, but have an inner loop through each sub-array that adds up values)
for (int i = 0; i < arrayOfArrays.length; i++){
double amount = 0;// Initialize a variable to store the amount of the values in the current sub array
for (int j = 0; j < arrayOfArrays[i].length; j++){
amount += arrayOfArrays[i][j];// add the value to the amount
}
result[i] = amount / arrayOfArrays[i].length;// divide the amount by the number of values in the sub array to get the average

}
return result;

}
}

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_2

Step: 3

blur-text-image_3

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

Global Strategy

Authors: Mike W. Peng

5th Edition

0357512367, 978-0357512364

More Books

Students explore these related Algorithms questions