This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds and have James Denton on-line!
See this thread for details.
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

array of rectangles exam question  RSS feed

 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marc -

Thanks! I'll try that - so probably won't post again right away. No self-confidence ...
 
Marshal
Posts: 60105
188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Posts: 60105
188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!

  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!