Results 1 to 6 of 6
  1. #1
    jordanm_king is offline Member
    Join Date
    Feb 2016
    Posts
    3
    Rep Power
    0

    Default adding elements to an array

    I am trying to create a method for a project that has two classes, a library class and a book class, and the methods purpose is to add books to the library class. I am using an array to store the books but I do not know how to get the method to place the book into a new slot each time it is called. I was wondering if anyone knew how to work this problem out. here is code
    Java Code:
    package libraryandbook;
    
    public class Book {static String title;
    boolean borrowed;
    
    
    // Creates a new Book
    public Book(String bookTitle) {
    	title = bookTitle;
    
    }
    
    // Marks the book as rented
    public void borrowed() {
    	borrowed = true;
    
    }
    
    // Marks the book as not rented
    public void returned() {
    	borrowed = false;
    
    }
    
    // Returns true if the book is rented, false otherwise
    public boolean isBorrowed() {
    	return borrowed;
    
    }
    
    // Returns the title of the book
    public String getTitle() {
    	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("a" + example.getTitle());
    	
    	System.out.println("Borrowed? (should be false): " + example.isBorrowed());
    	example.borrowed();
    	System.out.println("Borrowed? (should be true): " + example.isBorrowed());
    	example.returned();
    	System.out.println("Borrowed? (should be false): " + example.isBorrowed());
    }
    }
    
    
    
    package libraryandbook;
    
    public class Library {
    
    	// fields//
    	String name;
    	Book[] books = new Book[100];
    	Book boo;
    	
    
    	// constructor//
    	Library(String na) {
    		name = na;
    
    	}
    
    	// methods//
    	public void addBook(Book boo) {
    	books[books.length -1] = boo;
    	}
    
    	public static void main(String[] args) {
    		Library a = new Library("a");
    		a.addBook(new Book("b"));
    		a.addBook(new Book("c"));
    
    	}
    
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: adding elements to an array

    how to get the method to place the book into a new slot each time it is called
    Define a class level variable to use as an index into the array.
    Its value will be the next empty slot in the array. It starts at 0 and is incremented by 1 each time an item is added to the array.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: adding elements to an array

    Why is the title of a book a static variable? You don't want all books to have the title. say. 'The Tele Tubbies meet Godzilla' (*) do you?

    kind regards,

    (*) or any other title will do here ...
    Build a wall around Donald Trump; I'll pay for it.

  4. #4
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,993
    Rep Power
    9

    Default Re: adding elements to an array

    Quote Originally Posted by JosAH View Post
    'The Teletubbies meet Godzilla'
    I hope they will turn that into a movie :)
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  5. #5
    jordanm_king is offline Member
    Join Date
    Feb 2016
    Posts
    3
    Rep Power
    0

    Default Re: adding elements to an array

    Quote Originally Posted by Norm View Post
    Define a class level variable to use as an index into the array.
    Its value will be the next empty slot in the array. It starts at 0 and is incremented by 1 each time an item is added to the array.
    Awesome! that was all that I needed to know.

  6. #6
    jordanm_king is offline Member
    Join Date
    Feb 2016
    Posts
    3
    Rep Power
    0

    Default Re: adding elements to an array

    Quote Originally Posted by JosAH View Post
    Why is the title of a book a static variable? You don't want all books to have the title. say. 'The Tele Tubbies meet Godzilla' (*) do you?

    kind regards,

    (*) or any other title will do here ...
    I know I just figured that out. I'm not sure when I set that to static..lol

Similar Threads

  1. 3D Array -- Adding Elements
    By mynorka in forum Advanced Java
    Replies: 4
    Last Post: 11-18-2013, 01:49 PM
  2. Replies: 1
    Last Post: 04-05-2011, 09:58 PM
  3. Adding elements to an array
    By slitka in forum New To Java
    Replies: 9
    Last Post: 04-01-2011, 05:01 AM
  4. Adding elements before and after array
    By liQuorice in forum New To Java
    Replies: 4
    Last Post: 03-04-2011, 01:52 AM
  5. Adding the Elements of a 2D Array
    By RMcLuckie45 in forum New To Java
    Replies: 0
    Last Post: 11-08-2010, 12:04 AM

Posting Permissions

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