hi all

im writing a function for rectangle where it takes a rectangle and return a rectangle (same top-left vertex) but reduces the width and height by a,b respectively.....

i have done this

public int shrink(int a, int b)
	{
		return (this.width -= a)&&(this.height -= b);
	}

but i get error message saying u cant use && for int

is there another way to write it???

thankyou

your not thinking OO.

your code is doing a boolean comparison on two integers, this is what you can't do. you can in C/C++. But that isn't what you want anyway.

Ok, what I think you want to do is return a rectangle? this is how I would do it:

public Rectangle shrink(int width, int height){
   return new Rectangle(this.width - width, this.height - height);
}

which returns a new rectangle instance with the new shrunken dimensions... if instead you want to shrink the current dimensions... then try this:

public boolean shrink(int width, int height){
   if(this.width > width && this.height > height){
      this.width -= width;
      this.height -= height;
      return true;
   }
   return false;
}

which shrinks the rectangle instance and returns true, unless the shrinkage is greater than the original dimensions :)

hi thanx for ur help

i used the first one but the thing is that i have another class Point2D which rectangle uses it now in if i want the hight and width to be reduced can i do this:

public Point2D shrink(int a, int b)
	{
   		return new Point2D(this.width - a, this.height - b);
	}

thnx

This article has been dead for over six months. Start a new discussion instead.