Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- *//can someone explain the point of passing a point object into a rectangle object.
- At first I thought passing the origin would somehow alter the width and height of the rectangle,
- but it seems that is not the case because i tested it. the point must be there to show the starting point of the object?
- how does rectangle access its starting point?
- *//
- //point class
- public class Point {
- public int x = 0;
- public int y = 0;
- //constructor
- public Point(int a, int b){
- x = a;
- y = b;
- }
- public int getx(){
- return x;
- }
- }
- //rectangle class
- public class Rectangle {
- public int width = 0;
- public int height = 0;
- public Point origin;
- //four constructors
- public Rectangle() {
- origin = new Point(0,0);
- }
- public Rectangle (Point p){
- origin = p;
- }
- public Rectangle (int w, int h){
- origin = new Point(0,0);
- width = w;
- height = h;
- }
- public Rectangle(Point p, int w, int h){
- origin = p;
- width = w;
- height = h;
- }
- public void move(int x, int y){
- origin.x = x;
- origin.y = y;
- }
- public int getArea(){
- return width * height;
- }
- }
- //running it
- public class RectDemo {
- public static void main(String[] args){
- Point pobj1 = new Point(5,76);
- Rectangle rect2 = new Rectangle(pobj1);
- System.out.println(rect2.width);
- System.out.println(rect2.getArea());
- System.out.println(rect2.origin);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement