Question

Suppose OPO 1 can cover demand points only within 50 miles. Explain in 10 sentences (or less) how to modify your 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 t# ???? 10.5 11 18 18 24 28.5 3 26 25.5 4 32.5 -5 9 2.5 5 12 13 oPo1 14 OPO1 30.5 5.5 OPO2 20.5 -5.5 | ???? 125 5 13.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-xof 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 following 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 Allocation Results: [1, 2, 3, 3, 2, 1,1, 2, .. Chose an allocation rule: 1-nearest rule, or 2-farthest rule: 3 Chose an allocation rule: 1-nearest rule, or 2-farthest rule: 1

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Java Code:

import java.util.Scanner;

class Main {

                public static void main(String[] args) {

                                //demand points

                                double demandX[] = {0, 9, 10, 10.5, 11, 18, 18, 21, 24, 28.5, 25, 26, 25.5, 30, 32.5};

                                double demandY[] = {0, 1, 8, 11, -5, 2.5, -7, -4, -15, 3, -9, -3, 4, -6, -5};

                                //opos

                                double OPOX[] = {30.5, 20.5, 25.5};

                                double OPOY[] ={-5.5, -5.5, 3.5};

                                //Scanner

                                Scanner sc = new Scanner(System.in);

                                int count = 0, choice = 0;

                                //get choice of allocation

                                while(count<3&&(choice!=1&&choice!=2))

                                {

                                                System.out.print("Chose an allocation rule: 1 - nearest rule, or 2 - farthest rule: ");

                                                choice = sc.nextInt();

                                                count++;

                                                if(count<3&&choice!=1&&choice!=2)

                                                                System.out.println("Invalid input! try again!");

                                }

                                //if exceed, print and terminate

                                if(count>3||(count==3&&choice!=1&&choice!=2))

                                                System.out.println("You have exceeded max selections. Good Bye!!");

                                               

                                //if choice is 1, find nearest

                                else if(choice==1)

                                {

                                                int[] allocation = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};

                                                double near;

                                                //for each demand point find nearest

                                                for(int i=0; i<demandX.length; i++)

                                                {

                                                                allocation[i] = 1;

                                                                near = (demandX[i]-OPOX[0])*(demandX[i]-OPOX[0]) + (demandY[i]-OPOY[0])*(demandY[i]-OPOY[0]);

                                                                if(near>(demandX[i]-OPOX[1])*(demandX[i]-OPOX[1]) + (demandY[i]-OPOY[1])*(demandY[i]-OPOY[1]))

                                                                {

                                                                                allocation[i] = 2;

                                                                                near = (demandX[i]-OPOX[1])*(demandX[i]-OPOX[1]) + (demandY[i]-OPOY[1])*(demandY[i]-OPOY[1]);

                                                                }

                                                                if(near>(demandX[i]-OPOX[2])*(demandX[i]-OPOX[2]) + (demandY[i]-OPOY[2])*(demandY[i]-OPOY[2]))

                                                                {

                                                                                allocation[i] = 3;

                                                                                near = (demandX[i]-OPOX[2])*(demandX[i]-OPOX[2]) + (demandY[i]-OPOY[2])*(demandY[i]-OPOY[2]);

                                                                }

                                                }

                                                //print allocation

                                                System.out.print("Allocation Results: [ " + allocation[0]);

                                                for(int i=1; i<allocation.length; i++)

                                                                System.out.print(", " + allocation[i]);

                                                System.out.println(" ]");

                                }

                                else if(choice==2)

                                {

                                                int[] allocation = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};

                                                double far;

                                                //find farthest for all demand points

                                                for(int i=0; i<demandX.length; i++)

                                                {

                                                                allocation[i] = 1;

                                                                far = (demandX[i]-OPOX[0])*(demandX[i]-OPOX[0]) + (demandY[i]-OPOY[0])*(demandY[i]-OPOY[0]);

                                                                if(far<(demandX[i]-OPOX[1])*(demandX[i]-OPOX[1]) + (demandY[i]-OPOY[1])*(demandY[i]-OPOY[1]))

                                                                {

                                                                                allocation[i] = 2;

                                                                                far = (demandX[i]-OPOX[1])*(demandX[i]-OPOX[1]) + (demandY[i]-OPOY[1])*(demandY[i]-OPOY[1]);

                                                                }

                                                                if(far<(demandX[i]-OPOX[2])*(demandX[i]-OPOX[2]) + (demandY[i]-OPOY[2])*(demandY[i]-OPOY[2]))

                                                                {

                                                                                allocation[i] = 3;

                                                                                far = (demandX[i]-OPOX[2])*(demandX[i]-OPOX[2]) + (demandY[i]-OPOY[2])*(demandY[i]-OPOY[2]);

                                                                }

                                                }

                                                //print allocations

                                                System.out.print("Allocation Results: [ " + allocation[0]);

                                                for(int i=1; i<allocation.length; i++)

                                                                System.out.print(", " + allocation[i]);

                                                System.out.println(" ]");

                                }

                                sc.close();

                }

}

OUTPUT:

