Need to work this out in Java. On the next page, there are 4 grid-based maps eac
ID: 3782802 • Letter: N
Question
Need to work this out in Java.
On the next page, there are 4 grid-based maps each with 5 small towns shown as red squares. We would like to write a program that determines the "optimal" grid location to build a hospital so that it lies at a distance that minimizes the travel from each town. That is ... the furthest distance that a town resident has to travel in order to reach the hospital is kept to a minimum. The answer is shown as a green circle for each of the 4 maps.
Write a class called HospitalBuilderProgram that computes the green circle location for each map. The program must read in a 3-dimensional array of town locations as shown below:
private static byte[][][] maps = {{{2, 2}, {2, 8}, {5, 15}, {19, 1}, {17, 17}}, {{1, 1}, {7, 19}, {13, 19}, {19, 7}, {19, 13}}, {{0, 19}, {2, 19}, {4, 19}, {6, 19}, {18, 19}}, {{7, 19}, {13, 19}, {19, 19}, {19, 13}, {19, 7}}};
The array has 4 maps. Each map has 5 towns. Each town has an (x,y) coordinate that matches the 4 maps shown earlier. Your program must loop through these towns and compute the answer to the problem. The easiest way is to compute the distance to each town from every one of the 20x20 grid locations and keep the best one as the answer. The distance between points (x1, y1) and (x2, y2) can be computed as d = ? + ?. The program should print out the (x, y) answer for each of the 4 maps.
There is a mistake that the location of town (at the top right corner of the first map) is (17,18) in the map but the code given says it is (17,17).
Any help would be appreciated.
COM 106 M1-W2017. O file://c/Users victor 0323/Downloads/coMP1106 A1 W2017-2.pdf (2) Hospital Builder Program On the next page, there are 4 grid-based maps each with 5 small towns shown as red squares. We would like to write a program that determines the "optimal grid location to build a hospital so that it lies at a distance that minimizes the travel from each town. That is the furthest distance that a town resident has to travel in order to reach the hospital is kept to a minimum. The answer is shown as a green circle for each of the 4 maps. a NG 2017/1/22Explanation / Answer
class Map
{
byte[][] towns;
byte size;
int[] optimum;
public Map(byte gridSize,byte[][] townList)
{
size=gridSize;
towns=townList;
}
public static double distance(int x1,int y1,int x2,int y2)
{
int diffX=(x2-x1),diffY=y2-y1;
return Math.sqrt((diffX*diffX + diffY*diffY));
}
public int[] computeOptimum()
{
double biggest=0,dist,previous=-1;
optimum=new int[2];
//iterate over each row and column in the grid
for(int x=1;x<=size;x++)
{
for(int y=1;y<=size;y++)
{
biggest=-1;
for(int i=0;i<towns.length;i++)
{
dist=distance(x,y,towns[i][0],towns[i][1]);
if(biggest==-1 || dist>biggest) //find the biggest distance from current point to all towns
biggest=dist;
}
if(previous==-1 || biggest<=previous) //if this distance is lowest from previous recorded distances
{
previous=biggest;
optimum[0]=x;
optimum[1]=y;
}
}
}
return optimum;
}
}
public class HospitalMap {
public static void main(String[] args) {
byte mapSize=20;
byte[][][] mapData = {
{{2, 2},{2, 8}, {5, 15},{19, 1}, {17, 18}},
{{1, 1}, {7, 19}, {13, 19}, {19, 7}, {19, 13}},
{{0, 19}, {2, 19}, {4, 19}, {6, 19}, {18, 19}},
{{7, 19}, {13, 19}, {19, 19}, {19, 13}, {19, 7}}
};
Map maps[]=new Map[mapData.length];
int opt[];
for(int i=0;i<maps.length;i++)
{
maps[i]=new Map(mapSize,mapData[i]);
opt=maps[i].computeOptimum();
System.out.println("Optimum location for Map "+(i+1)+" = ("+opt[0]+","+opt[1]+")");
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.