Advertisement
Guest User

Untitled

a guest
Oct 21st, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.76 KB | None | 0 0
  1. public class Rectangle {
  2.     int x, y;
  3.  
  4.     /* Create a new rectangle with the default size.
  5.      * This is a constructor that takes no arguments.
  6.      */
  7.     public Rectangle() {
  8.         x = 10;
  9.         y = 10;
  10.     }
  11.  
  12.     /* Create a new rectangle with the given size.
  13.      * `this` keyword access the class's `x` and `y` specifically,
  14.      * otherwise it would refer to the local ones.
  15.      * This is also a constructor! It just takes more arguments.
  16.      */
  17.     public Rectangle(int x, int y) {
  18.         this.x = x;
  19.         this.y = y;
  20.     }
  21.  
  22.     /* Works on any Rectangle class.
  23.      * This is a method given to an object.
  24.      * Example:
  25.      * ```
  26.      * Rectangle r = new Rectangle(10, 10);
  27.      * int area = r.getArea();
  28.      * System.out.println(area); // 100
  29.      * ```
  30.      */
  31.     public int getArea() {
  32.         return x * y;
  33.     }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement