Advertisement
AlexanderF

rectangle

Nov 14th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.72 KB | None | 0 0
  1. package be.kdg.start.geometry;
  2.  
  3. public class Rectangle {
  4.     private int x;
  5.     private int y;
  6.     private int height;
  7.     private int width;
  8.     public String description = "Rectangle";
  9.  
  10.     public Rectangle() {
  11.         this(5, 5);
  12.     }
  13.  
  14.     public Rectangle(int height, int width) {
  15.         this(1, 1, height, width);
  16.     }
  17.  
  18.     public Rectangle(int x, int y, int height, int width) {
  19.         this.x = x;
  20.         this.y = y;
  21.         this.height = height;
  22.         this.width = width;
  23.     }
  24.  
  25.     public Rectangle(Rectangle rectangleToCopy){
  26.         this(rectangleToCopy.getX(), rectangleToCopy.getY()
  27.                 , rectangleToCopy.getHeight(), rectangleToCopy.getWidth());
  28.     }
  29.  
  30.     public int getX() {
  31.         return x;
  32.     }
  33.  
  34.     public int getY() {
  35.         return y;
  36.     }
  37.  
  38.     public int getHeight() {
  39.         return height;
  40.     }
  41.  
  42.     public void setHeight(int height) {
  43.         this.height = height;
  44.     }
  45.  
  46.     public int getWidth() {
  47.         return width;
  48.     }
  49.  
  50.     public void setWidth(int width) {
  51.         this.width = width;
  52.     }
  53.  
  54.     public void setPosition(int x, int y){
  55.         this.x = x;
  56.         this.y = y;
  57.     }
  58.  
  59.     public int getArea(){
  60.         return width * height;
  61.     }
  62.  
  63.     public int getPerimeter(){
  64.         return (2*width) + (2*height);
  65.     }
  66.  
  67.     public void grow(int growOfWidth, int growOfHeight){
  68.         width += growOfWidth;
  69.         height += growOfHeight;
  70.     }
  71.  
  72.     public void grow(int growSize){
  73.         this.grow(growSize, growSize);
  74.     }
  75.  
  76.     @Override
  77.     public String toString() {
  78.         return String.format("Rectangle at (%d, %d), Width: %d, Height: %d"
  79.                 , getX(), getY(), getWidth(), getHeight() );
  80.     }
  81.  
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement