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

Java practice questions. I don\'t understand how to answer them (javadocs with y

ID: 668258 • Letter: J

Question

Java practice questions. I don't understand how to answer them (javadocs with your code will help me). Thanks:

Assume that in your test class, you have a field for your data structure named container that contains objects of type String.

Write a single test method that adds an object to your container field and covers both possible cases of the add() method.

Write a test method for removing an existing object from a non-empty data structure.

Case 1:

public void testRemove()
{
String s = “A string object”;
container.add(s);

// Write your assertions;
// You can check whether the removed object equals to what you expect
// you can also use size() and contains() to see whether the object is removed
...
}

Case 2:

public void testRemoveNull()
{
String nullString = null;
Exception exception = null;

// Use try-catch block to see whether you get an exception of expected type
......
}

Case 3:

public void testRemoveNo()
{
// Use try-catch block to see whether you get an exception of expected type
......
}

Write a test method(s) to check if an empty data structure contains an object and if a non-empty data structure has a particular object or not.

Write a test method(s) to check if it a non-empty data structure is empty and if an empty data structure is empty.

Write a test method(s) to check the size of a nonempty and empty data structure. (Consider also adding and removing objects and checking again if the size changes as expected if you haven’t done this in the add/remove test methods). Be sure to check that adding the same String object will not result in duplicate entries.

Write a test method that tests if the data structure’s capacity expanded correctly.

Example:

public void testExpandCapacity()
{
for (int i = 0; i < 10; i++)
{
container.add(“String #” + i);
}

......
}

Explanation / Answer

// use net beans IDE


import java.util.ArrayList;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;
import java.lang.*;

public class JUnitTest {
  
public JUnitTest() {
}
  
@BeforeClass
public static void setUpClass() {
}
  
@AfterClass
public static void tearDownClass() {
}
  
@Before
public void setUp() {
}
  
@After
public void tearDown() {
}
@Test
public void testConcatenate() {
String result = "one"+"two";
assertEquals("onetwo", result);
}
  
@Test
public void testRemove()
{
String s = "A string object";
ArrayList container = new ArrayList();
container.add(s);
//assertTrue (container.contains(s));
container.remove(s);
// Write your assertions;
// You can check whether the removed object equals to what you expect
// you can also use size() and contains() to see whether the object is removed
assertTrue (!container.contains(s));
}
  
@Test
public void testRemovenull()
{
String nullString = null;
//Exception exception=new Exception("java.lang.NullPointerException");
try {
System.out.println(nullString.toString());
}
catch (NullPointerException npe) {
assertEquals("java.lang.NullPointerException",npe.toString());
}
}
  
//Write a test method(s) to check if it a non-empty data structure is empty and if an empty data structure is empty.
@Test
public void testDataNonEmptyStructure()
{
String s = "A string object";
ArrayList container = new ArrayList();
container.add(s);
//assertTrue(container.isEmpty());
assertFalse(container.isEmpty());
}
  
@Test
public void testDataEmptyStructure()
{
ArrayList container = new ArrayList();
assertTrue (container.isEmpty());
}
  
//Write a test method(s) to check if an empty data structure contains an object and if a non-empty data structure has a particular object or not.
@Test
public void testNonEmptyStructure()
{
String s = "Hello";
ArrayList container = new ArrayList();
container.add(s);
assertTrue(container.contains(s));
}
  
@Test
public void testEmptyStructure()
{
ArrayList container = new ArrayList();
String s = "Hello";
//assertTrue(container.contains(s));
assertFalse(container.contains(s));
}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote