The given array below is: private static byte[] [] [] maps= {{{2,2}, {2,8},{5,15
ID: 3781165 • Letter: T
Question
The given array below is: 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 given array below is: 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}}}Please show the output of the code so I'll know it works. (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
Explanation / Answer
The below code is engineered to you problem and tested thoroughly. Simply copy paste this code in your java IDE (like eclipse, netbeans or intelliJ etc. and see the output.
package org.learning;
import java.util.ArrayList;
import java.util.List;
public class ClosestDistanceForHospital {
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}}};
public static double getDistance(int x1, int y1, int x2, int y2){
double res = Math.pow((x2-x1), 2) + Math.pow((y2-y1), 2);
return Math.sqrt(res);
}
public static double getLargest(List<Double> list){
double largest = list.get(0);
for(double d: list){
if(d > largest){
largest = d;
}
}
return largest;
}
public static List<Double> getSmallest(List<List<Double>> list){
List<Double> smallest = list.get(0);
for(List<Double> innerList: list){
if(innerList.get(0) < smallest.get(0)){
smallest = innerList;
}
}
return smallest;
}
private static int gridWidth = 20;
private static int gridHeight = 20;
public static void main(String[] args) {
for(int count = 0; count < maps.length; count++){
System.out.println("For Grid "+(count+1));
List<List<Double>> outer = new ArrayList<>();
//iterating for each point in the grid
for(int x1 = 0; x1 < gridWidth; x1++){
for(int y1=0; y1< gridHeight; y1++){
//iterating for each given point
List<Double> list = new ArrayList<>();
for(int i = 0; i < maps[count].length; i++){
list.add(getDistance(x1, y1, maps[count][i][0], maps[count][i][1]));
}
List<Double> toBeAdded = new ArrayList<>();
toBeAdded.add(getLargest(list));
toBeAdded.add(new Double(x1));
toBeAdded.add(new Double(y1));
outer.add(toBeAdded);
}
}
List<Double> optimalSolution = getSmallest(outer);
System.out.println("Point Optimally Closest to Every Point: ("+optimalSolution.get(1)+","+optimalSolution.get(2)+") ");
}
}
}
Output:
For Grid 1
Point Optimally Closest to Every Point: (11.0,8.0)
For Grid 2
Point Optimally Closest to Every Point: (9.0,9.0)
For Grid 3
Point Optimally Closest to Every Point: (9.0,19.0)
For Grid 4
Point Optimally Closest to Every Point: (13.0,13.0)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.