Advertisement
Guest User

Untitled

a guest
May 23rd, 2017
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.43 KB | None | 0 0
  1. *//can someone explain the point of passing a point object into a rectangle object.
  2. At first I thought passing the origin would somehow alter the width and height of the rectangle,
  3. 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?
  4. how does rectangle access its starting point?
  5. *//
  6.  
  7.  
  8. //point class
  9.  
  10.  
  11. public class Point {
  12.    
  13.     public int x = 0;
  14.     public int y = 0;
  15.     //constructor
  16.     public Point(int a, int b){
  17.         x = a;
  18.         y = b;
  19.     }
  20.    
  21.     public int getx(){
  22.         return x;
  23.     }
  24.  
  25. }
  26.  
  27.  
  28.  
  29. //rectangle class
  30.  
  31.  
  32. public class Rectangle {
  33.    
  34.     public int width = 0;
  35.     public int height = 0;
  36.     public Point origin;
  37.    
  38.     //four constructors
  39.    
  40.     public Rectangle() {
  41.         origin = new Point(0,0);
  42.     }
  43.    
  44.     public Rectangle (Point p){
  45.         origin = p;
  46.     }
  47.    
  48.     public Rectangle (int w, int h){
  49.         origin = new Point(0,0);
  50.         width = w;
  51.         height = h;
  52.     }
  53.    
  54.     public Rectangle(Point p, int w, int h){
  55.         origin = p;
  56.         width = w;
  57.         height = h;
  58.     }
  59.    
  60.     public void move(int x, int y){
  61.         origin.x = x;
  62.         origin.y = y;
  63.     }
  64.    
  65.     public int getArea(){
  66.         return width * height;
  67.     }
  68.  
  69. }
  70.  
  71.  
  72. //running it
  73.  
  74.  
  75. public class RectDemo {
  76.  
  77.     public static void main(String[] args){
  78.        
  79.         Point pobj1 = new Point(5,76);
  80.        
  81.         Rectangle rect2 = new Rectangle(pobj1);
  82.        
  83.         System.out.println(rect2.width);
  84.        
  85.         System.out.println(rect2.getArea());
  86.        
  87.         System.out.println(rect2.origin);
  88.     }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement