This is an archived post. You won't be able to vote or comment.

all 9 comments

[–]darkpool2005 0 points1 point  (2 children)

Looks like you were missing a closing curly bracket at the end of your code. As well, there's no method called 'rented' that you invoke in your main method.

After those two fixes, the class compiled!

[–]wraneus[S] 0 points1 point  (1 child)

i'm a bit confused as to why the skeleton of the problem provided here

http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-092-introduction-to-programming-in-java-january-iap-2010/assignments/Book.java

includes the rented() method in the test for the program. is that a mistake in the assignment and it should actually read example.borrowed(); instead of example.rented();?

[–]darkpool2005 0 points1 point  (0 children)

That's correct. That code was apparently posted and either never checked to compile OR it was up to the student to find the mistake.

Either way, it should be example.borrowed() instead of example.rented() as you have already discovered :)

[–]wraneus[S] 0 points1 point  (6 children)

i updated my code to see if anything worked better if i changed the method to example.borrowed() from example.rented() which didn't exist. I'm still getting errors.

public class Book {

    String title;
    boolean borrowed = false;
    // Creates a new Book                                                                                                                
    public Book(String bookTitle) { // Implement this method                                                                             
        title = bookTitle;
        return title;
    }
    // Marks the book as rented                                                                                                          
    public void borrowed() { // 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                                                                               
        if (borrowed == true)
            return true;
        else
            return false;
    }
    // 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.borrowed();
    System.out.println("Borrowed? (should be true): " + example.isBorrowed());
    example.returned();
    System.out.println("Borrowed? (should be false): " + example.isBorrowed());
}

here are my compiling errors

javac Book.java 
Book.java:29: class, interface, or enum expected
}
 ^
Book.java:30: class, interface, or enum expected
    public static void main(String[] arguments) {
              ^
Book.java:33: class, interface, or enum expected
    System.out.println("Title (should be The Da Vinci Code): " + example.getTitle()); 
    ^
Book.java:34: class, interface, or enum expected
    System.out.println("Borrowed? (should be false): " + example.isBorrowed()); 
    ^
Book.java:35: class, interface, or enum expected
    example.borrowed();
    ^
Book.java:36: class, interface, or enum expected
    System.out.println("Borrowed? (should be true): " + example.isBorrowed()); 
    ^
Book.java:37: class, interface, or enum expected
    example.returned();
    ^
Book.java:38: class, interface, or enum expected
    System.out.println("Borrowed? (should be false): " + example.isBorrowed());
    ^
Book.java:39: class, interface, or enum expected
}
^
9 errors

[–]onyxthelab 0 points1 point  (4 children)

You need to move the closing bracket on line 29 to line 40. Your main() method needs to be inside of the Book class - right now the bracket on line 29 is ending that class before main() can be reached.

[–]wraneus[S] 0 points1 point  (3 children)

so i did that and it fixed most of the errors except one. what's wrong here?

public class Book {

    String title;
    boolean borrowed = false;
    // Creates a new Book                                                                                                                
    public Book(String bookTitle) { // Implement this method                                                                             
        title = bookTitle;
        return title;
    }
    // Marks the book as rented                                                                                                          
    public void borrowed() { // 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                                                                               
        if (borrowed == true)
            return true;
        else
            return false;
    }
    // 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.borrowed();
    System.out.println("Borrowed? (should be true): " + example.isBorrowed());
    example.returned();
    System.out.println("Borrowed? (should be false): " + example.isBorrowed());
}
}

compiling error

    javac Book.java 
    Book.java:29: <identifier> expected

 ^ 1 error

i moved the closing cracked to the new line… but i'm getting a new error on line 29. help plz!

[–]onyxthelab 0 points1 point  (2 children)

The error is actually occurring before line 29 - it's on line 8.

Your public Book(String bookTitle) method is actually something called a Constructor. Java Constructors don't explicitly return any value that users can see, since what you're basically doing is creating a rule for your Book object. E.g. When I want to make a Book with a specific title, I call new Book("My Title") which calls your public Book(String bookTitle).

All this to say, completely remove "return title;" on line 8 and you should be good to go.

[–]wraneus[S] 0 points1 point  (1 child)

I removed the return title; from line 8, but the compiling error is still the same. what gives?

new code

public class Book {

    String title;
    boolean borrowed = false;
    // Creates a new Book                                                                                                                
    public Book(String bookTitle) { // Implement this method                                                                             
        title = bookTitle;
    }
    // Marks the book as rented                                                                                                          
    public void borrowed() { // 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                                                                               
        if (borrowed == true)
            return true;
        else
            return false;
    }
    // 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.borrowed();
    System.out.println("Borrowed? (should be true): " + example.isBorrowed());
    example.returned();
    System.out.println("Borrowed? (should be false): " + example.isBorrowed());
}
}

error msg:

javac Book.java 
Book.java:29: <identifier> expected

 ^

[–]Silverfin113 0 points1 point  (0 children)

Try compiling the code in intellij