Results 1 to 8 of 8
Thread: Help with constructors
- 08-30-2012, 12:50 AM #1
Member
- Join Date
- Aug 2012
- Location
- New York
- Posts
- 13
- Rep Power
- 0
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.
- 08-30-2012, 10:24 AM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,458
- Rep Power
- 16
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.
- 08-30-2012, 07:42 PM #3
Member
- Join Date
- Aug 2012
- Location
- New York
- Posts
- 13
- Rep Power
- 0
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)"
-
Re: Help with constructors
The error makes sense, and let's look at your code to see why:
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.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; }
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.
- 08-30-2012, 08:06 PM #5
Member
- Join Date
- Aug 2012
- Location
- New York
- Posts
- 13
- Rep Power
- 0
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.
-
Re: Help with constructors
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).
Fair enough.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.
- 08-31-2012, 07:19 AM #7
Member
- Join Date
- Aug 2012
- Location
- New York
- Posts
- 13
- Rep Power
- 0
Re: Help with constructors
Thank you very much for the help.
-
Similar Threads
-
constructors
By droidus in forum New To JavaReplies: 3Last Post: 04-28-2011, 08:14 PM -
Need help with constructors
By tpfaff in forum New To JavaReplies: 10Last Post: 10-22-2010, 04:33 AM -
Constructors
By suresh.sa in forum New To JavaReplies: 5Last Post: 10-20-2010, 12:10 AM -
Constructors
By new2java2009 in forum New To JavaReplies: 5Last Post: 08-18-2009, 06:46 AM -
constructors
By khamuruddeen in forum New To JavaReplies: 2Last Post: 12-01-2007, 03:15 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks