Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Java assignment problem

Options
  • 29-12-2016 5:05pm
    #1
    Closed Accounts Posts: 191 ✭✭


    Hi i am doing a coll assignment over Christmas on objects and classes. Its to make a library system that allows users to add books etc.Im having a problem printing the addresses and adding books. Ill show my code to help ye get a better idea. This is my main class.
    public static void main(String[] args) {
            
            Library firstLibrary = new Library ("Moylish Park, Limerick");
            Library secondLibrary = new Library ("Clare Street, Limerick");
            Library thirdLibrary = new Library ("Nenah road, Thurles");
            
            firstLibrary.addBook(new Book("Java how to program"));
            firstLibrary.addBook(new Book("What's new in Java 7?"));
            firstLibrary.addBook(new Book("Java in a nutshell"));
            firstLibrary.addBook(new Book("Pro android 2"));
            
            secondLibrary.addBook(new Book("The Power of colour"));
            secondLibrary.addBook(new Book("The Genius of Being"));
            secondLibrary.addBook(new Book("Creativity in Nature")); 
            
            System.out.println("Library hours:");
            
            
            System.out.println("");
            
            System.out.println("Library Addresses:");
            firstLibrary.printAddress();
            secondLibrary.printAddress();
            thirdLibrary.printAddress();
            
        }
    }
    
    
    //      Book example = new Book ("Java how to program");
    //      System.out.println("Title (should be Java How To Program): " + example.getTitle());
    //     
    //      System.out.println("Borrowed? (Should be false) " + example.isBorrowed());
    //      example.borrowed();
    //      
    //      System.out.println("Borrowed? (should be true): " + example.isBorrowed());
    //      example.rented();
    //      
    //      System.out.println("Borrowed? (should be false): " + example.isBorrowed());
    //    } 
    //}
    

    This is my library class
    public class Library {
        String address;
        
        Library (String address){
            this.address = address;
            
        }
        
        public void printOpeningHours(){
            System.out.println("Librarys are open daily from 9am to 5pm");
        }
    
        public void printAddress() {
            System.out.println(this.address);
        }
    
        void addBook(Book book) {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }
    
        
    }
    

    I also have a book class but i dont think thats needed for this problem im stuck on. the add book method was added for me by netbeans can someone explain why? Also why do the addresses not print and how do i add books? Im not looking for ye to do my assignment i actually want to learn and understand. Thanks in advance!


«1

