Need a unique gift idea?
A Pastebin account makes a great Christmas gift
SHARE
TWEET

Untitled

a guest Nov 17th, 2018 94 Never
Upgrade to PRO!
ENDING IN00days00hours00mins00secs
 
  1. import java.awt.Rectangle;
  2. import java.awt.Point;
  3.  
  4. public class HW3
  5. {
  6.  
  7.    public static void main(String[] args)
  8.    {
  9.         //Creates a rectangle with top left hand point at location 0,0.
  10.         // and width=100 and height=200
  11.         Rectangle box = new Rectangle(0,0,100,200);
  12.          
  13.         //TASK #1 - Object as an input parameter
  14.         System.out.println("Task #1, AREA is: " + getArea(box) );
  15.      
  16.         //TASK #2 - Object as a return type
  17.         Point[] pts = getPoints(box);
  18.         if (pts != null)
  19.         {
  20.           System.out.println("Task #2, Lower Left Point is: " + pts[0] );
  21.           System.out.println("Task #2, Top Left Point is: " + pts[1] );
  22.           System.out.println("Task #2, Top Right Point is: " + pts[2] );
  23.           System.out.println("Task #2, Lower Right Point is: " + pts[3] );
  24.         }
  25.         else
  26.           System.out.println("Task #2, the Rectangle points are unknown");
  27.        
  28.         //Task #3 - Mutable Object
  29.         int old_width = box.width;
  30.         int old_height = box.height;
  31.         shrink(box);
  32.         System.out.println("Task #3, width changed from " + old_width + " to " + box.width);
  33.         System.out.println("Task #3, height changed from " + old_height + " to " + box.height);
  34.        
  35.         //Task #4 - Aliasing
  36.         Rectangle new_box = copy_and_shrink(box);
  37.         System.out.println("Task #4, the area of the old box is: " + getArea(box) );
  38.         System.out.println("Task #4, the area of the new box is: "+ getArea(new_box) );
  39.        
  40.    }
  41.  
  42.    //TASK #1 - Object as an input parameter
  43.    //        - Complete the method to calculate the area of a rectangle.
  44.    public static double getArea(Rectangle rect)
  45.    {
  46.        //-----YOUR TASK #1 CODE GOES HERE!-----
  47.        double area = rect.getWidth() * rect.getHeight();
  48.        return area;  //Do not forget to change the return value
  49.    }
  50.    
  51.    //TASK #2 - Object as a return type
  52.    //        - Complete the method to return all four points that define the rectangle
  53.    public static Point[] getPoints(Rectangle rect)
  54.    {
  55.     Point[] pts = new Point[4];
  56.     pts[0] = new Point(rect.x,rect.y - rect.height); // Assigning the first point (lower left point pt0)
  57.     pts[1] = new Point(rect.x, rect.y);
  58.     pts[2] = new Point(rect.x + rect.width, rect.y);
  59.     pts[3] = new Point(rect.x + rect.width, rect.y - rect.height);
  60.     //-----YOUR TASK #2 CODE GOES HERE!-----
  61.      return pts; //Do not forget to change the return value
  62.    }
  63.    
  64.    //TASK #3 - Mutable Object  (notice that we do not need to return the object)
  65.    public static void shrink(Rectangle rect)
  66.    {
  67.        //-----YOUR TASK #3 CODE GOES HERE!-----      
  68.        rect.width = (rect.width/2);
  69.        rect.height = (rect.height/2);
  70.        //Notice - we do not have to return anything!
  71.    }
  72.    
  73.    //TASK #4 - Object Aliasing
  74.    public static Rectangle copy_and_shrink(Rectangle original_rectangle)
  75.    {
  76.       Rectangle new_rectangle = new Rectangle(original_rectangle);
  77.       //-----YOUR TASK #4 CODE GOES HERE!-----
  78.       new_rectangle.width = original_rectangle.width/2;
  79.       new_rectangle.height = original_rectangle.height/2;
  80.       return new_rectangle; //Do not forget to change the return value
  81.    }
  82.    
  83. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top