Skip to content

Instantly share code, notes, and snippets.

@rslima
Created June 15, 2012 13:50
Show Gist options
  • Save rslima/2936568 to your computer and use it in GitHub Desktop.
Save rslima/2936568 to your computer and use it in GitHub Desktop.
Squares and Rectangles in Java
public class Rectangle {
private final int h;
private final int w;
public Rectangle(int height, int width) {
h = height;
w = width;
}
public int getH() {
return h;
}
public int getW() {
return w;
}
public Rectangle setH(int height) {
return new Rectangle(height,w);
}
public Rectangle setW(int width) {
return new Rectangle(h,width);
}
public int perimeter() {
return 2 * (h + w);
}
public int area() {
return h * w;
}
}
public class Square extends Rectangle {
public Square(int side) {
super(side,side);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment