CSE116 Lecture Code Review 1

CSE116 Lecture Code Review 1 - TESTING FIRST DEVELOPMENT...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
TESTING FIRST DEVELOPMENT – SHOPPING CART CODE ITEM public interface Item { public int cost(); } NONTAXABLE public class NonTaxable implements Item { private int _cost ; public NonTaxable( int cost) { _cost = cost; } public int cost() { return _cost ; } } SHOPPINGCART public class ShoppingCart { private ArrayList<Item> _items ; public ShoppingCart() { _items = new ArrayList<Item>(); } public int subTotal() { int subTotal = 0; for (Item item : _items ) { subTotal = subTotal + item.cost(); } return subTotal; } public void add(NonTaxable nonTaxable) { _items .add(nonTaxable); } } SHOPPINGCARTTESTS public class ShoppingCartTests { ShoppingCart _cart ; @Before public void setUp() throws Exception { _cart = new ShoppingCart(); }
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
@After public void tearDown() throws Exception { _cart = null ; } @Test public void testSubTotalOfEmptyCart() { int expected = 0; int actual = _cart .subTotal(); assertTrue ( "I added nothing, expecting the subtotal to be " +expected+ ", but it was " +actual, expected == actual); } @Test public void testSubTotalOfCartWithOneNonTaxable() { int cost = 345; _cart .add( new NonTaxable(cost)); int expected = cost; int actual = _cart .subTotal(); assertTrue ( "I added an item for " +cost+ " cents, expecting the subtotal to be " +expected+ " cents, but it was " +actual+ " cents." , expected == actual); } } TESTING - HASH SET TESTS public class HashSetTests { private HashSet<String> _set ; @Before public void setUp() throws Exception { _set = new HashSet<String>(); } @After public void tearDown() throws Exception { _set = null ; } @Test public void testAddOneUsingSize() { _set .add( "Fred" ); int expected = 1; int actual = _set .size();
Background image of page 2
( "I expected the size to be " +expected+ ", but the size was " +actual, expected == actual); } @Test public void testAddOneTwiceUsingSize() { _set .add( "Fred" ); _set .add( "Fred" ); int expected = 1; int actual = _set .size(); assertTrue ( "I expected the size to be " +expected+ ", but the size was " +actual, expected == actual); } @Test public void testAddTwoUsingSize() { _set .add( "Fred" ); _set .add( "Wilma" ); int expected = 2; int actual = _set .size(); assertTrue ( "I expected the size to be " +expected+ ", but the size was " +actual, expected == actual); } @Test public void testAddOneRemoveOneUsingContains() { _set .add( "Fred" ); _set .remove( "Fred" ); boolean expected = false ; boolean actual = _set .contains( "Fred" ); assertTrue ( "I expected the set to not contain 'Fred', but it did" ,expected == actual); } } BAG BAG public class Bag<E> implements Collection<E> { private E [] _values ; /* * _size denotes the number of elements in the bag, and also * the first available position within the array _values * * These are invariant properties of the variable _size, which * we must maintain. */ private
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This document was uploaded on 12/11/2011.

Page1 / 28

CSE116 Lecture Code Review 1 - TESTING FIRST DEVELOPMENT...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online