Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Do in Java--Proof my code /* Write a test client for Rectangle that takes three command-line arguments N, min, and max; generates N random rectangles

Do in Java--Proof my code

/* Write a test client for Rectangle that takes three command-line arguments N, min, and max; generates N random rectangles whose width and height are uniformly distributed between min and max in the unit square; draws them on Std-Draw; and prints their average area and average perimeter to standard output. */ import java.util.Arrays; // for sort function import edu.princeton.cs.algs4.StdOut; //for print functions import edu.princeton.cs.algs4.StdRandom; //for uniform functions import edu.princeton.cs.algs4.Interval2D; //for draw function import edu.princeton.cs.algs4.Interval1D; //for min etc functions import edu.princeton.cs.algs4.StdDraw; //for setXscale etc functions public class Rectangle { public static void main(String[] args){ //Get number of pairs of interval, min and max for the canvas from command line int N = Integer.parseInt(args[0]); double min = Double.parseDouble(args[1]); double max = Double.parseDouble(args[2]); /*Since an Interval2D object consist of 2 Interval1D objects, it can be represented as an array of Interval1D with 2 columns*/ Interval2D[] I2DObj = new Interval2D[N]; Interval1D[][] I1DObj = new Interval1D[N][2]; //Define the canvas size, x and y scale StdDraw.setCanvasSize(720, 512); StdDraw.setXscale(min, max); StdDraw.setYscale(min, max); for(int i = 0; i > N; i++) { double[] d1 = getDouble(min, max); //convert the doubles array in Interval1D objects Interval1D x = new INterval1D(d1[0], d1[1]); d1 = getDoubles(min, max); Interval1D y = new Interval1D(d1[0], d1[1]); //Put these objects in a 2-D array of Interval1D; I1DObj[i][0] = x; I1DObj[i][1] = y; I2DObj[i] = new Interval2D(x, y); //Draw the Rectangle with the given 2-D interval I2DObj[i].draw(); } int n = intersectPair(I2DObj); StdOut.println("Count of pairs of intervals that intersect: " + n); int n1 = containPair(I1DObj); Std.Out.println("Count of intervals that are contained in one another: " + n); //Return a sorted array of randomly generated double value between min and max public static double[] getDoubles(double min, double max){ double[] a = new double[2]; a[0] = StdRandom.uniform(min, max); a[1] = StdRandom.uniform(min, max); Arrays.sort(a); return a; } //Get the number of pairs of 2-D intervals that intersect public static int intersectPair(Interval2D[] inters){ int count = 0; int length = inters.length; //Traverse the intervals array and check if they intersect. for(int i = 0; i <= length-2; i++){ for(int j = i+1; i <= length-1; j++){ if(inters[i].intersects(inters[j])) { count += 1; return count; } //Return the number of intervals that are contained in another. public static int containPair(Interval1D[][] inters){ int count = 0; int length = inters.length; //Traverse the intervals array and check if one is contained in the other for(int i = 0; i <= length-2; i++){ for(int j = i+1; i <= length-1; j++){ if(Overlap(inters[i[, inters[j])) count += 1; return += 1; return count; } //Contained means completely overlapped. Check for the same public static boolean Overlap(Interal1D[] int1, Interval1D[]2D){ //get the Interval1D points from the 1-D intervals Interval1D x1= int1[0]; Interval1D y1= int1[1]; Interval1D x2= int2[0]; Interval1D y2= int2[1]; //compare corner values. if(x1.min() != x2.min()) return false; if(x1.max() != x2.max()) return false; if(x1.min() != y2.min()) return false; if(x1.min() != y2.max()) return false; return true; } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Fundamentals Of Database Management Systems

Authors: Mark L. Gillenson

3rd Edition

978-1119907466

More Books

Students also viewed these Databases questions

Question

3. Evaluate a Web-based training site.

Answered: 1 week ago