*
The moose likes Beginning Java and the fly likes array of rectangles exam question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "array of rectangles exam question" Watch "array of rectangles exam question" New topic
Author

array of rectangles exam question

Jinny Morris
Ranch Hand

Joined: Apr 29, 2007
Posts: 101
Mr. Ritchie -

Sorry - thought it was pretty much the same question! So here's the new thread ... will add more when I have something working.
Jinny Morris
Ranch Hand

Joined: Apr 29, 2007
Posts: 101
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"

assumptions:

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.

So:

Comments? hints? criticisms? (I'm learning Java from books and don't have a whole lot of "exercises" to practice on ...)

Thanks!


EDIT by mw: Added Code Tags.
[ May 30, 2007: Message edited by: marc weber ]
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Jinny Morris:
Mr. Ritchie -

Sorry - thought it was pretty much the same question! So here's the new thread ... will add more when I have something working.

(For anyone wondering, this was prompted by this thread.)
Jinny Morris
Ranch Hand

Joined: Apr 29, 2007
Posts: 101
Marc -

Thanks! I'll try that - so probably won't post again right away. No self-confidence ...
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39044
    
  23
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.

Good luck.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39044
    
  23
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.
Jinny Morris
Ranch Hand

Joined: Apr 29, 2007
Posts: 101
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!

 
GeeCON Prague 2014
 
subject: array of rectangles exam question