Results 1 to 4 of 4

Thread: Java Programming Help

  1. #1
    Senior Member
    Join Date
    May 2003
    Posts
    226

    Java Programming Help

    I have a practical that requires:

    A book has the following attributes
    1. title
    2. author name
    3. loan status
    4. borrowerID
    5. loan date
    6. due dtae

    a) declare these instance variables using String class, boolean type and GregorianCalendar class.

    b) define a method(toString) that returns the Book object as a string format given:

    title;author;loanStatus;borrowerID;day/month/year;day/month/year

    c) define a static method(create) that takes in a string format given in (b), and create a Book object. Example of a given String

    Data Structure;Michael Main;true;1234;01/08/2003;15/08/2003

    d) define a main() method to test the Book class.


    I have coded the following:

    //Start of code

    import java.util.*;

    public class Book {
    String title;
    String author;
    boolean loanStatus;
    String borrowerID;
    GregorianCalendar loanDate;
    GregorianCalendar dueDate;

    public String toString() {

    return null;
    }

    public static void takeString(String title, String author, boolean loanStatus, String borrowerID, GregorianCalendar loanDate, GregorianCalendar dueDate) {
    Book book1 = new Book();
    }

    public static void main(String[] args) {
    Book book1 = new Book();
    //book1.takeString("Data Structure", "Michael Main", true, 1234, 01/8/2003, 15/8/2003);
    }
    }

    //End of Code

    How can i use the toString method to return the Book object into the String format? using StringTokenizer? Anyone can help?

    Btw, is this the right forum to post this question? Thank You...

  2. #2
    Senior Member
    Join Date
    Jun 2002
    Posts
    405
    Code:
    public String toString() { 
    
    String stringOne;
    
    stringOne = ("" + title + ";" + author + ";" + loanStatus + ";" + borrowerID + ";" + loanDate + ";" + dueDate);
    
    return stringOne;
    }
    This method compiled fine for me so it should be all good at your end. Assuming I interpreted your question correctly

  3. #3
    Senior Member
    Join Date
    Nov 2002
    Posts
    186
    I think PowerToad answered your question. You did mention the StringTokenizer class. You would probably want to use that in the create() method though. For the data structure of that format: Data Structure;Michael Main;true;1234;01/08/2003;15/08/2003, assuming the string is called inString, you would want to:
    Code:
    StringTokenizer st = new StringTokenizer(inString, ";");
    title = ((String)st.nextElement());
    .... (for each item)
    This code would go in the create() method. Remember nextElement() returns an Object so you need to cast it to the appropriate type.
    For info on the StringTokenzier refer to: http://java.sun.com/j2se/1.4.1/docs/...Tokenizer.html

    If you aren't comfortable with the StringTokenizer, you could pass in all the data seperately but that does not sound like what the problem describes.
    Good luck!
    \"When you say best friends, it means friends forever\" Brand New
    \"Best friends means I pulled the trigger
    Best friends means you get what you deserve\" Taking Back Sunday
    Visit alastairgrant.ca

  4. #4
    Senior Member
    Join Date
    May 2003
    Posts
    226
    What i have coded is:

    //start of code
    import java.util.*;

    public class Book {
    String title;
    String author;
    boolean loanStatus;
    String borrowerID;
    GregorianCalendar loanDate;
    GregorianCalendar dueDate;

    public String toString() {
    String stringOne;

    stringOne = ("" + title + ";" + author + ";" + loanStatus + ";" + borrowerID + ";" + loanDate + ";" + dueDate);
    return stringOne;
    }

    public static void takeString(String title, String author, String loanStatus, String borrowerID, String loanDate, String dueDate) {
    Book book1 = new Book();
    String stringOne = book1.toString();
    StringTokenizer st = new StringTokenizer(stringOne, ";");
    title = (String)st.nextElement();
    author = (String)st.nextElement();
    loanStatus = (String)st.nextElement();
    borrowerID = (String)st.nextElement();
    loanDate = (String)st.nextElement();
    dueDate = (String)st.nextElement();

    }

    public static void main(String[] args) {
    Book book1 = new Book();
    book1.takeString("Data Structure;", "Michael Main;", "true;", "1234;", "01/08/2003;", "15/08/2003");
    }
    }

    //end of code

    But i'm wondering that [b) define a method(toString) that returns the Book object as a string format given:]

    How do i return a book object just by doing "stringOne = ("" + title + ";" + author + ";" + loanStatus + ";" + borrowerID + ";" + loanDate + ";" + dueDate);"

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •