Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

*******Please I need Help ASAP******* Thanks Question 2. Suppose OPO 1 can cover

ID: 3920358 • Letter: #

Question

*******Please I need Help ASAP******* Thanks

Question 2. Suppose OPO 1 can cover demand points only within 50 miles. Explain in 10 sentences (or less) how to modify your java program code.

Resource Allocation The representative of Health Department of Texas wants to investigate the optimal allocation of organ procurement organizations (OPOs) for organ transplant. Location data Demand x 0 0 ???? 10 10.5 11 2.5 6 10 12 oPO114 21 24 28.53 25 26 25.5 4 30 32.5 -5 3 OPO2 OPO1 30.5 5.5 OPO2 20.5 5.5 OP03 | 25.5 | 3.5 Q1 (40 points). Write a program that can assign all demand points to the nearest or the farthest OPO given the location information above Program requirements 1. The distance must be calculated as follows Distance between A and B (x of A -x of B)A2 (y of A- y of B)A2 We assume the unit of distance is mile 2. The program asks the user to choose one of the allocation rules specified below (1) Rule 1: All demand points are assigned to the nearest OPO (2) Rule 2: All demand points are assigned to the farthest OPO 3. After the user chooses the rule, the program allocates the demand points folowing the rule the user chooses and prints out the allocation results The program must check to be sure that the user input is valid. The program keeps displaying a warning message until the user puts a valid input. If the user repeatedly enters an invalid input more than 3 time, the program displays an error message and terminates. 4. 5. Use selection(s), loop(s), and array(s) Sample Inputs/Outputs Invalid input! try again! Chose an allocation rule: 1-nearest rule, or 2-farthest rule:3 Chose an allocation rule: 1-nearest rule, or 2- farthest rule:1

Explanation / Answer

Q1

import java.io.*;
import java.util.*;


public class Demo241{

     public static void main(String[] args){

         double[] x = {0,9,10,10.5,11,18,18,21,24,28.5,25,26,25.5,30,32.5};
         double[] y = {0,1,8,11,-5,2.5,-7,-4,-15,3,-9,-3,4,-6,-5};

         double[] opox = {30.5,20.5,25.5};
         double[] opoy = {-5.5,-5.5,3.5};
         int[] assign = new int[15];
         Scanner sc = new Scanner(System.in);
         int n = -1;
         int invalidcount = 0;
         while(true){
             System.out.print("Choose an allocation rule: 1- nearest rule, or 2 - farthest rule:");
             n = Integer.parseInt(sc.nextLine());
             if (n != 1 && n != 2){
                invalidcount++;
                System.out.println("Invalid choice ");
                if (invalidcount == 3){
                   System.out.println("Terminating.....");
                   return;
                }
             }
             else {
                 break;
             }
          
         }
         System.out.print("Allocation Results: [");
         if (n == 1){
            for (int i = 0; i<15; i++){
                double min = 1000;
                int mini = 0;
                for (int j = 0; j<3; j++){
                    double d = Math.sqrt((x[i] - opox[j]) * (x[i] - opox[j]) + (y[i] - opoy[j]) * (y[i] - opoy[j]));
                    if (d < min){
                       min = d;
                       mini = j;
                    }
                }
                assign[i] = mini + 1;
           
                if (i < 14)
                   System.out.print(assign[i] + ",");
                else
                   System.out.print(assign[i] );
            }
         }
         if (n == 2){
            for (int i = 0; i<15; i++){
                double max = 0;
                int maxi = 0;
                for (int j = 0; j<3; j++){
                    double d = Math.sqrt((x[i] - opox[j]) * (x[i] - opox[j]) + (y[i] - opoy[j]) * (y[i] - opoy[j]));
                    if (d > max){
                       max = d;
                       maxi = j;
                    }
                }
                assign[i] = maxi + 1;
           
                if (i < 14)
                   System.out.print(assign[i] + ",");
                else
                   System.out.print(assign[i] );
             }
         }
         System.out.println("]");
        
     }
}

Q2 :

OPO1 can cover only 50 miles


The modification will be as follows:

1. The for loop will be modified. Here we are calculating the distance between the demand point and the OPO center. We need to place a check there
   that if the distance is more than 50 miles from OPO1 , then OPO1 will not be considered for neares and farthest consideration for this demand point.

   The code modification is as follows: (Shown for nearest calculation but it will be same for the relevant for code for the farthest calculation

            for (int i = 0; i<15; i++){
                double min = 1000;
                int mini = 0;
                for (int j = 0; j<3; j++){
                    double d = Math.sqrt((x[i] - opox[j]) * (x[i] - opox[j]) + (y[i] - opoy[j]) * (y[i] - opoy[j]));
                    if ( j == 0 && d > 50){
                        continue;
                    }
                    if (d < min){
                       min = d;
                       mini = j;
                    }
                }
                assign[i] = mini + 1;
           
                if (i < 14)
                   System.out.print(assign[i] + ",");
                else
                   System.out.print(assign[i] );
           }