2 Hospital Builder Program On the next page there are 4 grid

(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

Solution

Keep the codes below in two separate files by the name mentioned, also preferably use an IDE like Eclipse to manage programs with multiple files.

***Main.java***
public class Main {
   public static void main(String args[]){
       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}},
                       };
       new HospitalBuilderProgram(maps).findOptimalPosition();
      
   }
  
}

***HospitalBuilderProgram.java***


public class HospitalBuilderProgram {
   private byte maps[][][];
   private byte directionX[] = {0, 0, 1, -1};
   private byte directionY[] = {1, -1, 0 , 0};
   private int numDirections = 4;
   private int numTowns = 5;
   private int numMaps = 4;
  
   public HospitalBuilderProgram(byte inputMaps[][][]){
       maps = inputMaps;
   }
  
   public void findOptimalPosition(){
  
       for(int mapNum = 0; mapNum < numMaps;mapNum++){
           int optimalX = 0, optimalY = 0;
          
           for(int townNum = 0; townNum < numTowns; townNum++){
               optimalX += maps[mapNum][townNum][0];
               optimalY += maps[mapNum][townNum][1];
           }
          
           optimalX /= numTowns;
           optimalY /= numTowns;
  
           double optimalDistance = distanceFromOptimal(optimalX, optimalY, mapNum);
  
           int step = 50;
           int minStep = 1;
          
           boolean updateStep;
          
           while(step >= minStep){
               updateStep = true;
              
               for(int i = 0; i < numDirections; i++){
                   int potentialOptimalX = optimalX + step * directionX[i];
                   int potentialOptimalY = optimalY + step * directionY[i];
                   double potentialOptimalDistance = distanceFromOptimal(potentialOptimalX, potentialOptimalY, mapNum);
                  
                   if(potentialOptimalDistance < optimalDistance){
                       updateStep = false;
                       optimalDistance = potentialOptimalDistance;
                       optimalX = potentialOptimalX;
                       optimalY = potentialOptimalY;
                       break;
                   }
               }
              
               if(updateStep){
                   step /= 2;
               }
           }
           System.out.println(\"Optimal position for Hospital in Map-\"+ (mapNum + 1) + \" is (\"+ optimalX + \",\" + optimalY + \")\");
  
       }
   }
   private double distanceFromOptimal(int optimalX, int optimalY, int mapNum){
       double optimalDistance = 0;
       for(int townNum = 0; townNum < numTowns; townNum++){
           optimalDistance += Math.sqrt( Math.pow(maps[mapNum][townNum][0] - optimalX, 2) + Math.pow(maps[mapNum][townNum][1] - optimalY, 2) );
       }
       return optimalDistance;
   }
}

 (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
 (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

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site