Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package putin;
- public class rectangle {
- private int ix1, iy1, ix2, iy2;
- public rectangle(){}
- public rectangle(int x1, int y1,int x2, int y2) {
- this.ix1 = x1;
- this.iy2 = y2;
- this.ix2 = x2;
- this.iy2 = y2;
- }
- 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()<((rectangle)r).calcArea())
- return -1;
- if(this.calcArea()>((rectangle)r).calcArea())
- return 1;
- return 0;
- }
- public String toString(){
- return "x1:"+ix1+";y1:"+iy1+";x2:"+ix2+";y2:"+iy2;
- }
- public boolean equals(rectangle r){
- return this.calcArea()==r.calcArea();
- }
- public void translateX(int iPoints){
- ix1 = ix1 + iPoints;
- ix2 += iPoints;
- }
- public void translateY(int iPoints){
- iy1 = iy2 + iPoints;
- iy2 += iPoints;
- }
- public void translateXY(int iPoints){
- translateX (iPoints);
- translateY (iPoints);
- }
- public boolean isInside(int ptx, int pty){
- if(ix1<ptx && ix2>ptx && iy1 < pty && iy2>pty){
- return true;}
- else {
- return false; }
- }
- public rectangle unionRect(rectangle r){
- int x1 = Math.min(ix1,r.ix1);
- int y1 = Math.min(iy1,r.iy1);
- int x2 = Math.max(ix2,r.ix2);
- int y2 = Math.max(iy2,r.iy2);
- rectangle current = new rectangle (x1, y1, x2, y2);
- return current;
- }
- public static void main (String[] args){
- rectangle r1= new rectangle(5,5,12,12);
- rectangle r2 = new rectangle(4,4,2,2);
- System.out.println("r1:" + r1.toString());
- System.out.println("r2:" + r2.toString());
- }
- if(r1.equal(r2)){
- System.out.println("Areas are equal");
- }
- else {
- System.out.println("Areas aren't equal");
- }
- rectangle r3=r1.unionRect(r2);
- System.out.println("r3:"+r3.toString());
- }
- }
- RAW Paste Data
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement