## Object methods

No. 81

### Modeling geometry objects: Rectangles

Q:

We want to represent rectangles being defined by width and height to allow for the subsequently demonstrated operations:

final Rectangle r = new Rectangle(8, 5);

System.out.println("Perimeter:" + r.getPerimeter());
System.out.println("Area:" + r.getArea());

r.setWidth(4);
r.setHeight(7);

System.out.println("Perimeter:" + r.getPerimeter());
System.out.println("Area:" + r.getArea());
 Creating an instance of class Rectangle by calling a non-default constructor which allows for providing width (8) and height (5). Returning the rectangle's perimeter, Returning the rectangle's area. Setting with and height to new values. Write (possibly) changed perimeter and area values.

You may start from the following Rectangle class dummy implementation:

/**
* Representing rectangular shapes.
*
*/
public class Rectangle {

/**
*
* @param width The rectangle's width
* @param heigth The rectangle's height
*/
public Rectangle (double width, double heigth) {
//TODO
}
/**
* @return The rectangle's area.
*/
public double getArea() {
return 0; // TODO
}

/**
* @return The rectangle's perimeter.
*/
public double getPerimeter() {
return 0; // TODO
}

/**
* @return The rectangle's width.
*/
public double getWidth() {
return 0; // TODO
}
/**
* @param width The rectangle's new width
*/
public void setWidth(double width) {
// TODO
}

/**
* @return The rectangle's height.
*/
public double getHeight() {
return 0; // TODO
}

/**
* @param height The rectangle's new height
*/
public void setHeight(double height) {
// TODO
}
}

A:

First we define two instance variables width and height representing a Rectangle's corresponding two parameters width and height:

public class Rectangle {

// Instance variables representing a rectangle's parameters
private double width, height;
...
}

Next we allow for changing these two parameters:

public class Rectangle {

// Instance variables representing a rectangle's parameters
private double width, height;

...
/**
* @param width Changing the rectangle's width
*/
public void setWidth(double w) {
width = w;
}

/**
* @param width Changing the rectangle's height
*/
public void setHeight(double height) {
this.height = height;
}
...
}

Note the subtle implementation difference between setWidth(...) and setHeight(...):

setWidth(double w)

We use the formal parameter name w. Its name does not conflict with the instance variable name width being defined at class level. We can simply assign this value to our corresponding instance variable using width = w;.

setHeight(double height)

The method's formal parameter height shadows the instance variable's name being defined at class level. We need the this keyword in this.height = height to resolve the ambiguity.

Both ways are perfectly legal. The complete implementation including all remaining methods reads:

/**
* Representing rectangular shapes.
*
*/
public class Rectangle {

// Instance variables representing a rectangle's parameters
private double width, height;

/**
*
* @param width The rectangle's width
* @param heigth The rectangle's height
*/
public Rectangle (double width, double height) {
setWidth(width);
setHeight(height);
}
/**
* @return The rectangle's area.
*/
public double getArea() {
return width * height;
}

/**
* @return The rectangle's perimeter.
*/
public double getPerimeter() {
return 2 * (width + height);
}

/**
* @return The rectangle's width.
*/
public double getWidth() {
return width;
}
/**
* @param width The rectangle's new width
*/
public void setWidth(double w) {
width = w;
}

/**
* @return The rectangle's height.
*/
public double getHeight() {
return height;
}

/**
* @param width The rectangle's new height
*/
public void setHeight(double height) {
this.height = height;
}
}

No. 82