Results 1 to 12 of 12
  1. #1
    ozzy is offline Member
    Join Date
    Oct 2009
    Posts
    5
    Rep Power
    0

    Default Help needed with java

    Hello everyone

    I'm a Student and new to java and I have been given a question which I have to go through. I have come across a problem with one of the questions and am stuck, so I was wondering if you guys could help me out.

    here is my code so far:
    /**
    * A Class that maintains Information about a book
    * This might form part of a larger application such
    * as a library system, for example.
    * @author (your name)
    * @version (a version number or a date)
    */
    public class Book
    {
    // instance variables or fields
    private String author;
    private String title;

    /**
    * Set the author and title when the book object is constructed
    */
    public Book(String bookAuthor, String bookTitle)
    {
    author = bookAuthor;
    title = bookTitle;
    }
    /**
    * Return The name of the author.
    */
    public String getAuthor()
    {
    return author;
    }
    /**
    * Return The name of the title.
    */
    public String getTitle()
    {
    return title;
    }
    }
    and this is the question

    Add a further instance variable/field pages to the Book class to store the number of pages in the book.
    This should be of type int and should be set to 0 in the Constructor.
    Add a second Constructor with signature
    public Book(String bookAuthor, String bookTitle, int noPages) so it has a third parameter passed to it as well as the author and title;
    this parameter is used - obviously?? - to initialise the number of pages.
    Note: This is easiest done by making a copy of the existing Constructor and adding the parameter.
    Add a getPages() accessor method that returns the number of pages in the book.



    Add a method printDetails() to your Book class. This should print out the Author title and number of pages to the Terminal Window. It is your choice as to how the data is formatted, perhaps all on one line, perhaps on three, and with or without explanatory text. For instance you could print out in the format:
    Title: Robinson Crusoe, Author: Daniel Defoe, Pages:226



    Add a further instance variable/field refNumber() to your Book class. This stores the Library's reference number. It should be of type String and be initialised to the empty String "" in the constructor, as its initial value is not passed in as a parameter. Instead a public mutator method with the signature:
    public void setRefNumber(String ref) should be created. The body of this method should assign the value of the method parameter ref to the refNumber.
    Add a corresponding getRefNumber() accessor method to your class so you can check that the mutator works correctly

    Modify your printDetails() method to include printing the reference number of the book.
    However the method should print the reference number only if it has been set - that is the refNumber has a non-zero length.
    If it has not been set, print "ZZZ" instead.
    Hint Use a conditional statement whose test calls the length() method of the refNumber String and gives a result like:
    Title: Jane Eyre, Author: Charlotte Bronte, Pages:226, RefNo: CB479 or, if the reference number is not set:
    Title: Robinson Crusoe, Author: Daniel Defoe, Pages:347, RefNo: ZZZ

    Modify your setRefNumber() method so that it sets the refNumber field only if the parameter is a string of at least three characters. If it is less than three, then print an error message (which must contain the word error) and leave the field unchanged



    Add a further integer variable/field borrowed to the Book class, to keep a count of the number of times a book has been borrowed. It should (obviously??) be set to 0 in the constructor.
    Add a mutator method borrow() to the class. This should increment (add 1 to) the value of borrowed each time it is called.
    Include an accessor method getBorrowed() that returns the value of borrowed
    Modify Print Details so that it includes the value of the borrowed field along with some explanatory text
    any help will be greatly appreciated. Thankyou

  2. #2
    mrmatt1111's Avatar
    mrmatt1111 is offline Senior Member
    Join Date
    Aug 2009
    Location
    San Jose, CA, USA
    Posts
    320
    Rep Power
    6

    Default

    What exactly is your question? :P
    My Hobby Project: LegacyClone

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

  4. #4
    ozzy is offline Member
    Join Date
    Oct 2009
    Posts
    5
    Rep Power
    0

    Default

    Well the question is in the second quote. it is asking me to add certian codes to my code (first quote) but I don't know where to put them and what I should write :(

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

    Default

    where to put what? The second quote is just a print out of the assignment, and that's not what we're asking for. We want to know what specific question(s) you have. This means that you have to write out specifically in your own words what is tripping you up. This will require some effort on your part, but that's OK as it helps you to learn. Otherwise we really can't help you.

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Also posted at Sun Forums.

    Ozzy: Fubarable is right, we need to know what your problem is. And it would be a very good thing to either have the dialog in one forum or, at the very least, let the participants in each forum know about the other posts. You are likely to get much the same response whereever you post your homework.

  7. #7
    ozzy is offline Member
    Join Date
    Oct 2009
    Posts
    5
    Rep Power
    0

    Default

    Ok sorry about that, as you may have realized I am not very good at explaining things. So I will try my best.
    Basically In the first part it is asking me to adda field to store the number of pages and set the constructer to 0.
    I did this at the end of my code, and this what I put:
    * Create a page number to store number of pages in book.
    */
    public int noPages()
    {
    nopages = 0;
    }
    }
    then it says to add getPages() accessor methods so it returns the number of pages and I added this under the above code.

    /**
    * @Return The number of pages in the book.
    */
    public int getPages()
    {
    return pages;
    }
    but when I compile it I get 2 error messages and they both say
    "cannot find symbol variable noPages" and "cannot find symbol variable pages"

    So what am I doing wrong exactly. I hope this made more sense.

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,520
    Blog Entries
    7
    Rep Power
    20

    Default

    If those methods are in your Book class I can imagine that your compiler whines at you: you don't have a (private) member variable 'pages' or 'noPages' in your Book object (why do you need both variables anyway?).

    Your setter method is wrong too: it doesn't return anything so why make it of return type 'int' in the first place? Basically getters and setters have the following structure:

    Java Code:
    public class YourClass {
       private T property; // your property to be set and get.
       ...
       public void setProperty(T property) {
          ...
          this.property= property; // set the property
       }
       ...
       public T getProperty() {
          ...
          return this.property; // get the property
       }
    }
    About your first confession: you should practice explaining stuff; no matter to whom, even to yourself, it really helps understanding something when you're able to explain the details of that something.

    kind regards,

    Jos

  9. #9
    ozzy is offline Member
    Join Date
    Oct 2009
    Posts
    5
    Rep Power
    0

    Default

    Thanks for that it helped a lot :D
    But I have another problem now. I have to produce a code that prints the details of author, book and number of pages. this is my code:
    /**
    * Print book information.
    */
    public void printDetails()
    {
    // Simulate the printing of book details.
    System.out.println("##################");
    System.out.println("# Title");
    System.out.println("# Author");
    System.out.println("# Pages");
    System.out.println("##################");
    System.out.println();

    }
    But when I post my answer I get this back:
    # Test 1 (1.0 out of 1)

    The compilation was successful

    # Test 2 (0.0 out of 1)

    The output should have been:
    Author name included
    Book Title included
    Number of Pages included
    Does this mean that I have to put those exact words in the plaes where I have put Author, title and pages?

    Edit: I tried editing the code like this:
    /**
    * Print book information.
    */
    public void printDetails()
    {
    // Simulate the printing of book details.
    System.out.println("##################");
    System.out.println("# Title: Robinson Crusoe");
    System.out.println("# Author: Daniel Defoe");
    System.out.println("# Pages:226");
    System.out.println("##################");
    System.out.println();

    }
    But I am getting the same error
    Last edited by ozzy; 11-01-2009 at 09:43 PM.

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

    Default

    1) Where are you calling the printDetails method so that this method will run?
    2) Shouldn't this method use the fields held by the Book class including the author and title fields?
    3) If the code you paste here is properly indented to begin with, it'll be much easier for us to read. Please consider this.

  11. #11
    ozzy is offline Member
    Join Date
    Oct 2009
    Posts
    5
    Rep Power
    0

    Default

    ermm I didn't quite understand that XD.
    This is my full code so far:
    /**
    * A Class that maintains Information about a book
    * This might form part of a larger application such
    * as a library system, for example.
    * @author (your name)
    * @version (a version number or a date)
    */
    public class Book
    {
    // instance variables or fields
    private String author;
    private String title;
    private int pages;

    /**
    * Set the author and title when the book object is constructed
    */
    public Book(String bookAuthor, String bookTitle, int noPages)
    {
    author = bookAuthor;
    title = bookTitle;
    pages = noPages;
    }
    /**
    * Return The name of the author.
    */
    public String getAuthor()
    {
    return author;
    }
    /**
    * Return The name of the title.
    */
    public String getTitle()
    {
    return title;
    }
    /**
    * Create a page number to store number of pages in book.
    */
    public Book(int pages)
    {
    pages = 0;
    }
    /**
    * @Return The number pages in book.
    */
    public int getPages()
    {
    return pages;
    }

    /**
    * Print book information.
    */
    public void printDetails()
    {
    // Simulate the printing of book details.
    System.out.println("##################");
    System.out.println("# Title: Robinson Crusoe");
    System.out.println("# Author: Daniel Defoe");
    System.out.println("# Pages:226");
    System.out.println("##################");
    System.out.println();

    }
    }
    basically I have to add a printDetails() to my book class and I have print out the Author title and number of pages to the Terminal Window. and it said:
    "For instance you could print out in the format:

    Title: Robinson Crusoe, Author: Daniel Defoe, Pages:226"

    Do I have to add more code to it or something?

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

    Default

    Right now the method will always print Title: Robinson Crusoe and Author: Daniel Defoe. Is this what you desire? Myself, I'd use the author and title fields that are present in the class.

    For instance:
    Java Code:
    public class Song {
      String singer;
      String title;
      
      public Song(String singer, String title) {
        this.singer = singer;
        this.title = title;
      }
      
      // getters and setters, etc
      
      public void showStuff() {
        System.out.println("Song Info");
        
        // If I want to show the object's singer, I'd use the field
        // in this System.out.println statement
        System.out.println("Singer: " + singer);
        
        //... same for title
      }
    }
    Again, all of your code is left justified and very difficult to read. Please only post indented code.

Similar Threads

  1. Help needed with downloader in java
    By falcommoney in forum New To Java
    Replies: 3
    Last Post: 01-28-2009, 01:31 PM
  2. java help needed
    By mayhewj7 in forum New To Java
    Replies: 1
    Last Post: 01-28-2009, 05:33 AM
  3. Help needed in Java
    By javanewbie in forum New To Java
    Replies: 2
    Last Post: 07-08-2008, 07:55 PM
  4. Java experts needed- 30 minute online Java projects
    By michelle in forum Jobs Offered
    Replies: 0
    Last Post: 03-05-2008, 11:47 PM
  5. Java help needed
    By jeneal in forum New To Java
    Replies: 0
    Last Post: 11-21-2007, 03: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
  •