java - Library and Book Class, Contructor -


why constructor not reading elements in array of library class?

when call element let's csbooks[3], garbage.

public class library {       string address;      public static book[] booklibrary = {};         public library(string location, book[] s){          address = location;            for(int = 0; < booklibrary.length-1; i++){                  booklibrary[i] = s[i];               }           public static void main(string[] args) {         // create 2 libraries        book [] books = {new book("the da vinci code"),                         new book("le petit prince"),                         new book("a tale of 2 cities"),                         new book("the lord of rings")};          book [] csbooks = {new book("java dummies"),                           new book("operating systems"),                           new book("data structures in java"),                           new book("the lord of rings")};              library firstlibrary = new library("535 w114th st.", books);           library secondlibrary = new library("1214 amsterdam av.", csbooks);       } }  

this book class:

public class book {      string title;     boolean borrowed;      // creates new book     public book(string booktitle) {         //implement method     title = booktitle;     borrowed = false;     }      // marks book rented     public void borrowed() {         //implement method     borrowed = true;     }      // set book borrowed     public void rented() {     //implement method     borrowed = true;     }      // marks book not rented     public void returned() {     //implement method     borrowed = false;     }      // returns true if book rented, false otherwise     public boolean isborrowed() {     //implement method     if(borrowed){       return true;     } else {                   return false;     }     }      // returns title of book     public string gettitle() {     //implement method     return title;     }  }  

i assuming booklibrary supposed hold library's inventory, ie books in there. in case, it'll have non-static, since want store different things each instance of class (library).

the second problem create booklibrary-array empty. array can store total of 0 elements (books) not able store 4 elements try write it. more so, since you're iterating based on booklibrary's length. so, you'll have set booklibrary array in constructor along lines of booklibrary=new book[s.length];


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -