Java ERROR!!! HELP :(

Discussion in 'Programming & Scripting' started by h2o pete, Jan 9, 2009 with 95 replies and 3,366 views.

  1. h2o pete

    h2o pete Banned Retired

    Messages:
    4,559
    Ratings:
    524
    I hate Java.

    I hate how my teacher doesn't teach she just tells us about java.

    What's wrong with this code?

    Code:
    /**
    * A class that maintains information on a book.
    * This might form part of a larger application such
    * as a library system, for instance.
    *
    * @author (Insert your name here.)
    * @version (Insert today's date here.)
    */
    class Book
    {
    // The fields.
    private String author;
    private String title;
    
    /**
    * Set the author and title fields when this object
    * is constructed.
    */
    public Book(String bookAuthor, String bookTitle)
    {
    author = bookAuthor;
    title = bookTitle;
    }
    }
    
    /**
    * Prints the name of the author in the terminal window.
    */
    public void printAuthor()
    {
    System.out.println("Author: " + author);
    }
    
    /**
    * Returns the title of this book.
    */
    public String getTitle()
    {
    return title;
    }              
    }
    
    Also what's the answer to this:


    Code:
    What are all possible results of the expression (n % m), where n and m are integer variables?
     
  2. confused5

    confused5 Enthusiast

    Messages:
    766
    Ratings:
    75
    You need to declare Book as void or a data type(int, double, etc.)

    the & (modulus) operator returns the remainder of the first term divided by the 2nd term.
    ex: 10 % 4 = 2 4 % 2 = 0

    therefore the possible results of the expression n & m are any positive integers
     
    • Like Like x 1
  3. Amurka

    Amurka Contributor

    Messages:
    1,630
    Ratings:
    343
    No, that's a constructor. You messed up on one of the brackets, you have an extra.

    I made it red where there is an extra:
    Code:
    /**
    * A class that maintains information on a book.
    * This might form part of a larger application such
    * as a library system, for instance.
    *
    * @author (Insert your name here.)
    * @version (Insert today's date here.)
    */
    class Book
    {
    // The fields.
    private String author;
    private String title;
    
    /**
    * Set the author and title fields when this object
    * is constructed.
    */
    [B]public Book(String bookAuthor, String bookTitle)[/B]
    {
    author = bookAuthor;
    title = bookTitle;
    }
    [U][I][B][COLOR=Red]}[/COLOR][/B][/I][/U]
    
    /**
    * Prints the name of the author in the terminal window.
    */
    public void printAuthor()
    {
    System.out.println("Author: " + author);
    }
    
    /**
    * Returns the title of this book.
    */
    public String getTitle()
    {
    return title;
    }              
    }
    
    I just took it out for you here:
    Code:
    /**
    * A class that maintains information on a book.
    * This might form part of a larger application such
    * as a library system, for instance.
    *
    * @author (Insert your name here.)
    * @version (Insert today's date here.)
    */
    class Book
    {
    // The fields.
    private String author;
    private String title;
    
    /**
    * Set the author and title fields when this object
    * is constructed.
    */
    public Book(String bookAuthor, String bookTitle)
    {
    author = bookAuthor;
    title = bookTitle;
    }
    
    /**
    * Prints the name of the author in the terminal window.
    */
    public void printAuthor()
    {
    System.out.println("Author: " + author);
    }
    
    /**
    * Returns the title of this book.
    */
    public String getTitle()
    {
    return title;
    }              
    }
    
     
    • Like Like x 1
  4. OP
    h2o pete

    h2o pete Banned Retired

    Messages:
    4,559
    Ratings:
    524
    Thanks man - I haven't got the software to try that on this computer but I'll try it when I get home later!

    Is it ok if I ask you about other Java problems in the future? I'm really struggling with this class lol.
     
  5. Amurka

    Amurka Contributor

    Messages:
    1,630
    Ratings:
    343

    yea no problem
     
  6. OP
    h2o pete

    h2o pete Banned Retired

    Messages:
    4,559
    Ratings:
    524
    Hi Amurka I'm having another problem.

    The question is:

    Code:
    The provided project defines two fields and a constructor to initialise the fields.
    Add two accessor methods to the class - getAuthor and getTitle - that return the author and title fields as their respective results.
    Test your class by creating some instances and calling the methods.
    
    I've added the methods but I'm not sure if they're right because I did printAuthor and the question says getAuthor so I don't know what to do now lol. Also when I try to create a new book I get "cannot find symbol" for the variable of whatever my book title is.

    Code:
    /**
    * A class that maintains information on a book.
    * This might form part of a larger application such
    * as a library system, for instance.
    *
    * @author (Peter Moncrieff.)
    * @version (17th January 2009.)
    */
    class Book
    {
    // The fields.
    private String author;
    private String title;
    
    /**
    * Set the author and title fields when this object
    * is constructed.
    */
    public Book(String bookAuthor, String bookTitle)
    {
    author = bookAuthor;
    title = bookTitle;
    }
    
    /**
    * Prints the name of the author in the terminal window.
    */
    public void printAuthor()
    {
    System.out.println("Author: " + author);
    }
    
    /**
    * Prints the title of the book in the terminal window.
    */
    public void printTitle()
    {
    System.out.println("Title: " + title);
    }
    }
    
     
  7. confused5

    confused5 Enthusiast

    Messages:
    766
    Ratings:
    75
    I think I know what you're trying to do so i modified your code a little.
    Code:
    /**
    * A class that maintains information on a book.
    * This might form part of a larger application such
    * as a library system, for instance.
    *
    * @author (Peter Moncrieff.)
    * @version (17th January 2009.)
    */
    class Book
    {
    // The fields.
    private String author;
    private String title;
    
    /**
    * Set the author and title fields when this object
    * is constructed.
    */
    public Book(String bookAuthor, String bookTitle)
    {
    author = bookAuthor;
    title = bookTitle;
    }
    
    /**
    * Prints the name of the author in the terminal window.
    */
    public String getAuthor()
    {
    return this.author;
    }
    public String getTitle()
    {
    return this.title;
    }
    
    }
    
    and then the main class to access the getAuthor method:
    Code:
    class Book_Main
    {
    public static void main(String[] args)
    {
    Book HuckFinn = new Book("Mark Twain", "The Adventures of Huckleberry Finn");
    //access the getAuthor method, which returns a string and then print the string
    System.out.print(HuckFinn.getAuthor());
    }
    }
     
    • Like Like x 1
  8. Amurka

    Amurka Contributor

    Messages:
    1,630
    Ratings:
    343
    ^you dont need the this for the return in the two accessor methods^

    You can name your methods whatever you want, most people just use "getSomething", the something being whatever they want to return. An accessor method always returns something, so you would never print something like how you had it.

    public String getAuthor() { return author; }
    public String getTitle() { return title; }

    That's all your project is asking for. Then it just asks you to test it. You don't have to create a new class to test it, but you can.

    public static void main(String [] args) {
    Book ex1 = new Book("Book Author", "Book Title");
    System.out.println("The author is: " + ex1.getAuthor());
    System.out.println("The title of the book is: " + ex1.getTitle());
    }

    Sorry I'm like rarely on anymore or I would've helped earlier. Confused pretty much got it for ya though.



    Code:
    class Book
    {
    private String author, title;
    
    public Book(String bookAuthor, String bookTitle)
    { author = bookAuthor;  title = bookTitle; }
    
    public String getAuthor() { return author; }
    public String getTitle() { return title; }
    
    public static void main(String [] args) {
    Book ex1 = new Book("Book Author", "Book Title");
    System.out.println("The author is: " + ex1.getAuthor());
    System.out.println("The title of the book is: " + ex1.getTitle());
    }
    }
    
     
    • Like Like x 1
  9. OP
    h2o pete

    h2o pete Banned Retired

    Messages:
    4,559
    Ratings:
    524
    Thanks guys!

    I just tried it and I understand how the accessor method works now, but everytime I try to create a new book (we use software called blueJ) it gives me an error.

    E.g.

    If I make the book "being bob" by author "bob" I get the error "Cannot find symbol - variable bob"

    What I've got so far:

    Code:
    /**
    * A class that maintains information on a book.
    * This might form part of a larger application such
    * as a library system, for instance.
    *
    * @author (Peter Moncrieff.)
    * @version (17th January 2009.)
    */
    class Book
    {
    // The fields.
    private String author;
    private String title;
    
    /**
    * Set the author and title fields when this object
    * is constructed.
    */
    public Book(String bookAuthor, String bookTitle)
    {
    author = bookAuthor;
    title = bookTitle;
    }
    
    /**
    * Accesses the author
    */
    public String getbookAuthor()
    {
    return author;
    }
    
    /**
    * Accesses the title
    */
    public String getbookTitle()
    {
    return title;
    }
    }
    
    Nevermind, I forgot to put it in quotes when I typed it in!
     
  10. confused5

    confused5 Enthusiast

    Messages:
    766
    Ratings:
    75
    You're creating the Book object from another class right? Can I see it?

    Edit: Oh nevermind, I was going to check quotes :tongue:
     
  11. OP
    h2o pete

    h2o pete Banned Retired

    Messages:
    4,559
    Ratings:
    524
    lol

    I'm onto the next question now.

    Code:
    Add a further field,  pages, to the    Book class to store the size of the book. This should be of type   int, and  its value should be passed to the single constructor, along with the  author and title strings.  Include an appropriate getPages() accessor method which returns the value of this field.
    Here's what I've got.

    I know something's wrong because I'm getting an error but I just had a go at it to see if I could do it lol.

    Code:
    /**
    * A class that maintains information on a book.
    * This might form part of a larger application such
    * as a library system, for instance.
    *
    * @author (Peter Moncrieff.)
    * @version (17th January 2009.)
    */
    class Book
    {
    private String author;
    private String title;
    private int pages;
    
    /**
    * Set the author and title fields when this object
    * is constructed.
    */
    public Book(String bookAuthor, String bookTitle, int bookPages)
    {
    author = bookAuthor;
    title = bookTitle;
    pages = bookPages;
    }
    
    /**
    * Accesses the author
    */
    public String getbookAuthor()
    {
    return author;
    }
    
    /**
    * Accesses the title
    */
    public String getbookTitle()
    {
    return title;
    }
    
    /**
    * Accesses the pages
    */
    public int getbookPages()
    {
    return pages;
    }
    }
    
     
  12. confused5

    confused5 Enthusiast

    Messages:
    766
    Ratings:
    75
    That code is fine, it would have something to do with your main class. Check the parameters you pass to the new Book object when you create it
     
  13. Amurka

    Amurka Contributor

    Messages:
    1,630
    Ratings:
    343
    that code compiles..you must be getting a run-time error or a compile error with your testing class.

    oh and "getbookPages()" is not technically wrong, it will compile and run. But to do it properly you would make it "getBookPages()", the first word starts lower case, then each word after that starts upcase. Same with the other accessor methods. My teacher would take points off for stuff like that so i'm just letting you know.

    thats horribly false
     
    • Like Like x 1
  14. confused5

    confused5 Enthusiast

    Messages:
    766
    Ratings:
    75
    Yeah sorry about that i realized the dib**** error after i posted it
     
  15. Amurka

    Amurka Contributor

    Messages:
    1,630
    Ratings:
    343
    lol :tongue: i win.

    post your test class if you cant get it to work
     
  16. OP
    h2o pete

    h2o pete Banned Retired

    Messages:
    4,559
    Ratings:
    524
    I got it working now :biggrin:

    Thanks guys.

    Next question is:

    Code:
    Add a method,  printDetails, to the    Book class.
    This should print details of author, title and number of pages to the terminal window and formatted as below.
    This should use the accessor methods to access the relevant data, so should include instructions such as: 
    System.out.print( getAuthor() );
    Title: Robinson Crusoe, Author: Daniel Defoe, Pages: 234
    
    
    I think I've got it right this time :biggrin:

    Someone want to just check it to make sure?

    :smile:

    Code:
    /**
    * Prints the details about the book
    */
    public void printDetails()
    {
    System.out.print("Title: ");
    System.out.print(getBookTitle() + ", " );
    System.out.print("Author: ");
    System.out.print(getBookAuthor() + ", " );
    System.out.print("Pages: ");
    System.out.println(getBookPages() );
    }
    
    Totally stuck on this next one.

    Can someone show me how to do the mutator method because I have no idea lol.

    Code:
    Add a further integer field,  noCopies, to the Book class.
    This keeps a count of the number of copies this book which are  owned by the library.
    Add a mutator methods, addCopy and removeCopy, to the class.
    These should increment and decrement the field by 1. 
    Include an accessor, getNoCopies, that returns the value of this field.
    Modify printDetails so that it includes the value of this field too.
    
     
  17. confused5

    confused5 Enthusiast

    Messages:
    766
    Ratings:
    75
    First part looks good.

    Edit: Nvm wait for Amurka on the second part, I'd look up mutator methods but im studying for exams at the moment
     
  18. Amurka

    Amurka Contributor

    Messages:
    1,630
    Ratings:
    343
    All you need is to initialize the noCopies instance variable at the top.

    Code:
    private int noCopies;
    
    Then add the two mutator methods and the one accessor methods.

    A mutator method never returns anything, but rather just "mutates" a private instance variable in the class.
    Code:
    //mutator methods
    public void addCopy(){
    noCopies++;
    }
    public void removeCopy(){
    noCopies--;
    }
    
    //accessor method
    public int getNoCopies(){
    return noCopies;
    }
    
    then just alter your printDetails() method to add the number of copies of the book.

    I'll be on aim all night if you need any quick help. I gotta do my stupid project lol

    ohh, and add it to the constructor
     
    • Like Like x 1
  19. OP
    h2o pete

    h2o pete Banned Retired

    Messages:
    4,559
    Ratings:
    524
    Wow man you're amazing. I never understood the work before AT ALL. Lectures are really bad for me :frown:. But then you spend a few minutes doing it like this and I get it straight away haha.

    I'm stuck again though lol.

    Code:
    [FONT=arial] Add a further integer field,  noAvailable, to the    Book class. This keeps a count of the number of copies this book which are available for loan.
    Include an accessor,  getNoCopiesAvailable, that returns the value of this field.  Modify printDetails so that it includes the value of this field too.
    
    Add mutator methods, lendCopy and returnCopy, to the class.  [COLOR=red](These mean a customer takes a copy of the book out of the library and that a customer returns a copy to the library. Added 4 Jan 09.)[/COLOR] These should basically increment and decrement the field. However these methods should include some checking. If the number of copies currently available is zero it must not be possible to lend out a copy and when a copy of the book is returned the number of copies available should not exceed the number of copies owned by the library. If either rule is in danger of being violated an error message should be displayed.
    
    In the first part it confused me because the question says "noCopiesAvailable" and "noAvailable" ?? Are they supposed to be different? Also I have no idea how to do the checks. I imagine it's an if statement but I dunno how to do one :frown:.

    I could probably do it if you could show me how to do an if statement :smile:. Also if you could clear up the first part because I dunno if she put them as different names by accident or whether they're supposed to be different :/.

    Thanks for your help!

    :biggrin:
     
  20. confused5

    confused5 Enthusiast

    Messages:
    766
    Ratings:
    75