need help wif homework


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: need help wif homework

  1. #1
    Join Date
    Oct 2004
    Posts
    2

    need help wif homework

    *****LET ME MAKE THIS CLEAR! I DON'T NEED SOMEONE TO DO THE WHOLE PROGRAM FOR ME! I AM JUST GIVING YOU ALL THIS INFORMATION SO YOU WON'T BE CONFUSED ON WHAT THE PROGRAM IS ALL ABOUT... I JUST NEED SOMEONE TO CHECK THE BOOK CLASS CODE AND HELP ME WITH THE IFSTATEMENT IN THE PATRON CLASS! IT IS AT THE BOTTOM OF THIS LONG POST!

    I am new to java and I need much help with my school assignment... Here is the problem... I must write a library program... here is the question

    Patrons of a library can borrow up to three books. A patron, therefore, has a name and up to three books. A book has an author and a title. Design and implement two classes, Patron and Book, to represent these objects and the following behavior:

    -The client can instantiate a book with a title and author
    -The client can examine but not modify a book's title or author
    -The client can ask a patron wthere it has borrowed a given book (identified by title).
    -The client can tell a patron to return a given book (identified by title).
    -The client can tell a patron to borrow a given book.

    The Patron class should use a seperate instance variable for each book (a total of three).
    Each of these variables is initially null. When a book is borrowed, the patron looks for a variable that is not null. If no such variable is found, the method returns false. If a null variable is found, it is reset to the new book and the method returns true. Similar considerations apply to other methods. Use the method aString.equals(aString) to compare two strings for equality. Be sure to include appropriate toString methods for your classes and test them with a tester program.

    My teacher already wrote out a tester program for us... unfortunately... i don't have a copy of it to show..

    this here is what i have come up for the Book class
    public class Book

    //Instance Variables
    String author;
    String title;

    //Constructor method
    public void Book(){
    author =" ";
    title = " ";

    public void setName (String a, String t) // a being author and t being title
    //set author and title
    author = a;
    title = t;

    //other methods

    public string getAuthor(){
    //Get Author
    return Author;

    public sgring getTitle(){
    //Get Title
    return Title;

    public string toString(){
    //Construct and return a string rep of the Book
    String str;
    str = "Author: " + author + "\n" +
    "Title: " + title();
    return str;
    }
    }

    That's what i got... i dun know if that is even correct... but i think it's on the right track ne?
    This is my little guide line that me and the teacher made

    Book
    Instance Vars - author, title (string)
    Constructor - Book(), Book(author, title)
    Methods- getAuthor, getTitle, toString
    _________________________________
    i followed that little map... and that's what i have got

    I need major help on the Patron Class...

    this is the little guide map

    Patron
    instance vars - name, book1, book2, book3
    Constructors - Patron(), Patron(name) <-- string in front
    Methods- hasBorrowed(title) - checked out <----- if(getTitle != null)
    returnBook(title) - check in if(____.equals(__))
    borrowBook(title) - check out
    toString


    This is what i came up with before my brain literally shut off for the day

    public class Patron

    //Instance Variables
    String name,book1, book2, book3;

    //Constructors
    public Patron (){
    name = " ";
    book1 = " ";
    book2 = " ";
    book3 = " ";
    }

    public void setName (string nm){
    name = nm;}

    // methods

    **** if(book1.getTitle() != null){
    if(t.equals(t))
    return book1;
    else{
    book 1 = t;}
    } else {t = book2.getTitle();}

    pretty pathetic huh... i know the patron makes no sense at all... but yeah... i have a hunch...
    i just recently stared at this problem for 5 intense minutes... and i have come up with this(for the if statement in Patron)

    would it be like

    public void getTitle(){
    if (book1.getTitle() != null){
    if(t.equals(t))
    book1 = t ;}
    else if (book2.getTitle() != null){
    if(t.equals(t))
    book2 = t ; }
    else book3 = t;
    }

    bleh.. i dun know... SOMEONE HELP ME PLEAASE!
    blah blah blah

  2. #2
    Join Date
    Feb 2004
    Posts
    541
    When posting code in the forum (especially long sections of code), you should use the code tags and format it nicely. The code tags will make sure that what you write is correctly formatted so we can all read it easily. To use them it's pretty similar to HTML. You surround the code with the tags [ CODE ] and [ /CODE ] (but without the spaces inside the square brackets).

    Anyway, I had a look at the IF statement you wrote at the end. There are a number of things I can see that will cause problems.

    Firstly, you start with if(book1.getTitle() != null). Well in the Patron class book1 is a String. String doesn't have a getTitle() method, so this won't compile. Are the books meant to be in the form of a Book class? If so then they shouldn't be strings!

    Second is the statement if(t.equals(t)). I had a look through but I couldn't find any variable named 't'. Even if there is a variable named 't' somewhere you should rename it. Variable names should give information about what the variable is for, and 't' doesn't give any indication at all.

    More importantly, you are testing 't' against itself. This will always evaluate to true (if the equals method is written correctly and fulfills its contract), because they're both the same object! So basically, what is 't' and why are you comparing it to itself?

  3. #3
    Join Date
    Oct 2004
    Posts
    2
    my teacher gave us that code if(t.equals(t)) ... and she told us to use t as the name.... i defined t somewhere... here it is..

    public void setName (String a, String t) // a being author and t being title
    //set author and title
    author = a;
    title = t;

    would

    if(getTitle() != null) work then?

    well... you'd have to compare the title given by the tester when you see to check if you have "borrowed" the book... to what was originally assigned when you borrowed it.. then how are you supposed to do that if you can't compare the two? Ionno.. maybe in the tester program she gave us.. there is another variable we are supposed to test it with... maybe with the book strings... harhar... ionno... bleh... thankz for helping but it didn't fully answer my question... i still dun know what to exactly do since it's all wrong.. -_-... is those the only thing that's wrong or what? is the whole thing just screwed up or will it be okay if i fix what you mentioned...
    blah blah blah

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center