Question: Please write in JAVA import static org.junit.Assert.*; import org.junit.After; import org.junit.Before; import org.junit.Test; import java.math.BigInteger; import java.util.*; import java.util.zip.CRC32; public class ManhattanTest { @Test public

Please write in JAVAPlease write in JAVA import static org.junit.Assert.*; import org.junit.After; import org.junit.Before; import

import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.math.BigInteger;
import java.util.*;
import java.util.zip.CRC32;
public class ManhattanTest {
@Test public void testTotalArea() {
/* Explicit test cases */
int[] s1 = {2, 6, 9, 12, 15};
int[] e1 = {3, 8, 10, 14, 20};
int[] h1 = {3, 3, 4, 3, 2};
assertEquals(29, Manhattan.totalArea(s1, e1, h1));
int[] s2 = {3, 7, 8, 17, 24, 26, 27};
int[] e2 = {5, 18, 18, 28, 37, 29, 41};
int[] h2 = {1, 3, 2, 2, 3, 2, 1};
assertEquals(90, Manhattan.totalArea(s2, e2, h2));
/* Pseudorandom fuzz tester */
CRC32 check = new CRC32();
Random rng = new Random(777);
for(int i = 2; i
int n = rng.nextInt(3 * i) + 1;
int[] s = new int[n];
int[] e = new int[n];
int[] h = new int[n];
for(int j = 0; j
s[j] = rng.nextInt(4 * n);
}
Arrays.sort(s);
for(int j = 0; j
int w = 1 + rng.nextInt(2 * n);
e[j] = s[j] + w;
if(j > 0 && s[j-1] == s[j]) { e[j] = Math.max(e[j], e[j-1] + 1); }
h[j] = 1 + (j / w) + rng.nextInt(3);
}
int r = Manhattan.totalArea(s, e, h);
check.update(r);
}
assertEquals(2174298203L, check.getValue());
}
}

make sure the code can pass the test, will give up the vote. Thanks

Lab 25: Manhattan Skyline JUnit: ManhattanTest.java Manhattan skyline problem is a well-known exercise in computational geometry, with room for many educational and interesting variations. Its setup makes for a good illustration of the sweep line algorithm used to efficiently solve this problem. This general technique of sweep line will then solve many other problems that take place on the two-dimensional plane. In the basic version of this problem, a number of tall buildings (perhaps built in the heyday of some midwestern city on a flat plain) are seen from a distance against the horizon far away. Our vantage point is located far enough so that each building is seen as a two-dimensional silhouette rectangle. From our point of view towards the horizon, each individual silhouette has starting and ending x- coordinates s and e that of course satisfy s events = new ArrayListoO; with these "enter" and "exit" events encoded so that the building b entering the view is encoded as -1-b, and the building b exiting is encoded as b-1. (We need to do this encoding of events into integers in this tricky way so that each even is encoded to something nonzero, because there is no way to tell zero and minus zero apart!) Add these 2n event encodings into events, and sort them with a custom Comparator local strategy class whose compareTo method compares the events a and b by first decoding them into starting times (taken from array s) or ending times (taken from array e) that are then compared to render the final verdict. The events sorted in this manner are ready to be processed in the order that they occur sweeping from left to right, updating the tally of the area. To keep track of the buildings that are currently in your active view (the so-called active set of the sweep line algorithm), define another local variable Set active = new HashSet(); that contains the buildings that are currently in the active view. Loop through the sorted events, also keeping track of the x-coordinate of the event processed during the previous round. To process an event, first compute its horizontal distance from the previous event. Multiply that distance by the height of the active building that is currently tallest, and add that product to the total. (Sometimes several events take place a the same x-coordinate, but since the distance between those events is zero, such redundant product terms automatically vanish.) Next, update the active set by adding or removing the building that enters or exits the view, and move on to the next round. Once all buildings have entered and exited the active view, the method can return the accumulated total. S h Expected result {1, 4, 11} {5, 8, 13] {2, 1, 2} 15 {2, 6, 9, 12, 15} {3, 8, 10, 14, 20} {3, 3, 4, 3, 2} 29 113 {0, 9, 11, 13, 13, 18, 31, 32, 35} {4, 23, 21, 23, 24, 21, 38, 50, 45} {3, 3, 1, 1, 2, 2, 3, 3, 1}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!