Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

the test should pass all the requirements just need to add some code for class Vector so , that when i run VectorTest as JUnit

the test should pass all the requirements just need to add some code for class Vector so , that when i run VectorTest as JUnit it should pass all the requirements , i have included the IVector and VectorTest

public class Vector implements IVector { private IArray theArray = null; private int size = 0; private int capacity; public Vector() { this(0, 1); } public Vector(int size) { this(size, size); } public Vector(int size, int capacity) { this(size, capacity < size ? size : capacity, false); } public Vector(int size, int capacity, boolean debug) { this.size = size; this.capacity = capacity < size ? size : capacity; theArray = new Array(capacity); } @Override public T get(int index) { checkBounds(index); return null; } @Override public void set(int index, T data) { checkBounds(index); } @Override public int getSize() { return size; } @Override public void pushBack(T data) { if (size == capacity) { capacity *= 2; IArray newArray = new Array<>(capacity); for (int i = 0; i < theArray.getSize(); i++) { newArray.set(i, theArray.get(i)); theArray.set(i, null); } theArray = newArray; } } @Override public void popBack() { checkBounds(size - 1); } @Override public T back() { T data = theArray.get(size - 1); return data; } @Override public int getCapacity() { return capacity; } @Override public void shrinkToFit() { IArray newArray = new Array<>(size); int index = 0; while (index < size) { newArray.set(index, theArray.get(index)); theArray.set(index, null); index++; } theArray = newArray; size = capacity = theArray.getSize(); } @Override public String toString() { StringBuilder sb = new StringBuilder(); sb.append(getSize() + " " + getCapacity() + " "); sb.append("[ "); for (int i = 0; i < getSize(); i++) { sb.append(get(i).toString() + " "); } sb.append("]"); return sb.toString(); } @Override public java.util.Iterator iterator() { return new VectorIterator(); } private class VectorIterator implements java.util.Iterator { private int nextIndex = 0; @Override public boolean hasNext() { return nextIndex < getSize(); } @Override public T next() { return get(nextIndex++); } } private void checkBounds(int index) { if (index < 0 || index > size - 1) { throw new IndexOutOfBoundsException(); } } }

Vector test

import static org.junit.Assert.assertEquals; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TestWatcher; import org.junit.runner.Description;

public class VectorTests { private static StringBuilder report = new StringBuilder(); @Rule public TestWatcher watchman = new TestWatcher() { @Override protected void failed(Throwable t, Description description) { report.append(" FAILURE: ").append(description.getMethodName()).append(" "); } @Override protected void succeeded(Description description) { report.append(" Success: ").append(description.getMethodName()).append(" "); } }; @BeforeClass public static void setUpBeforeClass() throws Exception { if (System.getSecurityManager() == null) { System.setSecurityManager(new SecurityManager()); } report.append(" Testing: ").append(VectorTests.class.getSimpleName()).append(" "); } @AfterClass public static void tearDownClass() { System.out.println(report.toString()); } @Test public void arrayHasCorrectCapacityAndSize() { IVector theArray = new Vector<>(1); assertEquals(1, theArray.getSize()); assertEquals(1, theArray.getCapacity()); theArray = new Vector<>(2); assertEquals(2, theArray.getSize()); assertEquals(2, theArray.getCapacity()); theArray = new Vector<>(50, 100); assertEquals(50, theArray.getSize()); assertEquals(100, theArray.getCapacity()); theArray = new Vector<>(100); assertEquals(100, theArray.getSize()); assertEquals(100, theArray.getCapacity()); } @Test public void loadArrayThenIterate() { IVector theArray = new Vector<>(8); assertEquals(8, theArray.getSize()); theArray.set(0, "Dog"); theArray.set(1, "Cat"); theArray.set(2, "Mouse"); theArray.set(3, "Sheep"); theArray.set(4, "Frog"); theArray.set(5, "Fox"); theArray.set(6, "Horse"); theArray.set(7, "Coyote"); StringBuilder sb = new StringBuilder(); for (String s : theArray) { sb.append(s); } assertEquals("DogCatMouseSheepFrogFoxHorseCoyote", sb.toString()); } @Test public void getIsCorrect() { IVector theArray = new Vector<>(8); assertEquals(8, theArray.getSize()); theArray.set(0, "Dog"); theArray.set(1, "Cat"); theArray.set(2, "Mouse"); theArray.set(3, "Sheep"); theArray.set(4, "Frog"); theArray.set(5, "Fox"); theArray.set(6, "Horse"); theArray.set(7, "Coyote"); assertEquals("Sheep", theArray.get(3)); assertEquals("Dog", theArray.get(0)); assertEquals("Coyote", theArray.get(7)); } @Test(expected = java.lang.IndexOutOfBoundsException.class) public void outOfBoundsIndex() { IVector theArray = new Vector<>(8); assertEquals(8, theArray.getSize()); theArray.set(0, "Dog"); theArray.set(1, "Cat"); theArray.set(2, "Mouse"); theArray.set(3, "Sheep"); theArray.set(4, "Frog"); theArray.set(5, "Fox"); theArray.set(6, "Horse"); theArray.set(7, "Coyote"); theArray.get(8); } @Test public void increaseCapacity() { IVector theArray = new Vector<>(0, 1); assertEquals(0, theArray.getSize()); assertEquals(1, theArray.getCapacity()); theArray.pushBack("Dog"); assertEquals(1, theArray.getSize()); assertEquals(1, theArray.getCapacity()); theArray.pushBack("Cat"); assertEquals(2, theArray.getSize()); assertEquals(2, theArray.getCapacity()); theArray.pushBack("Mouse"); assertEquals(3, theArray.getSize()); assertEquals(4, theArray.getCapacity()); theArray.pushBack("Sheep"); assertEquals(4, theArray.getSize()); assertEquals(4, theArray.getCapacity()); theArray.pushBack("Frog"); assertEquals(5, theArray.getSize()); assertEquals(8, theArray.getCapacity()); } @Test public void shrinkToFitWorks() { IVector theArray = new Vector<>(0, 1); theArray.pushBack("Dog"); theArray.pushBack("Cat"); theArray.pushBack("Mouse"); theArray.pushBack("Sheep"); theArray.pushBack("Frog"); assertEquals(5, theArray.getSize()); assertEquals(8, theArray.getCapacity()); theArray.shrinkToFit(); assertEquals(5, theArray.getSize()); assertEquals(5, theArray.getCapacity()); theArray.pushBack("Horse"); assertEquals(6, theArray.getSize()); assertEquals(10, theArray.getCapacity()); } }

IVector

public interface IVector extends IArray { public void pushBack(T data); public void popBack(); public T back(); public int getCapacity(); public void shrinkToFit(); }

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

Repairing And Querying Databases Under Aggregate Constraints

Authors: Sergio Flesca ,Filippo Furfaro ,Francesco Parisi

2011th Edition

146141640X, 978-1461416401

More Books

Students also viewed these Databases questions

Question

4. What are the current trends in computer software platforms?

Answered: 1 week ago