1. Originally Posted by gcalvin
The actual findSmallest() method will go something like this:
Java Code:
```    set Rectangle result to null
set smallestArea to 0  // ***** possible typo error here ****
for each Rectangle in array
if this Rectangle's area is less than smallestArea or smallestArea is 0
set smallestArea to this Rectangle's area
set result to this Rectangle
return result```
You should write a getArea(Rectangle) method, if the Rectangle class doesn't already give you one.

-Gary-
To clarify and to avoid confusion, I think that Gary's advice is spot on, except I think he has a typographical in the line indicated above. Again, I'd use Integer.MAX_VALUE, and then I'd do everything else he suggests -- to the letter!

Again, best of luck!

2. Member
Join Date
Mar 2010
Posts
46
Rep Power
0
Ok i know how to print it out but how do i compute the area of that rectangle thats where i am confused at. since the test they will run on it will give it like 3 - 4 or even an empty array of rectangles and once i compute the area i need to show the smallest area. I just can't get past the computing the area part. For some reason it just doesn't make sense to me
Last edited by bdario1; 03-31-2010 at 07:45 AM.

3. Member
Join Date
Mar 2010
Posts
46
Rep Power
0
Java Code:
```public static Rectangle findSmallest (Rectangle[] rectangles)
{

{

Rectangle smallestRectangle = null;
double smallestArea = Integer.MAX_VALUE;

for(int i=0; i < rectangles.length; i++)
{
double width = rectangles[i].getWidth();
double height = rectangles[i].getHeight();
double area = width * height;
if((area < smallestArea) || smallestArea == 0)
{
smallestArea = area;
smallestRectangle = rectangles[i] ;
}
}
return smallestRectangle;
}

}```
This is what i got so far. But i seem to have a problem on specifying the correct rectangle please let me know where i could be making a mistake.
Last edited by bdario1; 03-31-2010 at 07:59 AM.

4. Senior Member
Join Date
Mar 2010
Posts
952
Rep Power
8
It looks right to me. Why do you believe it to be wrong?

-Gary-

5. Senior Member
Join Date
Mar 2010
Posts
952
Rep Power
8
Originally Posted by Fubarable
To clarify and to avoid confusion, I think that Gary's advice is spot on, except I think he has a typographical in the line indicated above. Again, I'd use Integer.MAX_VALUE, and then I'd do everything else he suggests -- to the letter!

Again, best of luck!
The second comparison two lines down from there should take care of the perceived problem, but it may have been better to use a -1 in the initialization and comparison, because it's possible that a rectangle will really have a zero area.

-Gary-

6. Member
Join Date
Mar 2010
Posts
46
Rep Power
0
Well i had it return a double first that why i was getting an error, but then i figured out that i had the smallestRectangle as a Rectangle object so i fixed that issue also I have a question if the two rectangles have the same area of lets say 20 i need to return the last object seen will this do that for me, or do i need to include something else in there?
Java Code:
`if((area <= smallestArea) || smallestArea == 0)`

7. Senior Member
Join Date
Mar 2010
Posts
952
Rep Power
8
You don't need the smallestArea == 0 comparison, because you are not initializing smallestArea to zero. That comparison is really only about setting smallestArea to the first Rectangle's area, the first time through the loop.

Originally Posted by bdario1
...if the two rectangles have the same area of lets say 20 i need to return the last object seen will this do that for me, or do i need to include something else in there?
Java Code:
`if((area <= smallestArea) || smallestArea == 0)`
Think it through. This is good practice and a good skill to develop. Trace through your code in your head, or use a pencil and paper. Your smallestArea variable is already set to 20, and another Rectangle comes through the loop, and its area is 20 also -- what happens? Is it what you want? What would happen if you had (area < smallestArea) instead of (area <= smallestArea)?

-Gary-

8. Member
Join Date
Mar 2010
Posts
46
Rep Power
0
Ok so instead of comparing the smallestArea to zero should i compare the area == 0 since i need to return that rectangle if that is the case. And for the same area, i would think that the rectangle with the first 20 would be returned since it is not smaller than the current value. So i think that (area<=smallestArea) should be the right. Correct me if i am wrong.

9. Senior Member
Join Date
Mar 2010
Posts
952
Rep Power
8
Originally Posted by bdario1
Ok so instead of comparing the smallestArea to zero should i compare the area == 0 since i need to return that rectangle if that is the case.
No, you don't compare anything to zero. You didn't initialize smallestArea to zero (as I did in my pseudo-code example), so you don't need that comparison.
Originally Posted by bdario1
And for the same area, i would think that the rectangle with the first 20 would be returned since it is not smaller than the current value. So i think that (area<=smallestArea) should be the right. Correct me if i am wrong.
You have me completely confused here. Try saying that again, a little more clearly.

-Gary-

10. Member
Join Date
Mar 2010
Posts
46
Rep Power
0
Ok so i can get rid of the area==0 comparison then?
Sorry about that, what i am saying, in case two rectangles have the same area i need to return only the last rectangle
Java Code:
` For findSmallest, the input array{new Rectangle(0, 0, 3, 8), new Rectangle(1, 1, 6, 7), new Rectangle(2, 2, 6, 4)} should return the last rectangle object - Rectangle(2, 2, 6, 4).`
and i am wondering will this
Java Code:
`(area<=smallestArea)`
Do that also for me.

11. Senior Member
Join Date
Mar 2010
Posts
952
Rep Power
8
Originally Posted by bdario1
Ok so i can get rid of the area==0 comparison then?
Yes.
Originally Posted by bdario1
Sorry about that, what i am saying, in case two rectangles have the same area i need to return only the last rectangle
Java Code:
` For findSmallest, the input array{new Rectangle(0, 0, 3, 8), new Rectangle(1, 1, 6, 7), new Rectangle(2, 2, 6, 4)} should return the last rectangle object - Rectangle(2, 2, 6, 4).`
and i am wondering will this
Java Code:
`(area<=smallestArea)`
Do that also for me.
I understand that this is what you are wondering. However, you should not seek the answer in an Internet forum. Even if I told you the answer, you should not believe me. You should:
1. Think it through, and
2. Test it.

-Gary-

12. Member
Join Date
Mar 2010
Posts
46
Rep Power
0
Yea i have done that just wanted to make sure i was not making a mistake somewhere since i was having a lot of trouble with this one. And it makes sense to use <= since if the area of the previous rectangle is equal to the smallestArea it will still go through the if statement and assign that area to that smallestArea. Thanks for all of your help

Page 2 of 2 First 12

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•