Last active
          March 12, 2020 18:47 
        
      - 
      
 - 
        
Save Sam-Belliveau/7984cd1b085c9a06da9199ad7ad4ad35 to your computer and use it in GitHub Desktop.  
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | public class Rectangle{ | |
| private final double x, y; // center of rectangle | |
| private final double width; // width of rectangle | |
| private final double height; // height of rectangle | |
| // Constructor | |
| public Rectangle(double x, double y, double width, double height){ | |
| if(width < 0.0 || height < 0.0) { | |
| throw new IllegalArgumentException("diamentions of Rectangle must be positive!"); | |
| } | |
| this.x = x; | |
| this.y = y; | |
| this.width = width; | |
| this.height = height; | |
| } | |
| public double area() { | |
| return width * height; | |
| } | |
| public double perimeter() { | |
| return 2 * width + 2 * height; | |
| } | |
| public boolean intersects(Rectangle other) { | |
| boolean result = true; | |
| result &= Math.abs(this.x - other.x) < (this.width + other.width); | |
| result &= Math.abs(this.y - other.y) < (this.height + other.height); | |
| return result; | |
| } | |
| public boolean contains(Rectangle other) { | |
| boolean result = true; | |
| result &= Math.abs(this.x - other.x) < (this.width - other.width); | |
| result &= Math.abs(this.y - other.y) < (this.height - other.height); | |
| return result; | |
| } | |
| public void draw() { | |
| StdDraw.rectangle(x, y, width / 2.0, height / 2.0); | |
| } | |
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | public class TestClient { | |
| public static void main(String[] args) { | |
| int n = Integer.valueOf(args[0]); | |
| double min = Double.valueOf(args[1]); | |
| double max = Double.valueOf(args[2]); | |
| Rectangle[] rects = new Rectangle[n]; | |
| double totalArea = 0; | |
| double totalPerimeter = 0; | |
| for(int i = 0; i < n; ++i) { | |
| double x = Math.random(); | |
| double y = Math.random(); | |
| double width = Math.random() * (max - min) + min; | |
| double height = Math.random() * (max - min) + min; | |
| rects[i] = new Rectangle(x,y,width,height); | |
| rects[i].draw(); | |
| totalArea += rects[i].area(); | |
| totalPerimeter += rects[i].perimeter(); | |
| } | |
| totalArea /= n; | |
| totalPerimeter /= n; | |
| System.out.println("Average Area: " + totalArea); | |
| System.out.println("Average Perimeter: " + totalPerimeter); | |
| } | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment