Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

HELP PLEASE! You\'ll find that it contains several methods that have not yet bee

ID: 3701836 • Letter: H

Question

HELP PLEASE!

You'll find that it contains several methods that have not yet been finished. For now, the methods contain only a placeholder return value so that the code will compile. Fill in your own implementation. The uniques() method is extra credit. If you plan to implement it, uncomment it and fill in your implementation. You'll also want to uncomment the corresponding unit test already written for you.

The test method for counts() is also missing. Please implement this. So far, you've been using the JUnit methods:

assertTrue()

assertFalse()

assertEquals()

We now have assertArrayEquals(A, B) which tests if the contents of the arrays that A and B reference are the same. You can see its use in some of the other test methods. You'll find it useful for the test you're writing.

You may not use the Arrays class in your methods for this assignment.


package arraypractice;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;


public class ArrayPracticeTest {

/**
* Test of uniques method, of class ArrayPractice.
*/
// @Test
// public void testUniques() {
// System.out.println("uniques");
// int[] A = {21,16,5,31,8,1,9,1,16,4,2,16};
// int[] expResult = {21,16,5,31,8,1,9,4,2};
// int[] result = ArrayPractice.uniques(A);
// assertArrayEquals(expResult, result);
// }
  

Explanation / Answer

package com.chegg.junit;

import java.util.Collections;

import java.util.HashSet;

import java.util.LinkedHashSet;

import java.util.List;

import java.util.Set;

import org.junit.Test;

import static org.junit.Assert.*;

public class ArrayPracticeTest {

// /**

// * @param args

// */

// public static void main(String[] args) {

// // TODO Auto-generated method stub

//

// }

/**

* Test of uniques method, of class ArrayPractice.

*/

@Test

public void testUniques() {

System.out.println("uniques");

int[] A = {21,16,5,31,8,1,9,1,16,4,2,16};

int[] expResult = {21,16,5,31,8,1,9,4,2};

int[] result = ArrayPracticeTest.uniques(A);

System.out.println(result);

assertArrayEquals(result,expResult);

}

/**

* Description : Here method return unique lit

* @param A

* @return

*/

public static int[] uniques(int[] A)

{

// Linked Hash Set preserve the order of insertion and maintain unique value

Set<Integer> s = new LinkedHashSet<Integer>();

// Iterate the array

for (Integer a : A)

{

s.add(a);

}

  

System.out.println(s.size() + " distinct words: " + s);

int[] uniqueArray = new int[s.size()];

// Loading elements into unique array

int i=0;

for(Object o: s.toArray())

{

System.out.println(o);

uniqueArray[i] = ((Integer)o).intValue();

i++;

}

// Return unique array

return uniqueArray;

}

}