Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //trying to understand what the point of this point object is when you pass it as
- //an argument in creating a new rectangle object
- //I passed pobj1 in and it says its x and ys are still 0. but pobj1's x, and ys are NOT 0.
- 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());
- }
- }
- //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;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement