Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Java TDD Help! Need to create a Library Class to pass 3 TDD tests. I'm clueless.

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java TDD Help! Need to create a Library Class to pass 3 TDD tests. I'm clueless.

    Here's the 3 tests that I need to pass:

    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 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;
    		}
    	}
    }





    Please help, I'm at a loss here. The code needs to include an instance variable that is an array of Strings, at least one loop, and one conditional statement. Please let me know if I'm on the right track of not. I'm so confused about how to do this.
    Last edited by csisock; March 6th, 2013 at 15:10. Reason: Not enough information and improper editing


  2. #2
    Administrator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    24,848
    Thanks
    64
    Thanked 2,645 Times in 2,615 Posts

    Default Re: Java TDD Help! Need to create a Library Class to pass 3 TDD tests. I'm clueless.

    What have you tried?

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java TDD Help! Need to create a Library Class to pass 3 TDD tests. I'm clueless.

    Here's the 3 tests that I need to pass:

    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"));
    	}
     
    }





    And here's my library class so far:
    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) 
    	{
     
    	}
    }


    Please help, I'm at a loss here. The code needs to include an instance variable that is an
    array of Strings, at least one loop, and one conditional statement. Thanks!

  4. #4
    Junior Member
    Join Date
    Mar 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java TDD Help! Need to create a Library Class to pass 3 TDD tests. I'm clueless.

    I've tried sticking
    for (int x = 0; x < numBooks; x++) 
    {  
                theBooks[x] = book;  
                if (book == null) 
                {  
                    break;  
                }  
    }
    In my public void addBook method

    Here's the 3 tests that I need to pass:

    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"));
    	}
     
    }





    And here's my library class so far:
    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) 
    	{
     
    	}
    }


    Please help, I'm at a loss here. The code needs to include an instance variable that is an
    array of Strings, at least one loop, and one conditional statement. Thanks!

  5. #5
    Junior Member
    Join Date
    Mar 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java TDD Help! Need to create a Library Class to pass 3 TDD tests. I'm clueless.

    I've tried sticking
    if(numBooks == theBooks.length) 
    {
    	theBooks[numBooks++] = book;
    }
    In my public void addBook method

    Here's the 3 tests that I need to pass:

    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"));
    	}
     
    }





    And here's my library class so far:
    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) 
    	{
     
    	}
    }


    Please help, I'm at a loss here. The code needs to include an instance variable that is an
    array of Strings, at least one loop, and one conditional statement. Thanks!

Similar Threads

  1. How to implement GregoraianCalendar which is subclass of Calender?
    By Meirikai in forum Object Oriented Programming
    Replies: 4
    Last Post: October 30th, 2012, 15:15
  2. Learn Java using Eclipse and TDD
    By ranga_pec in forum Java JDK & IDE Tutorials
    Replies: 0
    Last Post: August 13th, 2012, 05:00
  3. create a test class (main method) to start(run) the class in Java
    By curious725 in forum Java Theory & Questions
    Replies: 5
    Last Post: August 1st, 2012, 03:21
  4. Replies: 1
    Last Post: January 17th, 2012, 09:25
  5. Java Tip Jan 22, 2011 - Primality Tests
    By helloworld922 in forum Java Programming Tutorials
    Replies: 2
    Last Post: April 18th, 2011, 20:16

Tags for this Thread