Results 1 to 8 of 8
  1. #1
    philip1597 is offline Member
    Join Date
    Aug 2012
    Location
    New York
    Posts
    13
    Rep Power
    0

    Default Help with Arrays

    Hello all,
    I am having figuring out how to make my code add the objects(books) listed in the main to my array. Any help would be Appreciated.
    Java Code:
    package book.pkg;
    public class Book {
        String title;
        boolean borrowed;
    
        // Creates a new Book
        public Book(String bookTitle) {
            title = bookTitle;
            // Implement this method
        }
    
        // Marks the book as rented
        public void rented() {
            // Implement this method
            borrowed = true;
        }
    
        // Marks the book as not rented
        public void returned() {
            // Implement this method
            borrowed = false;
        }
    
        //
        // Returns true if the book is rented, false otherwise
        public boolean isBorrowed() {
            // Implement this method
            //rented();
            //returned();
            
            return borrowed;
        }
        //public boolean notBorrowed(){
            
        //}
        // Returns the title of the book
        public String getTitle() {
            // Implement this method
            return title;
        }
    
        //
        public static void main(String[] arguments) {
            // Small test of the Book class
            Book example = new Book("The Da Vinci Code");
            System.out.println("Title (should be The Da Vinci Code): "
                    + example.getTitle());
            System.out.println("Borrowed? (should be false): "
                    + example.isBorrowed());
            example.rented();
            System.out.println("Borrowed? (should be true): "
                    + example.isBorrowed());
             example.returned();
            System.out.println("Borrowed? (should be false): "
                    + example.isBorrowed());
        }
    
    
        }
    Java Code:
    import book.pkg.Book;
    
    public class Library {
    	Book[] books = new Book[3];
    	Object firstLibrary;
    	Object secondLibrary;
    	//Constructors for two libraries
    	Library (String address1) {
    		firstLibrary = address1;
    	}
    
    	Library(String address1, String address2) {
    		secondLibrary = address2;
    	}
    	//Add certain books to libraries
    	public Book[] addBook(Book book) {
    		books[0] = book;
    		books[1] = book;
    		books[2] = book;
    		books[3] = book;
    		return books;
    	}
    
    	public static void printOpeningHours() {
    		System.out.println("Libraries are open daily from 9 AM to 5 PM");
    	}
    
    	public void printAddress() {
    		System.out.println(firstLibrary);
    		System.out.println(secondLibrary);
    	}
    	//Remove certain books from libraries
    	public Book[] borrowBook(String Book) {
    		return books;
    	}
    	//Print books in Libraries
    	public void printAvailableBooks() {
    
    	}
    	//Re-add Borrowed books
    	public String returnBook(String Book) {
    		return Book;
    	}
    
    	public static void main(String[] args) {
    
    		// Create two libraries
    		Library firstLibrary = new Library("10 Main St.");
    		Library secondLibrary = new Library("228 Liberty St.");
    		// Add four books to the first library
    		firstLibrary.addBook(new Book("The Da Vinci Code"));
    		firstLibrary.addBook(new Book("Le Petit Prince"));
    		firstLibrary.addBook(new Book("A Tale of Two Cities"));
    		firstLibrary.addBook(new Book("The Lord of the Rings"));
    		// Print opening hours and the addresses
    		System.out.println("Library hours:");
    		printOpeningHours();
    		System.out.println();
    		System.out.println("Library addresses:");
    		firstLibrary.printAddress();
    		secondLibrary.printAddress();
    		System.out.println();
    		// Try to borrow The Lords of the Rings from both libraries
    		System.out.println("Borrowing The Lord of the Rings:");
    		firstLibrary.borrowBook("The Lord of the Rings");
    		firstLibrary.borrowBook("The Lord of the Rings");
    		secondLibrary.borrowBook("The Lord of the Rings");
    		System.out.println();
    		// Print the titles of all available books from both libraries
    		System.out.println("Books available in the first library:");
    		firstLibrary.printAvailableBooks();
    		System.out.println();
    		System.out.println("Books available in the second library:");
    		secondLibrary.printAvailableBooks();
    		System.out.println();
    		// Return The Lords of the Rings to the first library
    		System.out.println("Returning The Lord of the Rings:");
    		firstLibrary.returnBook("The Lord of the Rings");
    		System.out.println();
    		// Print the titles of available from the first library
    		System.out.println("Books available in the first library:");
    		firstLibrary.printAvailableBooks();
    	}
    }
    Last edited by philip1597; 08-30-2012 at 07:40 PM.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,086
    Rep Power
    20

    Default Re: Help with constructors

    What problem are you having?
    That is, what are you seeing happen with your code and what do you think you should be seeing?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    philip1597 is offline Member
    Join Date
    Aug 2012
    Location
    New York
    Posts
    13
    Rep Power
    0

    Default Re: Help with constructors

    The code is giving me an error when there aren't any visible errors. I would expect it would at least add one object to the array. It gives me the error "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
    at Library.addBook(Library.java:20)
    at Library.main(Library.java:51)"

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Help with constructors

    The error makes sense, and let's look at your code to see why:

    Java Code:
        Book[] books = new Book[3]; //  ************** line 1
        Object firstLibrary;
        Object secondLibrary;
        //Constructors for two libraries
        Library (String address1) {
            firstLibrary = address1;
        }
     
        Library(String address1, String address2) {
            secondLibrary = address2;
        }
        //Add certain books to libraries
        public Book[] addBook(Book book) {
            books[0] = book;
            books[1] = book;
            books[2] = book;
            books[3] = book; //  ************** line 2
            return books;
        }
    Notice that on line 1 you declare an array, books that is initialized to hold Book objects. Then on line 2 you give it a 4th Book object. The error not only tells you exactly what is wrong -- array index is out of bounds, but it likely will tell you which line is causing it, line 20 which is probably the line 2 that I commented above. Solution, if you need an array to hold 4 items, then initialize it to hold 4 items.

    Also note that your addBook(...) method is wrong. It adds the same Book item into the array 4 times, and I doubt that's what you want to do.

  5. #5
    philip1597 is offline Member
    Join Date
    Aug 2012
    Location
    New York
    Posts
    13
    Rep Power
    0

    Default Re: Help with constructors

    You were right I expanded the array and the error was fixed, but wouldn't declaring it as [3] make it length 4 because arrays start at 0? and I know method addbook won't work I just put something there as a placeholder until I figure out what I should be putting there.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Help with constructors

    Quote Originally Posted by philip1597 View Post
    You were right I expanded the array and the error was fixed, but wouldn't declaring it as [3] make it length 4 because arrays start at 0?
    Nope.

    An array declared with 3 items will hold 3 items, with indices 0, 1, and 2. Since arrays are zero-based, they will go up to index (length - 1).

    and I know method addbook won't work I just put something there as a placeholder until I figure out what I should be putting there.
    Fair enough.

  7. #7
    philip1597 is offline Member
    Join Date
    Aug 2012
    Location
    New York
    Posts
    13
    Rep Power
    0

    Default Re: Help with constructors

    Thank you very much for the help.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. constructors
    By droidus in forum New To Java
    Replies: 3
    Last Post: 04-28-2011, 08:14 PM
  2. Need help with constructors
    By tpfaff in forum New To Java
    Replies: 10
    Last Post: 10-22-2010, 04:33 AM
  3. Constructors
    By suresh.sa in forum New To Java
    Replies: 5
    Last Post: 10-20-2010, 12:10 AM
  4. Constructors
    By new2java2009 in forum New To Java
    Replies: 5
    Last Post: 08-18-2009, 06:46 AM
  5. constructors
    By khamuruddeen in forum New To Java
    Replies: 2
    Last Post: 12-01-2007, 03:15 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •