Results 1 to 9 of 9
  1. #1
    noobForever is offline Member
    Join Date
    Nov 2010
    Posts
    12
    Rep Power
    0

    Default Calling a class method directly?

    Afternoon all; this is an assignment question, i'm not looking for direct answers, more someone to point me in the right direction please :)

    We have this below code provided to us; we need to build the classes\methods to make it work; we're not allowed to modify the Main class code at all:


    Java Code:
    package msccfsisdassignment2;
    
    public class Main {
       
        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:");
    	[COLOR="Red"]printOpeningHours();[/COLOR]	
                 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();
    
    
        }
    }

    I've got 99% of this working, but a simple one (i hope) is stumping me. The "printOpeningHours();" call in the above i cannot get working. It's referencing a class method directly, but i'm not sure how to achieve this. The below is my library class, which is working fine for the rest of the above!


    Java Code:
    package msccfsisdassignment2;
    
    import java.util.ArrayList;
    
    public class Library {
    
        //private ArrayList<String> libraryBooks = new ArrayList<String>();
        private ArrayList<Book> libraryBooks = new ArrayList<Book>();
        private String address;
        private String printOpeningHours = "9 to 5";
    
        public Library(String LibraryAddress) {
            address = LibraryAddress;
        }
    
        public void addBook(Book newBook) {
            //libraryBooks.add(newBook.getTitle());
            libraryBooks.add(newBook);
        }
    
        static void printOpeningHours(){
            System.out.println("Library open from 9AM to 5PM");
        }
    
        public void printAddress() {
            System.out.println(address);
        }
    
        public void borrowBook(String requestedBook) {
            //check whether the library array has any books; if empty, return message
            if (libraryBooks.isEmpty()) {
                System.out.println("Sorry, this book is not in our catalog.");
                //library does contain books so check the requested book against our array
            } else {
                for (int i = 0; i < libraryBooks.size(); i++) {
                    //if the requested book matches an array book then check the borrowed status
                    if (requestedBook.equals(libraryBooks.get(i).getTitle())) {
                        if (libraryBooks.get(i).isBorrowed()) {
                            System.out.println("Sorry, this book is already borrowed.");
                        } else {
                            System.out.println("You successfully borrowed " + libraryBooks.get(i).getTitle());
                            libraryBooks.get(i).rented();
                        }
                        //if the libraray contains books but we don't see a match, and the array list has reached the end, print no match
                    } else if (libraryBooks.size() == i + 1) {
                        System.out.println("Sorry, this book is not in our catalog.");
                    }
                }
            }
        }
    
        public void printAvailableBooks() {
            if (libraryBooks.isEmpty()) {
                System.out.println("No book in catalog");
            } else {
                for (int i = 0; i < libraryBooks.size(); i++) {
                    if (libraryBooks.get(i).isBorrowed()) {
                    } else {
                        System.out.println(libraryBooks.get(i).getTitle());
                    }
                }
            }
    
        }
    
        public void returnBook(String requestedBook) {
            for (int i = 0; i < libraryBooks.size(); i++) {
                if (requestedBook.equals(libraryBooks.get(i).getTitle())) {
                    if (libraryBooks.get(i).isBorrowed()) {
                        libraryBooks.get(i).returned();
                        System.out.println("You successfully returned " + libraryBooks.get(i).getTitle());
                    } else {
                        System.out.println("This book is not rented out");
                        libraryBooks.get(i).rented();
                    }
                }
            }
        }
    }
    It's literally just that single opening hours line i'm having trouble with. Please help!

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

    Default

    My guess: your instructor goofed and forgot to include the "firstLibrary." in front of the printOpeningHours() method call.

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,996
    Blog Entries
    7
    Rep Power
    19

    Default

    Quote Originally Posted by Fubarable View Post
    My guess: your instructor goofed and forgot to include the "firstLibrary." in front of the printOpeningHours() method call.
    Maybe the students are not allowed to alter the main( ... ) method but possibly they're free to add more methods to the class.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

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

    Default

    Quote Originally Posted by JosAH View Post
    Maybe the students are not allowed to alter the main( ... ) method but possibly they're free to add more methods to the class.
    True, but which makes more sense? for this to be a static method of Main or an instance method of Library? For me the choice is obvious -- this should be an instance method of Library and the prof goofed. The best solution I know of is to ask the prof.

  5. #5
    noobForever is offline Member
    Join Date
    Nov 2010
    Posts
    12
    Rep Power
    0

    Default

    Thanks both for the speedy response; on checking the Uni intranet, there was an updated note on the discussion board literally just posted (as i submitted this post!); it previously read:

    You must not modify the main method of the provided code.

    ..and now reads:

    You must not modify the main method of the provided code; you may add methods to the LibraryTester class (e.g., printOpeningHours).

    The mystery is solved! This makes much more sense; i was having difficulty understanding how a 'parentless' (right terminology?) method could be called when not part of the same class..

    Having said that, it would make more sense for this to be included in the library class as opposed to a static method of the main class; but i'll keep it this way in case they mark me down or something..
    Last edited by noobForever; 12-04-2010 at 03:29 PM. Reason: Over zealous

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

    Default

    Well blow me down! Jos is right and I'm wrong! That's what happens when I answer questions Grolsch-deficient.

  7. #7
    noobForever is offline Member
    Join Date
    Nov 2010
    Posts
    12
    Rep Power
    0

    Default

    ..it actually appears to be an MIT assignment, which has got a ton more detail on the requirements. Assignment 4

  8. #8
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,996
    Blog Entries
    7
    Rep Power
    19

    Default

    Quote Originally Posted by noobForever View Post
    Thanks both for the speedy response; on checking the Uni intranet, there was an updated note on the discussion board literally just posted (as i submitted this post!); it previously read:

    You must not modify the main method of the provided code.

    ..and now reads:

    You must not modify the main method of the provided code; you may add methods to the LibraryTester class (e.g., printOpeningHours).

    The mystery is solved! This makes much more sense; i was having difficulty understanding how a 'parentless' (right terminology?) method could be called when not part of the same class..

    Having said that, it would make more sense for this to be included in the library class as opposed to a static method of the main class; but i'll keep it this way in case they mark me down or something..
    Still then, both Libraries are referenced by local variables in the main( ... ) method only and that printOpeningHours() method isn't passed any parameters. That sort of implies that those Libraries should know which library was instantiated. Nothing a little static List<Library> can't solve in the Library class itself.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,996
    Blog Entries
    7
    Rep Power
    19

    Default

    Quote Originally Posted by Fubarable View Post
    Well blow me down! Jos is right and I'm wrong! That's what happens when I answer questions Grolsch-deficient.
    Oh; didn't you know about rule #1? It goes like this: JosAH is always right. Remember that ;-)

    kind regards,

    Jos

    ps. corollary: Grolsch is healthy fodder for the brain.
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Calling a method in another class
    By uncopywritable in forum New To Java
    Replies: 9
    Last Post: 10-22-2012, 04:01 PM
  2. How to know which class is calling a method?
    By ranju4u6 in forum New To Java
    Replies: 14
    Last Post: 09-16-2010, 08:41 AM
  3. Child-Class Calling a Method in a Parent-Class
    By Blah_ in forum New To Java
    Replies: 5
    Last Post: 09-29-2009, 02:48 AM
  4. Replies: 29
    Last Post: 09-25-2008, 07:55 PM
  5. Calling a method on original class from created class
    By kpedersen in forum Advanced Java
    Replies: 4
    Last Post: 08-20-2008, 12:25 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
  •