Page 2 of 2 FirstFirst 12
Results 21 to 32 of 32
  1. #21
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by gcalvin View Post
    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. #22
    bdario1 is offline Member
    Join Date
    Mar 2010
    Posts
    46
    Rep Power
    0

    Default

    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 06:45 AM.

  3. #23
    bdario1 is offline Member
    Join Date
    Mar 2010
    Posts
    46
    Rep Power
    0

    Default

    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 06:59 AM.

  4. #24
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    It looks right to me. Why do you believe it to be wrong?

    -Gary-

  5. #25
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Quote Originally Posted by Fubarable View Post
    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. #26
    bdario1 is offline Member
    Join Date
    Mar 2010
    Posts
    46
    Rep Power
    0

    Default

    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. #27
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    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.

    As for your question:
    Quote Originally Posted by bdario1 View Post
    ...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. #28
    bdario1 is offline Member
    Join Date
    Mar 2010
    Posts
    46
    Rep Power
    0

    Default

    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. #29
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Quote Originally Posted by bdario1 View Post
    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.
    Quote Originally Posted by bdario1 View Post
    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. #30
    bdario1 is offline Member
    Join Date
    Mar 2010
    Posts
    46
    Rep Power
    0

    Default

    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. #31
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Quote Originally Posted by bdario1 View Post
    Ok so i can get rid of the area==0 comparison then?
    Yes.
    Quote Originally Posted by bdario1 View Post
    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. #32
    bdario1 is offline Member
    Join Date
    Mar 2010
    Posts
    46
    Rep Power
    0

    Default

    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 FirstFirst 12

Similar Threads

  1. Replies: 2
    Last Post: 03-26-2010, 05:12 PM
  2. ArrayLists compareTo method, equals method
    By random0munky in forum New To Java
    Replies: 2
    Last Post: 10-26-2009, 07:20 PM
  3. Replies: 1
    Last Post: 07-16-2009, 02:15 PM
  4. How to Draw Round Rectangles in SWT
    By Java Tip in forum SWT
    Replies: 0
    Last Post: 06-28-2008, 09:25 PM
  5. cannot call private method from static method
    By jon80 in forum New To Java
    Replies: 3
    Last Post: 05-07-2008, 08:37 AM

Posting Permissions

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