Advertisement
Guest User

Untitled

a guest
May 23rd, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.25 KB | None | 0 0
  1. //trying to understand what the point of this point object is when you pass it as
  2. //an argument in creating a new rectangle object
  3. //I passed pobj1 in and it says its x and ys are still 0. but pobj1's x, and ys are NOT 0.
  4.  
  5.  
  6.  
  7. public class RectDemo {
  8.  
  9.     public static void main(String[] args){
  10.        
  11.         Point pobj1 = new Point(5,76);
  12.        
  13.         Rectangle rect2 = new Rectangle(pobj1);
  14.        
  15.         System.out.println(rect2.width);
  16.        
  17.         System.out.println(rect2.getArea());
  18.        
  19.     }
  20. }
  21.  
  22.  
  23. //point class
  24.  
  25.  
  26. public class Point {
  27.    
  28.     public int x = 0;
  29.     public int y = 0;
  30.     //constructor
  31.     public Point(int a, int b){
  32.         x = a;
  33.         y = b;
  34.     }
  35.    
  36.     public int getx(){
  37.         return x;
  38.     }
  39.  
  40. }
  41.  
  42.  
  43. //rectangle class
  44.  
  45.  
  46. public class Rectangle {
  47.    
  48.     public int width = 0;
  49.     public int height = 0;
  50.     public Point origin;
  51.    
  52.     //four constructors
  53.    
  54.     public Rectangle() {
  55.         origin = new Point(0,0);
  56.     }
  57.    
  58.     public Rectangle (Point p){
  59.         origin = p;
  60.     }
  61.    
  62.     public Rectangle (int w, int h){
  63.         origin = new Point(0,0);
  64.         width = w;
  65.         height = h;
  66.     }
  67.    
  68.     public Rectangle(Point p, int w, int h){
  69.         origin = p;
  70.         width = w;
  71.         height = h;
  72.     }
  73.    
  74.     public void move(int x, int y){
  75.         origin.x = x;
  76.         origin.y = y;
  77.     }
  78.    
  79.     public int getArea(){
  80.         return width * height;
  81.     }
  82.  
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement