CIS191-s12/Project 7

From MCIS Wiki

Jump to: navigation, search

Due Monday, March 21

This program will not contain a GUI. Create a new Java project in Netbeans and add classes / interfaces as needed. Attached below is a testing file that will be used to grade your assignment.

When you run this file you'll see a "green light" if all of the tests pass.

Note that there is no main program - just the test.

Contents

The Collection Interface

Create an interface called Collection. This will represent a collection of String objects that has the following methods:

  • the isEmpty method returns a boolean: true if the collection is empty.
  • the size method returns an int, the current size of the collection.
  • the add method places a string in the collection
  • the get method take a String out of the collection

You should be able to figure out the inputs and return types of these methods based on this description.

The Collection Base Class

Create an abstract class for collections. This will implement the Collection interface. This base has an instance variable containing an ArrayList of strings - this is the body of the collection. Implement the isEmpty, size, and add methods in CBase but don't fill in the get method.

The Queue Class

Extend the CBase class and add two more methods: toString and get. The get method should take the appropriate value out of the ArrayList and return it.

The toString method should return "Queue of x Strings", where x is the size.

The Stack Class

This is similar to Queue except for the order of the things removed from the collection. Add a toString method similar to that in Queue.

Testing

Place the following file in your project. The best way to do this is to add a unit test: Click "new" and select other - look for JUnit and add a Junit test. This will go under tests rather than src - make sure it is in the same package as your source code. You want to select JUnit 4.x. Run the test by using "Run Tests" on the run menu. Replace the code in the test file with the following:



package queuesandstacks;

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.*;


public class QueueTest {
    public String values[];
    public Queue queue;
    public Stack stack;
    public QueueTest() {
    }

    @BeforeClass
    public static void setUpClass() throws Exception {
    }

    @AfterClass
    public static void tearDownClass() throws Exception {
    }

    @Before
    public void setUp() {
        values = new String[5];
        values[0] = "Zero";
        values[1] = "One";
        values[2] = "Two";
        values[3] = "Three";
        values[4] = "Four";
        queue = new Queue();
        stack = new Stack();
    }

    @After
    public void tearDown() {
    }

    @Test
    public void testEmptyQueue() {
        assertEquals("Initial Queue should be empty", queue.isEmpty(), true);
    }
    @Test
    public void testEmptyStack() {
        assertEquals("Initial Stack should be empty", stack.isEmpty(), true);
    }
    @Test
    public void testNonEmptyQueue() {
        queue.add("Stuff");
        assertEquals("Initial Queue should be empty", queue.isEmpty(), false);
    }
    @Test

    public void testNonEmptyStack() {
        stack.add("Stuff");
        assertEquals("Initial Stack should be empty", stack.isEmpty(), false);
    }
    @Test
    public void testQueueAddGet() {
        for (String s : values) queue.add(s);
        for (String s : values) queue.get();
        assertEquals("Queue should be empty", queue.isEmpty(), true);
    }
    @Test
    public void testStackAddGet() {
        for (String s : values) stack.add(s);
        for (String s : values) stack.get();
        assertEquals("Stack should be empty", stack.isEmpty(), true);
    }
    @Test
    public void testQueueOrder() {
        for (String s : values) queue.add(s);
        for (int i = 0; i < values.length; i++)
            assertEquals("Expecting " + values[i], queue.get(), values[i]);
    }
    @Test
    public void testStackOrder() {
        for (String s : values) stack.add(s);
        for (int i = values.length-1; i >= 0; i--)
            assertEquals("Expecting " + values[i], stack.get(), values[i]);
    }
    @Test
    public void testQueueSize() {
        for (int i = 0; i < 12; i++) queue.add("Junk");
        assertEquals("Expecting length 12 values", queue.size(), 12);
    }
    @Test
    public void testStackSize() {
        for (int i = 0; i < 9; i++) stack.add("Junk");
        assertEquals("Expecting 9 values", stack.size(), 9);
    }

}

This is called a "unit test". When you run this file you'll see a "green light" if all of the tests pass.

Note that there is no main program - just the test.

Personal tools