Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Console;
- public class ColorRectangle extends Color implements
- Comparable<Object>{
- private int ix1,iy1,ix2,iy2;
- public ColorRectangle(){
- super(Console.readLong("Enter the color"));
- this.ix1=Console.readInt("Enter x1:");
- this.iy1 = Console.readInt("Enter y1:");
- this.ix2 = Console.readInt("Enter x2:");
- this.iy2 = Console.readInt("Enter y2:");
- }
- public ColorRectangle(int x1,int y1,int x2,int y2)
- {
- this.ix1 = x1;
- this.iy1 = x2;
- this.ix2 = y1;
- this.iy2 = y1;
- }
- public int getIX1() { return ix1; }
- public int getIY1() { return iy1; }
- public int getIX2() { return ix2; }
- public int getIY2() { return iy2; }
- public int calcArea()
- {
- return Math.abs((ix2-ix1)*(iy2-iy1));
- }
- public int compareTo(Object r)
- {
- if(this.calcArea()<((ColorRectangle)r).calcArea())
- {
- return -1;
- }
- if(this.calcArea()>((ColorRectangle)r).calcArea())
- {
- return 1;
- }
- return 0;
- }
- public String toString()
- {
- return "x1:" + ix1 + " y1:" + iy1 + " x2:" + ix2 + " y2:" + iy2 + super.toString();
- }
- public boolean equals(ColorRectangle R)
- {
- return (this.calcArea()==R.calcArea());
- }
- public void translateX(int iPoints)
- {
- ix1+=iPoints;
- ix2+=iPoints;
- }
- public void translateY(int iPoints)
- {
- iy1+=iPoints;
- iy2+=iPoints;
- }
- public void translateXY(int iPoints)
- {
- translateX(iPoints);
- translateY(iPoints);
- }
- public boolean isInside(int ptX,int ptY)
- {
- return(ix1 < ptX && ix2>ptY && iy1<ptY && iy2 > ptY);
- }
- public ColorRectangle unionRect(ColorRectangle R)
- {
- int x = ix1<R.ix1? ix1 : R.ix1;
- int y = iy1<R.iy1? iy1 : R.iy1;
- int x2 = ix2>R.ix2? ix2 : R.ix2;
- int y2 = iy2>R.iy2? iy2 : R.iy2;
- return new ColorRectangle(x,y,x2,y2);
- }
- public ColorRectangle intersectionRect(ColorRectangle R)
- {
- int x = ix1>R.ix1? ix1 : R.ix1;
- int y = iy1>R.iy1? iy1 : R.iy1;
- int x2 = ix2<R.ix2? ix2 : R.ix2;
- int y2 = iy2<R.iy2? iy2 : R.iy2;
- return new ColorRectangle(x,y,x2,y2);
- }
- public static void main(String[] args)
- {
- ColorRectangle r1 = new ColorRectangle();
- ColorRectangle r2 = new ColorRectangle();
- System.out.println("r1: " + r1.toString());
- System.out.println("r2: " + r2.toString());
- if(r1.equals(r2))
- {
- System.out.println("Areas are equal");
- }
- else
- System.out.println("Areas are not equal");
- ColorRectangle U = r1.unionRect(r2);
- System.out.println("r3: "+U.toString());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement