Question

Please help complete the items marked TODO in the code and get the tests to pass:...

Please help complete the items marked TODO in the code and get the tests to pass:

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class TestIterator {

  private List<Integer> list;
  // See the Java List Interface documentation to understand what all the List methods do ...

  @Before
  public void setUp() throws Exception {
    list = new ArrayList<Integer>();
    // TODO also try with a LinkedList - does it make any difference?
  }

  @After
  public void tearDown() throws Exception {
    list = null;
  }

  @Test
  public void testEmpty() {
    final Iterator<Integer> i = list.iterator();
    assertFalse(i.hasNext());
  }

  @Test
  public void testNonempty() {
    list.add(33);
    list.add(77);
    list.add(44);
    list.add(77);
    list.add(55);
    list.add(77);
    list.add(66);
    final Iterator<Integer> i = list.iterator();
    assertTrue(i.hasNext());
    assertEquals(33, i.next().intValue());
    // TODO fix the expected values in the assertions below
    assertTrue(i.hasNext());
    assertEquals(0, i.next().intValue());
    assertTrue(i.hasNext());
    assertEquals(0, i.next().intValue());
    assertTrue(i.hasNext());
    assertEquals(0, i.next().intValue());
    assertTrue(i.hasNext());
    assertEquals(0, i.next().intValue());
    assertTrue(i.hasNext());
    assertEquals(0, i.next().intValue());
    assertTrue(i.hasNext());
    assertEquals(0, i.next().intValue());
    assertFalse(i.hasNext());
  }

  @Test
  public void testRemove() {
    list.add(33);
    list.add(77);
    list.add(44);
    list.add(77);
    list.add(55);
    list.add(77);
    list.add(66);
    final Iterator<Integer> i = list.iterator();
    while (i.hasNext()) {
      if (i.next() == 77) {
        i.remove(); // TODO what happens if you use list.remove(Integer.valueOf(77))?
      }
    }
    // TODO using assertEquals and Arrays.asList, express which values are left in the list
    // See TestList.java for examples of how to use Arrays.asList; also see the Java Arrays
    // class for more information
    fail("Not yet implemented"); // remove this line when done
  }

  @Test
  public void testAverageValues() {
    list.add(33);
    list.add(77);
    list.add(44);
    list.add(77);
    list.add(55);
    list.add(77);
    list.add(66);
    double sum = 0;
    int n = 0;
    // TODO use an iterator and a while loop to compute the average (mean) of the values
    // (defined as the sum of the items divided by the number of items)
    // testNonempty shows how to use an iterator; use i.hasNext() in the while loop condition
    assertEquals(61.3, sum / n, 0.1);
    assertEquals(7, n);
  }
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

// TestIterator.java

import static org.junit.Assert.assertEquals;

import static org.junit.Assert.assertFalse;

import static org.junit.Assert.assertTrue;

import java.util.ArrayList;

import java.util.Arrays;

import java.util.Iterator;

import java.util.LinkedList;

import java.util.List;

import org.junit.After;

import org.junit.Before;

import org.junit.Test;

public class TestIterator {

      private List<Integer> list;

      // See the Java List Interface documentation to understand what all the List

      // methods do ...

      @Before

      public void setUp() throws Exception {

            list = new ArrayList<Integer>();

            // TODO also try with a LinkedList - does it make any difference?

            list = new LinkedList<Integer>(); // no difference

      }

      @After

      public void tearDown() throws Exception {

            list = null;

      }

      @Test

      public void testEmpty() {

            final Iterator<Integer> i = list.iterator();

            assertFalse(i.hasNext());

      }

      @Test

      public void testNonempty() {

            list.add(33);

            list.add(77);

            list.add(44);

            list.add(77);

            list.add(55);

            list.add(77);

            list.add(66);

            final Iterator<Integer> i = list.iterator();

            assertTrue(i.hasNext());

            assertEquals(33, i.next().intValue());

            // TODO fix the expected values in the assertions below

            assertTrue(i.hasNext());

            assertEquals(77, i.next().intValue());

            assertTrue(i.hasNext());

            assertEquals(44, i.next().intValue());

            assertTrue(i.hasNext());

            assertEquals(77, i.next().intValue());

            assertTrue(i.hasNext());

            assertEquals(55, i.next().intValue());

            assertTrue(i.hasNext());

            assertEquals(77, i.next().intValue());

            assertTrue(i.hasNext());

            assertEquals(66, i.next().intValue());

            assertFalse(i.hasNext());

      }

      @Test

      public void testRemove() {

            list.add(33);

            list.add(77);

            list.add(44);

            list.add(77);

            list.add(55);

            list.add(77);

            list.add(66);

            final Iterator<Integer> i = list.iterator();

            while (i.hasNext()) {

                  if (i.next() == 77) {

                        i.remove(); // TODO what happens if you use

                                           // list.remove(Integer.valueOf(77))?

                        // if you use list.remove(Integer.valueOf(77)) while iterating,

                        // it will throw ConcurrentModificationException next time the

                        // iterator is used.

                  }

            }

            // TODO using assertEquals and Arrays.asList, express which values are

            // left in the list

            // See TestList.java for examples of how to use Arrays.asList; also see

            // the Java Arrays

            // class for more information

            assertEquals(Arrays.asList(33, 44, 55, 66), list);

      }

      @Test

      public void testAverageValues() {

            list.add(33);

            list.add(77);

            list.add(44);

            list.add(77);

            list.add(55);

            list.add(77);

            list.add(66);

            double sum = 0;

            int n = 0;

            // TODO use an iterator and a while loop to compute the average (mean)

            // of the values

            // (defined as the sum of the items divided by the number of items)

            // testNonempty shows how to use an iterator; use i.hasNext() in the

            // while loop condition

            Iterator<Integer> it = list.iterator(); // getting iterator

            while (it.hasNext()) {

                  // adding next item to sum

                  sum += it.next().intValue();

                  // incrementing n

                  n++;

            }

            assertEquals(61.3, sum / n, 0.1);

            assertEquals(7, n);

      }

}

/*OUTPUT of JUnit test case*/


Add a comment
Know the answer?
Add Answer to:
Please help complete the items marked TODO in the code and get the tests to pass:...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please help complete the items marked TODO in the code and get the tests to pass:...

    Please help complete the items marked TODO in the code and get the tests to pass: import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import org.junit.After; import org.junit.Before; import org.junit.Test; public class TestPerformance { // TODO run test and record running times for SIZE = 10, 100, 1000, 10000, ... // (choose in conjunction with REPS below up to an upper limit where the clock // running time is in the tens of seconds) // TODO (optional) refactor to DRY // TODO...

  • Please help complete the items marked TODO in the code and get the tests to pass:...

    Please help complete the items marked TODO in the code and get the tests to pass: Someone already answered it, but it was incorrect! import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import org.junit.After; import org.junit.Before; import org.junit.Test; public class TestPerformance { // TODO run test and record running times for SIZE = 10, 100, 1000, 10000, ... // (choose in conjunction with REPS below up to an upper limit where the clock // running time is in the tens of seconds)...

  • Need some assistance coding this assembler exercise. I have included the test file. Thank you very...

    Need some assistance coding this assembler exercise. I have included the test file. Thank you very much. package sequencer; import java.util.List; public class Assembler {    /**    * Creates a new Assembler containing a list of fragments.    *    * The list is copied into this assembler so that the original list will not    * be modified by the actions of this assembler.    *    * @param fragments    */    public Assembler(List<Fragment> fragments) {   ...

  • Java code tasks: Please help with a single class (class Coord) in this program. This project...

    Java code tasks: Please help with a single class (class Coord) in this program. This project is called Panic! We will describe a single-level floorplan of a building, filled with different kinds of people and different kinds of threats. We will simulate how the people seek to evacuate the building to safety, and how the threats spread throughout the building. Various events will be announced, such as person movements, threats spawning, and people reaching safety or succumbing to the threats....

  • I need help with todo line please public class LinkedList { private Node head; public LinkedList()...

    I need help with todo line please public class LinkedList { private Node head; public LinkedList() { head = null; } public boolean isEmpty() { return head == null; } public int size() { int count = 0; Node current = head; while (current != null) { count++; current = current.getNext(); } return count; } public void add(int data) { Node newNode = new Node(data); newNode.setNext(head); head = newNode; } public void append(int data) { Node newNode = new Node(data);...

  • Need help with a number guessing game in java 1) You should store prior guessses in...

    Need help with a number guessing game in java 1) You should store prior guessses in a linked list, and the nodes in the list must follow the order of prior guesses. For example, if the prior guesses are 1000, 2111, 3222 in that order, the nodes must follow the same order 2) You should store the candidate numbers also in a linked list, and the nodes must follow the order of numbers (i.e. from the smallest to the largest)....

  • Can someone please explain this piece of java code line by line as to what they...

    Can someone please explain this piece of java code line by line as to what they are doing and the purpose of each line (you can put it in the code comments). Code: import java.util.*; public class Array { private Integer[] array; // NOTE: Integer is an Object. Array() {     super();     array = new Integer[0]; } Array(Array other) {     super();     array = other.array.clone(); // NOTE: All arrays can be cloned. } void add(int value) {    ...

  • I am not passing some of the code when i run the testing . PriorityQueue public...

    I am not passing some of the code when i run the testing . PriorityQueue public class PriorityQueue<T extends Comparable<T>> implements IPriorityQueue<T> {    private Vector<T> theVector = new Vector<>(0, 1);    private int size = 0;    @Override    public void insert(T element) {        theVector.pushBack(element);        size++;        bubbleUp(); }    @Override    public T peekTop() throws java.util.NoSuchElementException {        if (isEmpty()) {            throw new java.util.NoSuchElementException();        }       ...

  • Analyze the code to determine what the code does, how the data is structured, and why...

    Analyze the code to determine what the code does, how the data is structured, and why it is the appropriate data structure (i.e. linked list, stack or queue). Note that these are examples of classes and methods and do not include the "main" or "test" code needed to execute. When you are done with your analysis, add a header describing the purpose of the program (include a description of each class and method), the results of your analysis, and add...

  • Can someone help me to figure that error I have put below. JAVA ----jGRASP exec: javac...

    Can someone help me to figure that error I have put below. JAVA ----jGRASP exec: javac -g P4Program.java P4Program.java:94: error: package list does not exist Iterator i = new list.iterator(); ^ 1 error ----jGRASP wedge2: exit code for process is 1. ----jGRASP: operation complete. Note: Below there are two different classes that work together. Each class has it's own fuctions/methods. import java.util.*; import java.io.*; public class P4Program{ public void linkedStackFromFile(){ String content = new String(); int count = 1; File...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT