Advertisement
fursty

OOP2 Console

Mar 2nd, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.36 KB | None | 0 0
  1. package Console;
  2.  
  3. public class ColorRectangle extends Color implements
  4. Comparable<Object>{
  5.  
  6. private int ix1,iy1,ix2,iy2;
  7. public ColorRectangle(){
  8.     super(Console.readLong("Enter the color"));
  9.     this.ix1=Console.readInt("Enter x1:");
  10.     this.iy1 = Console.readInt("Enter y1:");
  11.     this.ix2 = Console.readInt("Enter x2:");
  12.     this.iy2 = Console.readInt("Enter y2:");
  13. }
  14.  
  15. public ColorRectangle(int x1,int y1,int x2,int y2)
  16. {
  17.  
  18. this.ix1 = x1;
  19. this.iy1 = x2;
  20. this.ix2 = y1;
  21. this.iy2 = y1;
  22.  
  23. }
  24.  
  25. public int getIX1() { return ix1; }
  26. public int getIY1() { return iy1; }
  27. public int getIX2() { return ix2; }
  28. public int getIY2() { return iy2; }
  29. public int calcArea()
  30.  
  31. {
  32.  
  33. return Math.abs((ix2-ix1)*(iy2-iy1));
  34.  
  35. }
  36.  
  37. public int compareTo(Object r)
  38. {
  39.  
  40. if(this.calcArea()<((ColorRectangle)r).calcArea())
  41.  
  42. {
  43.  
  44. return -1;
  45.  
  46. }
  47.  
  48. if(this.calcArea()>((ColorRectangle)r).calcArea())
  49.  
  50. {
  51. return 1;
  52. }
  53. return 0;
  54. }
  55. public String toString()
  56. {
  57. return "x1:" + ix1 + " y1:" + iy1 + " x2:" + ix2 + " y2:" + iy2 + super.toString();
  58. }
  59. public boolean equals(ColorRectangle R)
  60. {
  61. return (this.calcArea()==R.calcArea());
  62. }
  63. public void translateX(int iPoints)
  64. {
  65. ix1+=iPoints;
  66. ix2+=iPoints;
  67. }
  68. public void translateY(int iPoints)
  69. {
  70. iy1+=iPoints;
  71. iy2+=iPoints;
  72.  
  73. }
  74.  
  75. public void translateXY(int iPoints)
  76. {
  77. translateX(iPoints);
  78. translateY(iPoints);
  79. }
  80. public boolean isInside(int ptX,int ptY)
  81. {
  82. return(ix1 < ptX && ix2>ptY && iy1<ptY && iy2 > ptY);
  83. }
  84. public ColorRectangle unionRect(ColorRectangle R)
  85. {
  86. int x = ix1<R.ix1? ix1 : R.ix1;
  87. int y = iy1<R.iy1? iy1 : R.iy1;
  88. int x2 = ix2>R.ix2? ix2 : R.ix2;
  89. int y2 = iy2>R.iy2? iy2 : R.iy2;
  90. return new ColorRectangle(x,y,x2,y2);
  91. }
  92. public ColorRectangle intersectionRect(ColorRectangle R)
  93. {
  94. int x = ix1>R.ix1? ix1 : R.ix1;
  95. int y = iy1>R.iy1? iy1 : R.iy1;
  96. int x2 = ix2<R.ix2? ix2 : R.ix2;
  97. int y2 = iy2<R.iy2? iy2 : R.iy2;
  98. return new ColorRectangle(x,y,x2,y2);
  99.  
  100. }
  101.  
  102. public static void main(String[] args)
  103. {
  104.  
  105. ColorRectangle r1 = new ColorRectangle();
  106. ColorRectangle r2 = new ColorRectangle();
  107. System.out.println("r1: " + r1.toString());
  108. System.out.println("r2: " + r2.toString());
  109.  
  110. if(r1.equals(r2))
  111. {
  112. System.out.println("Areas are equal");
  113. }
  114. else
  115.  
  116. System.out.println("Areas are not equal");
  117. ColorRectangle U = r1.unionRect(r2);
  118. System.out.println("r3: "+U.toString());
  119. }
  120.  
  121.  
  122.    
  123.  
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement