Advertisement
fursty

OOP 2 Rectangle

Mar 2nd, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.23 KB | None | 0 0
  1. package putin;
  2.  
  3. public class rectangle {
  4. private int ix1, iy1, ix2, iy2;
  5. public rectangle(){}
  6. public rectangle(int x1, int y1,int x2, int y2) {
  7. this.ix1 = x1;
  8. this.iy2 = y2;
  9. this.ix2 = x2;
  10. this.iy2 = y2;
  11. }
  12. public int getix1(){
  13. return ix1;
  14. }
  15. public int getiy1(){
  16. return iy1;
  17. }
  18. public int getix2(){
  19. return ix2;
  20. }
  21. public int getiy2(){
  22. return iy2;
  23. }
  24. public int calcArea(){
  25. return Math.abs((ix2-ix1)*(iy2-iy1));
  26. }
  27.  
  28. public int compareTo(Object r){
  29. if(this.calcArea()<((rectangle)r).calcArea())
  30. return -1;
  31. if(this.calcArea()>((rectangle)r).calcArea())
  32. return 1;
  33. return 0;
  34. }
  35. public String toString(){
  36. return "x1:"+ix1+";y1:"+iy1+";x2:"+ix2+";y2:"+iy2;
  37. }
  38. public boolean equals(rectangle r){
  39. return this.calcArea()==r.calcArea();
  40. }
  41. public void translateX(int iPoints){
  42. ix1 = ix1 + iPoints;
  43. ix2 += iPoints;
  44. }
  45. public void translateY(int iPoints){
  46. iy1 = iy2 + iPoints;
  47. iy2 += iPoints;
  48. }
  49. public void translateXY(int iPoints){
  50. translateX (iPoints);
  51. translateY (iPoints);
  52. }
  53. public boolean isInside(int ptx, int pty){
  54. if(ix1<ptx && ix2>ptx && iy1 < pty && iy2>pty){
  55. return true;}
  56. else {
  57. return false; }
  58. }
  59. public rectangle unionRect(rectangle r){
  60. int x1 = Math.min(ix1,r.ix1);
  61. int y1 = Math.min(iy1,r.iy1);
  62. int x2 = Math.max(ix2,r.ix2);
  63. int y2 = Math.max(iy2,r.iy2);
  64. rectangle current = new rectangle (x1, y1, x2, y2);
  65. return current;
  66. }
  67. public static void main (String[] args){
  68. rectangle r1= new rectangle(5,5,12,12);
  69. rectangle r2 = new rectangle(4,4,2,2);
  70. System.out.println("r1:" + r1.toString());
  71. System.out.println("r2:" + r2.toString());
  72. }
  73. if(r1.equal(r2)){
  74. System.out.println("Areas are equal");
  75. }
  76. else {
  77. System.out.println("Areas aren't equal");
  78. }
  79. rectangle r3=r1.unionRect(r2);
  80. System.out.println("r3:"+r3.toString());
  81. }
  82. }
  83. RAW Paste Data
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement