Advertisement
Guest User

Untitled

a guest
Jul 24th, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 KB | None | 0 0
  1.  
  2. public class Rectangle {
  3. private double w;
  4. private double h;
  5.  
  6. public Rectangle(double w, double h){
  7. this.w=w;
  8. this.h=h;
  9. }
  10.  
  11. public Rectangle(Rectangle r){
  12. this.w=r.w;
  13. this.h=r.h;
  14. }
  15.  
  16. public double perimeter(){
  17. return 2*h+2*w;
  18. }
  19.  
  20. public double area(){
  21. return h*w;
  22. }
  23.  
  24. public String toString() {
  25. return "Rectangle [w=" + w + ", h=" + h + "]";
  26. }
  27.  
  28. public boolean contains(Point p){
  29. if (p.getX()<this.w && p.getY()<this.h)
  30. return true;
  31. else
  32. return false;
  33. }
  34.  
  35. public int compare(Rectangle r){
  36. if (r.area()>this.area())
  37. return -1;
  38. else if(r.area()<this.area())
  39. return 1;
  40. else
  41. return 0;
  42. }
  43.  
  44. public static void sort(Rectangle[] rectArr)
  45. {
  46. Rectangle temp;
  47. for (int i=0; i<rectArr.length; i++)
  48. {
  49. for (int j=0; j<(rectArr.length-i-1); j++)
  50. {
  51. if (rectArr[j].compare(rectArr[j+1])>0)
  52. {
  53. temp=rectArr[j];
  54. rectArr[j]=rectArr[j+1];
  55. rectArr[j+1]=temp;
  56. }
  57. }
  58. }
  59. System.out.print("your rectangle by increasing area are: ");
  60. for(int i=0; i<rectArr.length; i++)
  61. System.out.print(rectArr[i]+" ");
  62. }
  63.  
  64. /*public Point maxIn(Rectangle[] arr){
  65. int counter=0;
  66. Rectangle []arr1= new Rectangle[5];
  67. arr1[0]=new Rectangle(5,5);
  68. arr1[1]=new Rectangle(1, 3);
  69. arr1[2]=new Rectangle(7, 2);
  70. arr1[3]=new Rectangle(1.7, 7.8);
  71. arr1[4]=new Rectangle(2.4, 4.6);
  72. if (p.contains(arr[0]))
  73. counter++;
  74. return ;*/
  75. }
  76.  
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement