Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Rectangle {
- int x, y;
- /* Create a new rectangle with the default size.
- * This is a constructor that takes no arguments.
- */
- public Rectangle() {
- x = 10;
- y = 10;
- }
- /* Create a new rectangle with the given size.
- * `this` keyword access the class's `x` and `y` specifically,
- * otherwise it would refer to the local ones.
- * This is also a constructor! It just takes more arguments.
- */
- public Rectangle(int x, int y) {
- this.x = x;
- this.y = y;
- }
- /* Works on any Rectangle class.
- * This is a method given to an object.
- * Example:
- * ```
- * Rectangle r = new Rectangle(10, 10);
- * int area = r.getArea();
- * System.out.println(area); // 100
- * ```
- */
- public int getArea() {
- return x * y;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement