Sorry - thought it was pretty much the same question! So here's the new thread ... will add more when I have something working.
Joined: Apr 29, 2007
The question was:
"The question opens with a class of a rectangle it has methods to set the dimensions return the width height and area and print these out
the question is
a) Write a method that takes as a parameter an array of Rectangle objects and prints out the details of every rectangle stored in this array.
b) Write a method that takes as a parameter an array of Rectangle objects and returns an array of the areas of the rectangles stored in this array"
1. We don't have to construct the rectangles 2. The methods that return the height, width and area are called getHeight, getWidth and getArea respectively 3. I don't know whether there is one method which prints out all properties of a rectangle or separate methods to print out each property; since the question is easier to answer if there is already a method to print everything in an understandable way, I'm going to make that assumption - this (hypothetical) method will be called printRectangle.
Comments? hints? criticisms? (I'm learning Java from books and don't have a whole lot of "exercises" to practice on ...)
EDIT by mw: Added Code Tags. [ May 30, 2007: Message edited by: marc weber ]
That's a great way to learn! There are a few details that need to be worked out here. The best way is to complete the code with a Rectangle class and see how it really works.
One tip: When declaring a method argument list, you need to specify the type along with the variable name. For example, (Rectangle rectangles) where "Rectangle" is the type (an array of Rectangle references) and "rectangles" is the local variable referencing that array. [ May 30, 2007: Message edited by: marc weber ]
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer sscce.org
Sorry, I didn't realise you were asking about the same rectangles question. You have managed more than that other chap. You won't get your enhanced for loop (also called a for-each loop) to work like that. Look here, and use ctrl-F->"enhanced". You can miss out the bit about i and (as Marc Weber has already told you) put "Rectangle rectangles" as your parameters.
Joined: Oct 13, 2005
Only two changes needed for your "rectangleAreas" method. Apart from the change of the parameter to "Rectangle rectangles" which Marc Weber told you about, you need to insert a statement to return the array of areas.
Any method which has a return type must have a statement to return something of that type, and a method with "void" must not return anything at all.
Joined: Apr 29, 2007
Marc and Campbell -
Thank you both very much! Marc, thank you for the "prod"; I needed it. Campbell - thank you for the hints.
Having spent much too long plumbing the depths of my misunderstandings about object orientation, I finally have created something that works. Right. Yeeeee-HA!