Chose an allocation rule: 1 - nearest rule, or 2 - farthest rule: 3 Invalid input! try again! Chose an allocation rule: 1 - nearest rule, or 2 - farthest rule: 1 Allocation Results: 2, 2, 3, 3, 2, 3. 2, 2, 2, 3, 2, 1, 3, 1, 1

Add a comment
Know the answer?
Add Answer to:
Suppose OPO 1 can cover demand points only within 50 miles. Explain in 10 sentences (or...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please help I am struggling with this and 3*Math.random())+1; and how to use it within the...

    Please help I am struggling with this and 3*Math.random())+1; and how to use it within the program For this project you will write a Java program that will play a simple game of Rock, Paper, Scissors.  If you have never played this game before the rules are simple - each player chooses one of Rock, Paper or Scissors and they reveal their choices simultaneously. • The choice of Rock beats the choice of Scissors ("Rock smashes Scissors") • The choice of...

  • Debugging Section Exercise #1 50 points Plus up to 10 points extra The following pseudocode contains...

    Debugging Section Exercise #1 50 points Plus up to 10 points extra The following pseudocode contains errors. You must find 3 or more errors in each of the following programs. If you find more than 3 mistakes, you will earn extra credit points. This pseudocode should determine the rental fees for cars Standard cars rent for $65 per day. compacts rent for $40 per day and subcompacts rent for $30 per day Rentals for at least 7 days receive a...

  • Program Set 2 10 points) Credit card number validation Program Credit card numbers follow certain patterns:...

    Program Set 2 10 points) Credit card number validation Program Credit card numbers follow certain patterns: It must have between 13 and 16 digits, and the number must start with: 4 for Visa cards 5 for MasterCard credit cards 37 for American Express cards 6 for Discover cards In 1954, Hans Luhn of IBM proposed an algorithm for validating credit card numbers. The algorithm is useful to determine whether a card number is entered correctly or whether a credit card...

  • Programming language C The third picture is the code that is being refactored Student 1:10 PM...

    Programming language C The third picture is the code that is being refactored Student 1:10 PM 3296.- Function Specifications [15 points 5 bonus points Use appropriate loops to ensure that if the user enters on incorrect value (out-of-range) mare hece-for an input, that the user is given an accurate messoge and provided an opportunity te enter the value egain (fer every input) Use an appropriate loop to allow the user to execute the program over and over again up to...

  • Homework 3: Input Validation 1   Objectives control structures console-based user input using Scanner class writing complete...

    Homework 3: Input Validation 1   Objectives control structures console-based user input using Scanner class writing complete programs using two classes: client and supplier 2   User Interface Specification This is a console-based I/O program. Display should go to System.out (print or println) and the program will get user input using the Scanner class. The flow of execution should be as follows: When the program starts, display a one-line introduction to the user Display a menu with 5 options 1. validate zip...

  • Before you start doing this code Read carefully there is a format that you have to...

    Before you start doing this code Read carefully there is a format that you have to follow HERE IS THE FORMAT YOU MUST USE FOR HW #2 - NOTE THE IF ELSE IF...not IF IF IF HW #2 Format : Instructions: This program will generate some information for a user about interplanetary travel (pretend we can travel easily to other planets for this problem). This program will perform calculations concerning weight on various planets as well as travel time between...

  • 1) Echo the input: First, you should make sure you can write a program and have...

    1) Echo the input: First, you should make sure you can write a program and have it compile and run, take input and give output. So to start you should just echo the input. This means you should prompt the user for the plaintext, read it in and then print it back out, with a message such as "this is the plaintext you entered:". [4 points, for writing a working program, echoing the input and submitting the program on the...

  • In the original flashcard problem, a user can ask the program to show an entry picked...

    In the original flashcard problem, a user can ask the program to show an entry picked randomly from a glossary. When the user presses return, the program shows the definition corresponding to that entry. The user is then given the option of seeing another entry or quitting. A sample session might run as follows: Enter s to show a flashcard and q to quit: s Define: word1 Press return to see the definition definition1 Enter s to show a flashcard...

  • Please explain each line of code, all code will be in Java. Thank you JKL Restaurant...

    Please explain each line of code, all code will be in Java. Thank you JKL Restaurant maintains a members’ club for its customers.  There are three levels of membership: (1) Basic, (2) Silver, and (3) Gold. A certain member has to be exactly a Basic, Silver, or Gold member at any point in time.  Whenever a member spends money at JKL, he/she gets points and as these points are accumulated, one can redeem one or more $20 dining certificates.  Each Gold member can...

  • Welcome to the Triangles program Enter length 1: 3 Enter length 2: 5 Enter length 3:...

    Welcome to the Triangles program Enter length 1: 3 Enter length 2: 5 Enter length 3: 5 Enter the base: 5 Enter the height: 8 --------------------- The triangle is Isosceles since it has two equal length sides. Isosceles triangle also has two equal angles The area is: 20 The permimeter is: 13 Continue? (y/n): y Enter length 1: 10 Enter length 2: 10 Enter length 3: 10 Enter the base: 10 Enter the height: 7 --------------------- The triangle is Equilateral...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT