I'm back again (just like i said i would!) haha. anyways, this program is something i had so complete, and based a lot of the code off another example, which works perfectly, however, i'm getting a "recursive constructor invocation" error, that i don't understand, and 6 other errors which all stem from the same basic concept that i don't understand. "cannot find symbol" when calling on Rectangle1.java from the Rectangle1Test.java i'm in way over my head here, so help is greatly appreciated.OH, my instructions as to what i'm supposed to accomplish are in the comments at the beginning of the program. this chapter i "learned" the concepts of "this" and overloaded constructors, so im supposed to utilize those.....(i think)

2 programs

Code:

// ex8.4 Rectangle1.java
// Create a class Rectangle with attributes "length", "width", each of which
//defaults to 1. Provide methods that calculate the rectangle's perimeter and area
//It has SET and GET methods for both length and width. The SET methods should verify
//that the length and width are each floating-point numbers larger than 0.0 and less 20.0

//// ex8.4 Rectangle1Test.java
// Create a class Rectangle with attributes "length", "width", each of which
//defaults to 1. Provide methods that calculate the rectangle's perimeter and area
//It has SET and GET methods for both length and width. The SET methods should verify
//that the length and width are each floating-point numbers larger than 0.0 and less 20.0

The errors about println occur because println takes only one argument. You provide two. Have a look at Printstream (the type of System.out) to find the correct method.

The other errors occur because of a typo.

Success.

honestly, like i said, im not entirely familiar with the new "this" keyword, and im ASSUMING that this sets the l (length) and w (width) to float numbers, as opposed to using the initial value? not sure though.

11-14-2010, 01:13 AM

pbrockway2

No, you would set those variables with

Code:

length = l;
width = w;

If you think about it, how is Java to know that l goes with length and w with width unless you tell it.

Perhaps you should consult your textbooks etc in an attempt to answer Erik's question. Or read Using the this Keyword in Oracle's Tutorial. Because you ought to wonder why this(l,w) failed while the other this() calls did not.

Inside the constructor, the this.width and this.width statements refer to instance variables. This is needed as just writing width would instead refer to the width variable passed into the constructor.

The this keyword can also be used to call the different constructors of the class.

In the above example, the this keyword is being used to call the other constructors in the class. The MySize(int) constructor contains the call "this()" which calls the MySize() constructor, but since this isn't really doing anything its not noticeable. The MySize(int, int) constructor contains the call "this(height)" which calls the MySize(int) constructor which sets the height of the class.

I've used the above to consolidate the logic/code when a class has a lot of different constructors.

11-14-2010, 01:33 AM

hayden06f4i

i figured out the mistakes about the print output based on what you said, oversight on my part with using println instead of printf, and misspelling perimeter (3x).

as i said, this is all still over my head, and im trying to comprehend it by reading a book, and its an online course, so help from a instructor is days away....trying to learn it on my own.

i ASSUME that when i set up a this with

( 1, 1) it sets the sides to the default of 1 and 1?
( L, 1) capital l used for clarity at the moment uses...Length as a floating varible and 1?
( L, W) i though would use both use floating variables. i guess i don't really understand the concept.

11-14-2010, 01:37 AM

hayden06f4i

instead of using "this" should that line read?:

Code:

setRectangle1 ( l,w );

11-14-2010, 02:53 AM

hayden06f4i

sorry for the now (triple post) but im just updating on progress, and seeking addntl help...

i am reviewing my book, but not sure at all how to write the return statement. any help would be greatly appreciated, sorry for a million questions, but im only 2 weeks into this Java class, so im a newbie.

11-14-2010, 02:58 AM

al_Marshy_1981

post the methods at line 71 and 79, im guessing they are calculate Perimeter and calculate Area methods since they do not return floats

11-14-2010, 03:05 AM

hayden06f4i

this is what i have so far, but its way off base. i know i need a "return" value somehow.

Code:

// calculate area
public float calculateArea ()
{
float area;

area = ( getLength () * getWidth () );

} // end calculate area

public float calculatePerimeter ()
{
float perimeter;

perimeter = ( (getLength() * 2) + (getWidth() * 2) );

11-14-2010, 03:09 AM

al_Marshy_1981

yup calculateArea and calculatePerimeter methods are expecting a return type of float....you have float variables inside those methods namely area and perimeter, return them.

the reason you need a return type is in your method declaration

[code]
public float calculateArea(){}
[/code[

the float keyword in the method declaration means you want this method to return a type of float...

11-14-2010, 04:02 AM

hayden06f4i

how do i appropriately return them? the only example i have in the book is the following, with no explanations:

but that example doesn't need a calculation, like i need to calculate area from the length and width.

11-14-2010, 04:15 AM

al_Marshy_1981

the example you gave

Code:

public String toString()
{}

this method expects a String as a return type, again look at the type after the access modifier (public) in this case.

Not sure what you mean by needing calculations since it looks like you have already, see your methods...

Code:

// calculate area
public float calculateArea ()
{
float area;

area = ( getLength () * getWidth () ); // calculation

} // end calculate area

public float calculatePerimeter ()
{
float perimeter;

perimeter = ( (getLength() * 2) + (getWidth() * 2) ); //calculation

literally you need to return the float variables area and perimeter. Please, please understand the reason for this however, and the clue is in your method declarations after the access modifier(public) in your case, comes the return type (float) in your case, this means this method must return a type of float. If your variables area and perimeter are floats, what are you missing from these methods? see you String method for an example of returning a type

11-14-2010, 02:39 PM

hayden06f4i

i understand the reason WHY i needed to return a value. tried all these complicated ways to return a value, when all i had to do was add

return area;

to the end of my statement.....wow, i should have realized i was tired last night, it clicked first thing this morning. thank you.

now in continuing on, it completes the compiling, but during runtime i get an exception error....i will troubleshoot that awhile, and post details up if i can't get through it.

11-14-2010, 02:50 PM

hayden06f4i

Code:

//// ex8.4 Rectangle1Test.java
// Create a class Rectangle with attributes "length", "width", each of which
//defaults to 1. Provide methods that calculate the rectangle's perimeter and area
//It has SET and GET methods for both length and width. The SET methods should verify
//that the length and width are each floating-point numbers larger than 0.0 and less 20.0

got it working! had to change the %d to %s, which raises a question. i thought %s was for inserting a string into a printf? not inserting a float? is there an online list that shows what each one does, so i can keep it for reference, until i memorize it better? im always forgetting. just something basic like