5 Replies - 338 Views - Last Post: 06 March 2013 - 03:05 PM Rate Topic: -----

#1 csisock  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 06-March 13

Need Java TDD help! Trying to create a Library Class.

Posted 06 March 2013 - 01:45 PM

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
{

	/**
	 * When a library is created, it should know its space.
	 */
	@Test
	public void testInitialization()
	{
		Library library;
		library = new Library(10);
		assertEquals(10, library.getSize());
		assertEquals(0, library.getNumBooks());
	}
	
	/**
	* Tests that you can add books to the Library
	*/
	@Test
	public void testAddBook()
	{
		Library library;
		library = new Library(3);
		library.addBook("Call of the Wild");
		assertEquals(1, library.getNumBooks());
		library.addBook("War and Peace");
		assertEquals(2, library.getNumBooks());
		library.addBook("Sleep Time Stories");
		assertEquals(3, library.getNumBooks());
		library.addBook("Should not get added");
		assertEquals(3, library.getNumBooks());
	}
	
	/**
	* Tests that you can find the books in the Library
	*/
	@Test
	public void testFindBook()
	{
		Library library;
		library = new Library(3);
		library.addBook("Call of the Wild");
		library.addBook("War and Peace");
		library.addBook("Sleep Time Stories");
		library.addBook("Should not get added");
		assertTrue(library.find("Call of the Wild"));
		assertTrue(library.find("War and Peace"));
		assertTrue(library.find("Sleep Time Stories"));
		assertFalse(library.find("Should not get added"));
	}

}








Here's my current Library Class:



public class Library 
{
	private int size;
	private int numBooks;
	private String theBooks[];
	
	/**
	 * Create an instance
	 * 
	 * @param theSize Maximum number of books the library can hold.
	 */
	public Library(int theSize) 
	{
		size = theSize;
	}
	
	/**
	 * @return the size of the library
	 */
	public int getSize()
	{
		return size;
	}
	
	/**
	 * @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) 
	{
		
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Need Java TDD help! Trying to create a Library Class.

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Need Java TDD help! Trying to create a Library Class.

Posted 06 March 2013 - 01:54 PM

First the constructor of Library should create the array and you do not need the size variable... it is actually the length of the array so no need to try to keep the same information at two places

	public Library(int theSize) 
	{
		theBooks = new String[theSize];
	}
	public int getSize()
	{
		return theBooks.length;
	}



Now your addBook method should:
1) test there is still room in the Library
2) insert that book


	public void addBook(String book) 
	{
	    if(numBooks == theBooks.length) {
               .. no more room what will you do ??
               return;
	    theBooks[numBooks++] = book;
	}


That should be a start
Was This Post Helpful? 0
  • +
  • -

#3 csisock  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 06-March 13

Re: Need Java TDD help! Trying to create a Library Class.

Posted 06 March 2013 - 02:43 PM

I tried what you told me to and it's still not getting that test to pass..
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Need Java TDD help! Trying to create a Library Class.

Posted 06 March 2013 - 02:48 PM

I said it was a start :) What error do you have ?
testInitialization() and testAddBook() should work
Was This Post Helpful? 0
  • +
  • -

#5 csisock  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 06-March 13

Re: Need Java TDD help! Trying to create a Library Class.

Posted 06 March 2013 - 02:52 PM

testAddBook() doesn't work. It's giving me the error expected:<1> but was:<0> at (TestLibrary.java:38)
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Need Java TDD help! Trying to create a Library Class.

Posted 06 March 2013 - 03:05 PM

Mine failed there

library.addBook("Should not get added");

because I coded


	public void addBook(String book) 
	{
		if(numBooks == theBooks.length)
			throw new IllegalStateException("No more room");
		theBooks[numBooks++] = book;
	}


Not having JUnit, your test environment, I wrote mine that seems to work

public class TestLibrary
{

	/**
	 * When a library is created, it should know its space.
	 */
	
	public void testInitialization()
	{
		Library library;
		library = new Library(10);
		assertEquals(10, library.getSize());
		assertEquals(0, library.getNumBooks());
	}
	
	/**
	* Tests that you can add books to the Library
	*/
	public void testAddBook()
	{
		Library library;
		library = new Library(3);
		library.addBook("Call of the Wild");
		assertEquals(1, library.getNumBooks());
		library.addBook("War and Peace");
		assertEquals(2, library.getNumBooks());
		library.addBook("Sleep Time Stories");
		assertEquals(3, library.getNumBooks());
		library.addBook("Should not get added");
		assertEquals(3, library.getNumBooks());
	}
	
	private void assertEquals(int i, int size) {
		if(i == size)
			System.out.println("Yes " + i + " == " + size);
		else
			System.out.println("Error " + i + " != " + size);
		
	}

	public static void main(String[] args) {
		TestLibrary tl = new TestLibrary();
		tl.testInitialization();
		tl.testAddBook();
	}

}


This post has been edited by pbl: 06 March 2013 - 03:07 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1