daily pastebin goal
24%
SHARE
TWEET

Untitled

a guest Dec 13th, 2017 61 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.awt.Rectangle;
  2. import java.util.Arrays;
  3. import java.util.Comparator;
  4. public class ComparingRectangles
  5. {
  6.  
  7.    public static void main(String[] args)
  8.    {
  9.       Rectangle[] rectangles = {
  10.             new Rectangle(0, 0, 4, 2),
  11.             new Rectangle(0, 0, 7, 2),
  12.             new Rectangle(0, 0, 5, 3),
  13.             new Rectangle(0, 0, 3, 4),          
  14.             new Rectangle(0, 0, 6, 2) };
  15.  
  16.       //code to sort by perimeter here
  17.       Arrays.sort(rectangles, new Comparator<Rectangle>()
  18.       {
  19.          public int compare(Rectangle t1,Rectangle t2)
  20.          {
  21.             return Double.compare(t1.getWidth() +t1.getWidth() + t1.getHeight() + t1.getHeight(), t2.getWidth() +t2.getWidth() + t2.getHeight() + t2.getHeight());
  22.          }
  23.      
  24.       }
  25.       );
  26.    
  27.       System.out.println("**Sorted by perimeter");
  28.       for (Rectangle r : rectangles)
  29.       {
  30.          System.out.println(r);
  31.       }
  32.      
  33.       class RectangleComparatorByHeight implements Comparator<Rectangle>
  34.       {
  35.          public int compare(Rectangle t1, Rectangle t2)
  36.          {
  37.             return Double.compare(t1.getHeight() * t1.getWidth(), t2.getHeight() * t2.getWidth());
  38.          }
  39.       }  
  40.      
  41.     //code to sort by area here
  42.       Arrays.sort(rectangles, new RectangleComparatorByHeight());
  43.       System.out.println("**Sorted by area");
  44.  
  45.       for (Rectangle r : rectangles)
  46.       {
  47.          System.out.println(r);
  48.  
  49.       }
  50.    }
  51. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top