Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- interface Shape
- {
- Shape setWidth(int value);
- Shape setHeight(int value);
- int getWidth();
- int getHeight();
- }
- class Rect implements Shape {
- private final int width;
- private final int height;
- public Rect(int width, int height) {
- this.width = width;
- this.height = height;
- }
- @Override public Shape setWidth(int value) {
- return new Rect(value, height);
- }
- @Override public Shape setHeight(int value) {
- return new Rect(width, value);
- }
- @Override public int getWidth() {
- return width;
- }
- @Override public int getHeight() {
- return height;
- }
- }
- class Square implements Shape {
- private final int size;
- public Square(int size) {
- this.size = size;
- }
- @Override public Shape setWidth(int value) {
- if (value != size){
- return new Rect(value, size);
- }
- return this;
- }
- @Override public Shape setHeight(int value) {
- if (value != size){
- return new Rect(size, value);
- }
- return this;
- }
- @Override public int getWidth() {
- return size;
- }
- @Override public int getHeight() {
- return size;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement