Results 1 to 13 of 13
  1. #1
    teekei is offline Member
    Join Date
    May 2011
    Posts
    39
    Rep Power
    0

    Default Code for class is not compiling, I'm lost.

    public class RectangleClass
    {
    private double length = 1.0;
    private double width = 1.0;
    private double perimeter = 0.0;
    private double area = 0.0;

    public void setLength(double length)
    {
    if (length > 0.0 && length < 20.0)
    {
    this.length = length;
    }
    }

    public void setWidth( double width)
    {
    if ( width > 0.0 && width < 20.0)
    {
    this.width = width;
    }
    }
    public double getLength()
    {
    return this.length;
    }
    public double getWidth()
    {
    return this.width;
    }
    public double calcPerimeter()
    {
    perimeter = ((2 * length) + (2* width));
    }
    public double calcArea()
    {
    area = length * width;
    }
    public String toString(double length, double width, double perimeter, double area)
    {
    return String.format(System.out.print("length: %d", length\n "width: %d", width\n "perimeter: %d", perimeter\n
    "area: %d", area\n));
    }
    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default

    What's the error shown? Copy and paste the error text, don't paraphrase.

    And edit your post to use code tags so the code retains its legibility.

    db

  3. #3
    airowe is offline Member
    Join Date
    Jan 2011
    Posts
    23
    Rep Power
    0

    Default

    What do you mean by not compiling? Do you have a main class?

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Use code tags:
    [code]
    YOUR CODE HERE
    [/code]

    Are we supposed to guess at your problem? Why not give us the error messages? Don't just dump your code here. Ask specific questions about what you don't understand, and provide is with any and all errors(exact errors(copy/paste not paraphrasing))

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Quote Originally Posted by airowe View Post
    Do you have a main class?
    So you mean a class with a main method? If so that has nothing to do with it. You should still be able to compile a class if there is no main method.

  6. #6
    airowe is offline Member
    Join Date
    Jan 2011
    Posts
    23
    Rep Power
    0

    Default

    Oh, I know that. Just a leading question to try and get some more info... :)

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Java Code:
    return String.format(System.out.print("length: %d", length\n "width: %d", width\n "perimeter: %d", perimeter\n
    "area: %d", area\n));
    Well there's ya problem. System.out.print method is void so there is no return value to format.

  8. #8
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Also, you should probably override toString and not overload it.

  9. #9
    teekei is offline Member
    Join Date
    May 2011
    Posts
    39
    Rep Power
    0

    Default

    I apologize for not adding the error messages(still fairly new).

    --------------------Configuration: RectangleTest - JDK version 1.6.0_23 <Default> - <Default>--------------------
    C:\Users\Timzareli\Documents\JCreator LE\MyProjects\RectangleTest\src\RectangleClassTest .java:41: illegal character: \92
    return String.format("length: %d", length\n "width: %d", width\n "perimeter: %d", perimeter\n
    ^
    C:\Users\Timzareli\Documents\JCreator LE\MyProjects\RectangleTest\src\RectangleClassTest .java:41: not a statement
    return String.format("length: %d", length\n "width: %d", width\n "perimeter: %d", perimeter\n
    ^
    C:\Users\Timzareli\Documents\JCreator LE\MyProjects\RectangleTest\src\RectangleClassTest .java:41: ';' expected
    return String.format("length: %d", length\n "width: %d", width\n "perimeter: %d", perimeter\n
    ^
    C:\Users\Timzareli\Documents\JCreator LE\MyProjects\RectangleTest\src\RectangleClassTest .java:41: illegal character: \92
    return String.format("length: %d", length\n "width: %d", width\n "perimeter: %d", perimeter\n
    ^
    C:\Users\Timzareli\Documents\JCreator LE\MyProjects\RectangleTest\src\RectangleClassTest .java:41: not a statement
    return String.format("length: %d", length\n "width: %d", width\n "perimeter: %d", perimeter\n
    ^
    C:\Users\Timzareli\Documents\JCreator LE\MyProjects\RectangleTest\src\RectangleClassTest .java:41: not a statement
    return String.format("length: %d", length\n "width: %d", width\n "perimeter: %d", perimeter\n
    ^
    C:\Users\Timzareli\Documents\JCreator LE\MyProjects\RectangleTest\src\RectangleClassTest .java:41: ';' expected
    return String.format("length: %d", length\n "width: %d", width\n "perimeter: %d", perimeter\n
    ^
    C:\Users\Timzareli\Documents\JCreator LE\MyProjects\RectangleTest\src\RectangleClassTest .java:41: illegal character: \92
    return String.format("length: %d", length\n "width: %d", width\n "perimeter: %d", perimeter\n
    ^
    C:\Users\Timzareli\Documents\JCreator LE\MyProjects\RectangleTest\src\RectangleClassTest .java:41: not a statement
    return String.format("length: %d", length\n "width: %d", width\n "perimeter: %d", perimeter\n
    ^
    C:\Users\Timzareli\Documents\JCreator LE\MyProjects\RectangleTest\src\RectangleClassTest .java:41: not a statement
    return String.format("length: %d", length\n "width: %d", width\n "perimeter: %d", perimeter\n
    ^
    C:\Users\Timzareli\Documents\JCreator LE\MyProjects\RectangleTest\src\RectangleClassTest .java:41: ';' expected
    return String.format("length: %d", length\n "width: %d", width\n "perimeter: %d", perimeter\n
    ^
    C:\Users\Timzareli\Documents\JCreator LE\MyProjects\RectangleTest\src\RectangleClassTest .java:42: illegal character: \92
    "area: %d", area\n);
    ^
    C:\Users\Timzareli\Documents\JCreator LE\MyProjects\RectangleTest\src\RectangleClassTest .java:42: not a statement
    "area: %d", area\n);
    ^
    C:\Users\Timzareli\Documents\JCreator LE\MyProjects\RectangleTest\src\RectangleClassTest .java:42: not a statement
    "area: %d", area\n);
    ^
    C:\Users\Timzareli\Documents\JCreator LE\MyProjects\RectangleTest\src\RectangleClassTest .java:42: ';' expected
    "area: %d", area\n);
    ^
    15 errors

    Process completed.

    If you don't mind me asking, how do I put in the "tags" for the code that I paste here??

    Thank you

  10. #10
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Looks like a lot of your problems stem from improper use of String.format.

    The format method should take a string of some length and following it should be a list of arguments.

    To use code tags, type
    [code]
    YOUR CODE HERE
    [/code]

    String format also shouldn't have a System.out.print inside of it. You also probably didn't mean to overload toString and meant to override it.

    Java Code:
    String.format("here is a %s, %d, %f", "string", 5, 5.005);
    Notice the difference between mine and yours.

  11. #11
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Did you read the API for the format method? The paremeters are (String, Object...). The second parameter is what is called varargs. That means you can pass 1, 2, 3, M objects. The number of these objects must match the number of formatters in the String.

    Your code on the other hand is (String, Object, String, Object, String, Object, etc).

    For code tags you type [ code ] before and [ /code ] after your code (without the spaces).

  12. #12
    teekei is offline Member
    Join Date
    May 2011
    Posts
    39
    Rep Power
    0

    Default

    Everything I was beneficial in my code compiling correctly and I greatly appreciate the help. Next time, I will be sure to tag my code and the errors.

    Thank you

  13. #13
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Glad to have helped.

    To explain the reason why you should override the toString and not overload is because if you overload it, you are passing the arguments to be returned into the method. The toString method is meant to return a string that describes the object it is called on. However, if you overload it you must pass the arguments to be used into it, this can lead to the same object returning different strings on multiple calls of toString and the object may be identical.

    Instead you should override it, and use the @Override annotation to enforce it.
    Java Code:
    @Override public String toString()
    Then use the instance variables to return the description as a string.

Similar Threads

  1. simple code not compiling
    By cliffh in forum New To Java
    Replies: 8
    Last Post: 09-30-2010, 05:47 AM
  2. my code compiling but not running
    By girishkumar in forum New To Java
    Replies: 16
    Last Post: 03-16-2010, 04:45 PM
  3. Help me out in compiling the source code
    By aks.nitw in forum Advanced Java
    Replies: 3
    Last Post: 10-17-2008, 08:33 AM
  4. Trouble compiling code
    By waelhelbawi in forum New To Java
    Replies: 1
    Last Post: 05-12-2008, 04:25 AM
  5. I am so lost in my java class
    By taliGLM in forum New To Java
    Replies: 1
    Last Post: 04-30-2008, 10:02 PM

Posting Permissions

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