Results 1 to 13 of 13
- 06-06-2011, 04:50 AM #1
Member
- Join Date
- May 2011
- Posts
- 39
- Rep Power
- 0
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));
}
}
- 06-06-2011, 04:54 AM #2
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
- 06-06-2011, 04:56 AM #3
Member
- Join Date
- Jan 2011
- Posts
- 23
- Rep Power
- 0
What do you mean by not compiling? Do you have a main class?
- 06-06-2011, 04:56 AM #4
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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))
- 06-06-2011, 04:58 AM #5
- 06-06-2011, 04:59 AM #6
Member
- Join Date
- Jan 2011
- Posts
- 23
- Rep Power
- 0
Oh, I know that. Just a leading question to try and get some more info... :)
- 06-06-2011, 04:59 AM #7
Well there's ya problem. System.out.print method is void so there is no return value to format.Java Code:return String.format(System.out.print("length: %d", length\n "width: %d", width\n "perimeter: %d", perimeter\n "area: %d", area\n));
- 06-06-2011, 05:00 AM #8
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Also, you should probably override toString and not overload it.
- 06-06-2011, 05:33 AM #9
Member
- Join Date
- May 2011
- Posts
- 39
- Rep Power
- 0
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
- 06-06-2011, 05:39 AM #10
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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.
Notice the difference between mine and yours.Java Code:String.format("here is a %s, %d, %f", "string", 5, 5.005);
- 06-06-2011, 05:43 AM #11
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).
- 06-06-2011, 05:52 AM #12
Member
- Join Date
- May 2011
- Posts
- 39
- Rep Power
- 0
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
- 06-06-2011, 06:27 AM #13
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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.
Then use the instance variables to return the description as a string.Java Code:@Override public String toString()
Similar Threads
-
simple code not compiling
By cliffh in forum New To JavaReplies: 8Last Post: 09-30-2010, 05:47 AM -
my code compiling but not running
By girishkumar in forum New To JavaReplies: 16Last Post: 03-16-2010, 04:45 PM -
Help me out in compiling the source code
By aks.nitw in forum Advanced JavaReplies: 3Last Post: 10-17-2008, 08:33 AM -
Trouble compiling code
By waelhelbawi in forum New To JavaReplies: 1Last Post: 05-12-2008, 04:25 AM -
I am so lost in my java class
By taliGLM in forum New To JavaReplies: 1Last Post: 04-30-2008, 10:02 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks