Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

JUST need help with answering the TODO question in the test below! package cs271.lab.list; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import static

JUST need help with answering the TODO question in the test below! package cs271.lab.list; 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.LinkedList; import java.util.Iterator; import java.util.Arrays; import java.util.List; import org.junit.After; import org.junit.Before; import org.junit.Test; public class TestIterator { private List list; // See the Java List Interface documentation to understand what all the List methods do ... @Before public void setUp() throws Exception { // list = new ArrayList(); list = new LinkedList(); // TODO also try with a LinkedList - does it make any difference? } @After public void tearDown() throws Exception { list = null; } @Test public void testEmpty() { final var 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 var 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 var 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 List.of, express which values are left in the list // See TestList.java for examples of how to use List.of; also see the Java Arrays // class for more information } @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 final Iterator i = list.iterator(); while (i.hasNext()) { int x = i.next(); sum = sum + x; n++; } // (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); } } 

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

Step: 3

blur-text-image

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

Database Processing Fundamentals, Design, and Implementation

Authors: David M. Kroenke, David J. Auer

14th edition

133876705, 9781292107639, 1292107634, 978-0133876703

More Books

Students also viewed these Databases questions