Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: .toString method question!

  1. #1
    Member Scorks's Avatar
    Join Date
    Jan 2013
    Posts
    56
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default .toString method question!

    Hello there!

    So, I just finished a rather long program, dealing with inheritance. in each class, I was asked to include a toString method. Now, the problem is, inside of my subclasses I have methods such as getArea and getPerimeter. in my toString method for this class, I want toString to run and print the area of the object that toString is invoked upon, for example. I just don't know the syntax of this... I though it would be:

    "Area : " + this.getArea()

    but it gives me an error. So, how would I refer to this *unknown* object in my toString class?

    Thanks a lot!


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: .toString method question!

    What is the error message? Please copy/paste it here in its entirety.

  3. #3
    Member Scorks's Avatar
    Join Date
    Jan 2013
    Posts
    56
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: .toString method question!

    Well, I've chopped that part out. Now, I'm having a lot of trouble with my return statements. NONE of my return statements are printing. It's compiling, but when I run a method on an object, I just get a blank...

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: .toString method question!

    We can't read your mind or see what is on your computer screen. You have to post enough information here for us to be able to help you. What code you you have so far, what is it doing (exactly), and what are you expecting (exactly)?

  5. #5
    Member Scorks's Avatar
    Join Date
    Jan 2013
    Posts
    56
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: .toString method question!

    Sorry, here's the code I'm working with:

    class ARectangle extends AnObject {
     
    double Width;
    double Length;
     
    //No parameter constructor
     
    public ARectangle() {
    double Width;
    double Length;
    }
     
    // 2 Parameter constructor
     
    public ARectangle(double Width1, double Length2) {
    Width = Width1;
    Length = Length2;
    }
     
    // 4 Parameter constructor
     
    public ARectangle(double Width1, double Length1, String Colour1, String Filled1) {
    Width = Width1;
    Length = Length1;
    Colour = Colour1;
    Filled = Filled1;
    }
     
    //ACCESSOR AND MUTATOR METHODS
     
    public double geWidth() {
    return Width;
    }
     
    public double getLength() {
    return Length;
    }
     
    public void setWidth(double Width1) {
    Width = Width1;
    }
     
    public void setLength(double Length1) {
    Length = Length1;
    }
     
    //GET AREA METHOD
     
    public double getArea() {
    double Area = (Width*Length);
    return Area;
    }
     
    // GET PERIMETER METHOD
     
    public double getPerimeter() {
    double Perimeter = (Width + Width + Length + Length);
    return Perimeter;
    }
     
    // TOSTRING METHOD
    public String toString() {
    return "\n" + "Width : " + Width + "\n" + "Length : " + Length;
    }

    Then, when I put this in the main method:

    ARectangle rec = new ARectangle(1.0,3.0);
    rec.getArea();

    it compiles, but then I run it and nothing is printed...

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: .toString method question!

    The purpose of a toString() method is to provide the desired String representation of an instance of the class in which the toString() method exists for use in an output statement. So, if your main() method (or whatever you're using to test the ARectangle class) included an output statement, like:

    System.out.println( "This is the rectangle: " + rec );

    'rec' would automatically be output as rec.toString(). If you've done that and are getting nothing "printed," then post the full driver program or main method so that we can see what's going on.

  7. #7
    Member
    Join Date
    Sep 2013
    Posts
    70
    Thanks
    1
    Thanked 13 Times in 13 Posts

    Default Re: .toString method question!

    ARectangle rec = new ARectangle(1.0,3.0);
    rec.getArea();

    Won't print out anything. Your getArea() method returns a double to the main but since you are not displaying it or assigning it to anything so the main doesn't really do anything with it.

    As for the toString() as mentioned by GregBrannon it is a way for the object to be displayed in the format you want it. If you don't include the toString() method and just use System.out.println(rec); it will give you something like ARectangle@be230d5.

    In your initial post you mentioned doing "Area" + this.getArea(); What error did you get back then? Also keep in mind a toString() method must return a string.

Similar Threads

  1. toString method
    By pelane in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 23rd, 2012, 01:42 AM
  2. [SOLVED] Question about using the toString method.
    By iDizzle in forum Object Oriented Programming
    Replies: 12
    Last Post: April 9th, 2012, 09:24 AM
  3. toString method
    By feldmanb700 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 17th, 2011, 09:20 PM
  4. Help with toString method and an addObject method?
    By Camisado in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 12th, 2011, 07:00 AM
  5. [SOLVED] toString() method
    By chronoz13 in forum Object Oriented Programming
    Replies: 12
    Last Post: January 19th, 2010, 06:44 AM

Tags for this Thread