Quantcast
Channel: Programmers Heaven Forums RSS Feed
Viewing all articles
Browse latest Browse all 2703

Java TDD Help! Need to create a Library Class to pass 3 tests..

$
0
0
I've tried over and over again to try to get this working but I just don't know what I need to do. The code needs to include an instance variable that is an array of Strings, at least one loop, and one conditional statement. I'm not sure how to do that.. Below are the 3 tests that I need to pass. Below that is my current Library Class. Please assist me in getting me in the right direction.

public class TestLibrary
02	{
03	 
04	    /**
05	     * When a library is created, it should know its space.
06	     */
07	    @Test
08	    public void testInitialization()
09	    {
10	        Library library;
11	        library = new Library(10);
12	        assertEquals(10, library.getSize());
13	        assertEquals(0, library.getNumBooks());
14	    }
15	     
16	    /**
17	    * Tests that you can add books to the Library
18	    */
19	    @Test
20	    public void testAddBook()
21	    {
22	        Library library;
23	        library = new Library(3);
24	        library.addBook("Call of the Wild");
25	        assertEquals(1, library.getNumBooks());
26	        library.addBook("War and Peace");
27	        assertEquals(2, library.getNumBooks());
28	        library.addBook("Sleep Time Stories");
29	        assertEquals(3, library.getNumBooks());
30	        library.addBook("Should not get added");
31	        assertEquals(3, library.getNumBooks());
32	    }
33	     
34	    /**
35	    * Tests that you can find the books in the Library
36	    */
37	    @Test
38	    public void testFindBook()
39	    {
40	        Library library;
41	        library = new Library(3);
42	        library.addBook("Call of the Wild");
43	        library.addBook("War and Peace");
44	        library.addBook("Sleep Time Stories");
45	        library.addBook("Should not get added");
46	        assertTrue(library.find("Call of the Wild"));
47	        assertTrue(library.find("War and Peace"));
48	        assertTrue(library.find("Sleep Time Stories"));
49	        assertFalse(library.find("Should not get added"));
50	    }
51	 
52	}





And here's my Library Class so far

public class Library 
{
	private int numBooks;
	private String theBooks[];
	/**
	 * Create an instance
	 * 
	 * @param theSize Maximum number of books the library can hold.
	 */
	public Library(int theSize) 
	{
		theBooks = new String[theSize];
	}
	/**
	 * @return the size of the library
	 */
	public int getSize()
	{
		return theBooks.length;
	}
	/**
	 * @return the number of books in the library out
	 */
	public int getNumBooks()
	{
		return numBooks;
	}

	public boolean find(String book) 
	{
		return false;
	}

	public void addBook(String book) 
	{
		if(numBooks == theBooks.length) 
		{
			theBooks[numBooks++] = book;
		}
		return;
	}
}

Viewing all articles
Browse latest Browse all 2703

Trending Articles