Comments

  • Registered Users Posts: 6,252 ✭✭✭Buford T Justice


    What exactly is happening when you run it?

    I've tried it myself and the library addresses are printed


  • Closed Accounts Posts: 191 ✭✭chocolate boy123


    When I run it I get red errors in the print box


  • Registered Users Posts: 172 ✭✭aidanathome


    When I run it I get red errors in the print box

    Can you provide the actual error output?


  • Closed Accounts Posts: 191 ✭✭chocolate boy123


    Sure, have attached a pic


  • Registered Users Posts: 172 ✭✭aidanathome


    Sure, have attached a pic

    It's doing exactly what you're telling it to do - it's throwing an exception:

    void addBook(Book book) {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }


  • Advertisement
  • Closed Accounts Posts: 191 ✭✭chocolate boy123


    What will I fill in for that method that method was generated by net beans but I do need that method to add books


  • Closed Accounts Posts: 191 ✭✭chocolate boy123


    I left the method blank and the addresses are printing now!


  • Closed Accounts Posts: 191 ✭✭chocolate boy123


    When I try call my printOpeningHours method in my main class I get an error under it?


  • Registered Users Posts: 172 ✭✭aidanathome


    I left the method blank and the addresses are printing now!

    Addresses are printing because inside in your Library class you have a String called address, and earlier you add the address to that String (when you called the constructor).
    To add books, you need to find something to add the books to. A String won't work as it's singular, but something else will need to be declared.

    For any errors, you really need to provide them, otherwise it's just blind speculation as to what the problem is.


  • Registered Users Posts: 1,463 ✭✭✭Anesthetize


    Think of what a library does - it stores books. What you need to figure out here is how to make a library object store books. The addBook() method will be used to add book objects to the library, you need something to store them in.


  • Advertisement
  • Closed Accounts Posts: 191 ✭✭chocolate boy123


    Ok thanks very much guys! Help us greatly appreciated


  • Closed Accounts Posts: 191 ✭✭chocolate boy123


    Error on my print method is just underlined red and if I click lightbulb it says create method in then it names my main class but I already have it created in my library class


  • Registered Users Posts: 172 ✭✭aidanathome


    Error on my print method is just underlined red and if I click lightbulb it says create method in then it names my main class but I already have it created in my library class

    Screenshot? Including the call, and the method being called?


  • Closed Accounts Posts: 191 ✭✭chocolate boy123


    I call it in the main and it's written in the library class


  • Registered Users Posts: 172 ✭✭aidanathome


    I call it in the main and it's written in the library class

    The issue is where you are doing things. By calling 'printOpeningHours()' on it's own, there is no context - the system does not know what class the method is in, so it assumes that you mean call 'printOpeningHours()' on this class.

    See how other methods that you are calling refer to the class the method is in?


  • Closed Accounts Posts: 191 ✭✭chocolate boy123


    I understand what you mean but what object can I link it to? I tried Library.printOpeningHours(); but the error is still here!


  • Registered Users Posts: 172 ✭✭aidanathome


    I understand what you mean but what object can I link it to? I tried Library.printOpeningHours(); but the error is still here!

    Which library do you want to print the opening hours of?


  • Closed Accounts Posts: 191 ✭✭chocolate boy123


    JUst in general I just need it to print out the hours but I can't do it in a print statement in the main package because she wants us to print it in a method and call it in the main


  • Registered Users Posts: 172 ✭✭aidanathome


    I understand what you mean but what object can I link it to?

    The answer to this question is that you want to print out the hours of the Library that you want to know the library hours of.

    Doing Library.printOpeningHours() won't work, because Library is just a class. But you've also created specific library instances. They are the 'objects' that you want to use.
    JUst in general I just need it to print out the hours but I can't do it in a print statement in the main package because she wants us to print it in a method and call it in the main

    See above.


  • Closed Accounts Posts: 191 ✭✭chocolate boy123


    The answer to this question is that you want to print out the hours of the Library that you want to know the library hours of.

    Doing Library.printOpeningHours() won't work, because Library is just a class. But you've also created specific library instances. They are the 'objects' that you want to use.





    See above.


    But each library has the exact same opening hours.


  • Advertisement
  • Registered Users Posts: 172 ✭✭aidanathome


    But each library has the exact same opening hours.

    Ok, so what do you actually want to do?


  • Users Awaiting Email Confirmation Posts: 489 ✭✭AngryDiMaria


    firstLibrary.printOpeningHours();
    secondLibrary.printOpeningHours();
    thirdLibrary.printOpeningHours();

    Is this what you're trying to achieve?

    Edit:

    I noticed your printOpeningHours method is just a string. Would you not give each library their own opening and closing times?


  • Closed Accounts Posts: 191 ✭✭chocolate boy123


    No, we are told it has to be "the opening hours is 9 to 5" and we are given the name of the method as openingHours and we just have to fill it in so I must do it the way I'm trying to do it. I am just tryin to print out that string once!


  • Closed Accounts Posts: 191 ✭✭chocolate boy123


    I have solved that problem! can someone tell me is this ok for my addBook method i made an array of book objects.
    
    void addBook(Book book) {
            
            Book [] books = new Book [7];
           
            books[0] = new Book("Java how to program");
            books[1] = new Book("What's new in Java 7?");
            books[2] = new Book("Java in a nutshell");
            books[3] = new Book("Pro android 2");
            books[4] = new Book("The Power of colour");
            books[5] = new Book("The Genius of Being");
            books[6] = new Book("Creativity in Nature"); 
            }
    
    


  • Registered Users Posts: 1,463 ✭✭✭Anesthetize


    So you've created an array that stores seven books inside the addBook method. But what will happen to those books when the addBook method has finished executing? Will I be able to retrieve them? Why is the 'book' parameter not being used? What if somebody wanted to add another book?


  • Closed Accounts Posts: 191 ✭✭chocolate boy123


    I have solved all this since my last post this is my new library class which i believe should work fine
    Book[] books = new Book[7];
        int index = 0;
        String address;
    
        Library(String address) {
            this.address = address;
        }
    
        public void printOpeningHours() {
            System.out.println("Librarys are open daily from 9am to 5pm");
        }
    
        public void printAddress() {
            System.out.println(this.address);
        }
    
        public void addBook(Book bookObject) {
            books[index] = bookObject;
            index++;
        }
    
    //    public void borrowBook(String books) {
    //
    //    }
    
        void printAvailableBooks() {
            for(int i = 0; i < books.length; i++) {
                System.out.println(books[i].getTitle());
                
            }
        }
    }
    

    My book class:
    public class Book {
    
        String title;
        boolean borrowed;
    
        public Book(String title) {
            this.title = title;
        }
    
        public void borrowed() {
            borrowed = true;
        }
    
        public void rented() {
            borrowed = false;
        }
    
        public boolean isBorrowed() {
    
            if (borrowed == true) {
                return true;
            }
            return false;
        }
    
        public String getTitle() {
            return title;
        }
    
        void returned() {
        }
    
        public static void Main(String[] args) {
    
            Book example = new Book("Java how to program");
            System.out.println("Title (should be Java How To Program): " + example.getTitle());
    
            System.out.println("Borrowed? (Should be false) " + example.isBorrowed());
            example.borrowed();
    
            System.out.println("Borrowed? (should be true): " + example.isBorrowed());
            example.rented();
    
            System.out.println("Borrowed? (should be false): " + example.isBorrowed());
        }
    }
    

    and my main class
            Library firstLibrary = new Library ("Moylish Park, Limerick");
            Library secondLibrary = new Library ("Clare Street, Limerick");
            Library thirdLibrary = new Library ("Nenah road, Thurles");
            
            firstLibrary.addBook(new Book("Java how to program"));
            firstLibrary.addBook(new Book("What's new in Java 7?"));
            firstLibrary.addBook(new Book("Java in a nutshell"));
            firstLibrary.addBook(new Book("Pro android 2"));
            
            secondLibrary.addBook(new Book("The Power of colour"));
            secondLibrary.addBook(new Book("The Genius of Being"));
            secondLibrary.addBook(new Book("Creativity in Nature")); 
            
            System.out.println("Library hours:");
            firstLibrary.printOpeningHours();
            
            System.out.println("");
            
            System.out.println("Library Addresses:");
            
            secondLibrary.printAddress();
            thirdLibrary.printAddress();
            
            System.out.println("");
            
    //        System.out.println("Borrowing Java how to program");
    //        firstLibrary.borrowBook("Java how to program");
    //        secondLibrary.borrowBook("Java how to program");
    //        thirdLibrary.borrowBook("Java how to program");
            
            
            System.out.println("Books available in the first library");
            firstLibrary.printAvailableBooks();
            
            System.out.println("");
            
            System.out.println("Books available in the second libray");
            secondLibrary.printAvailableBooks();
            
          
            
        }
    }
    

    I dont understand why i get an error when running. I know its relatiod to printing the books available. When this is ran it will display everything except the second librarys books and the print sttement "books available in the second library." However if i replace firstLibrary.printAvailableBooks with secondLibrary.printAvailableBooks it will print the second librarys books but then i will still have the error. Its the exception error like i had yesterday but i do not throw any exceptions? Sorry if this reply is confusing.


  • Users Awaiting Email Confirmation Posts: 489 ✭✭AngryDiMaria


    Why on earth do you have a main method in the Book class? Why are you not using ArrayLists to store objects? I don't think you have been properly taught the fundamentals of OOP. I'm only a 2nd year in college so I cannot give you advice as good as whats available from other users on here. I think I solved your issues but it will not help you in the long run unless you understand the basics.

    Main:
    public class MainApp
    {
    
        public static void main(String[] args)
        {
            Library firstLibrary = new Library("Moylish Park, Limerick");
            Library secondLibrary = new Library("Clare Street, Limerick");
            Library thirdLibrary = new Library("Nenah road, Thurles");
    
            firstLibrary.addBook(new Book("Java how to program"));
            firstLibrary.addBook(new Book("What's new in Java 7?"));
            firstLibrary.addBook(new Book("Java in a nutshell"));
            firstLibrary.addBook(new Book("Pro android 2"));
    
            secondLibrary.addBook(new Book("The Power of colour"));
            secondLibrary.addBook(new Book("The Genius of Being"));
            secondLibrary.addBook(new Book("Creativity in Nature"));
    
            System.out.println("\nLibrary hours:");
            firstLibrary.printOpeningHours();
    
            System.out.println("\nLibrary Addresses:");
    
            firstLibrary.printAddress();
            secondLibrary.printAddress();
            thirdLibrary.printAddress();
    
            System.out.println("\nBooks available in the first library:");
            firstLibrary.printAvailableBooks();
    
            System.out.println("\nBooks available in the second library:");
            secondLibrary.printAvailableBooks();
    
            Book example = new Book("Java how to program");
    
            System.out.println("\nTitle: " + 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());
    
        }
    
    }
    

    Book Class:
    public class Book
    {
    
        String title;
        boolean borrowed;
    
        public Book(String title)
        {
            this.title = title;
        }
    
        public void borrowed()
        {
            borrowed = true;
        }
    
        public void returned()
        {
            borrowed = false;
        }
    
        public boolean isBorrowed()
        {
            return borrowed == true;
        }
    
        public String getTitle()
        {
            return title;
        }
    
    }
    

    Library Class:
    public class Library
    {
    
        String address;
        private ArrayList<Book> bookList;
    
        public Library(String address)
        {
            this.address = address;
            bookList = new ArrayList<>();
        }
    
        public void printOpeningHours()
        {
            System.out.println("Librarys are open daily from 9am to 5pm");
        }
    
        public void printAddress()
        {
            System.out.println(this.address);
        }
    
        public void printAvailableBooks()
        {
            for (Book b : bookList)
            {
                System.out.println(b.getTitle());
            }
        }
    
        public void addBook(Book book)
        {
            bookList.add(book);
        }
    
    }
    


  • Registered Users Posts: 1,463 ✭✭✭Anesthetize


    I dont understand why i get an error when running. I know its relatiod to printing the books available. When this is ran it will display everything except the second librarys books and the print sttement "books available in the second library." However if i replace firstLibrary.printAvailableBooks with secondLibrary.printAvailableBooks it will print the second librarys books but then i will still have the error. Its the exception error like i had yesterday but i do not throw any exceptions? Sorry if this reply is confusing.
    Do you know what a NullPointerException is? You will meet many of these on your adventures.


  • Closed Accounts Posts: 191 ✭✭chocolate boy123


    No, never heard of it?


  • Advertisement
  • Registered Users Posts: 6,252 ✭✭✭Buford T Justice


    Why on earth do you have a main method in the Book class? Why are you not using ArrayLists to store objects? I don't think you have been properly taught the fundamentals of OOP. I'm only a 2nd year in college so I cannot give you advice as good as whats available from other users on here. I think I solved your issues but it will not help you in the long run unless you understand the basics.
    public class Book
    {
    
        String title;
        boolean borrowed;
    
        public Book(String title)
        {
            this.title = title;
        }
    
        public void borrowed()
        {
            borrowed = true;
        }
    
        public void returned()
        {
            borrowed = false;
        }
    
        public boolean isBorrowed()
        {
           [B] return borrowed == true;[/B]
        }
    
        public String getTitle()
        {
            return title;
        }
    
    }
    

    Thee's no need to run an evaluation here. its pointless. borrowed is already declared of type boolean, so there's no need to check if its true or false. Just return it.
     public boolean isBorrowed()
        {
           return borrowed;
        }
    

    Also, method names should be verbs, so using borrowed as a method name is poor convention practice. it should be something more akin to
    public void borrowBook(){}
    
    It would also be a good idea to return something from this method so you could check if the borrow was successful.


Advertisement