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

PROBLEM DESCRIPTION: ^^^^^^^^^^^^^^^^^^^^ Nobody wants a nuclear power plant in

ID: 3870321 • Letter: P

Question

 PROBLEM DESCRIPTION: ^^^^^^^^^^^^^^^^^^^^ Nobody wants a nuclear power plant in their neighborhood. Unfortunately, the governor must locate a nuclear power plant somewhere inside a 25 square mile area containing four cities. The governor, being primarily concerned with reelection, desires to locate the plant in the location which will  cause the least amount of unhappiness among the constituents. In this lab, you will write a program to assist the governor in this difficult decision.   METHOD: ^^^^^^^ Assume each city is located in an integer-pair (x, y) valued location  within a 25 x 25 square mile area. For example, a city can be located at (4, 19), but not at (4.5, 19.8). In other words, there are no real valued coordinate for a location of a city.  Your program will consider each integer-pair valued location within the area as a possible site for the nuclear plant, starting at (1, 1) and continuing  across each row until the final site (25, 25) is reached.  At each possible location for the nuclear plant, your program will compute the average unhappiness if the plant were located there. The following two rules will be used to compute the unhappiness for a city.        1) If the plant is within two miles or less of a city, the           unhappiness is infinite (that is, assign a very large number             to the unhappiness for that city).              2) Otherwise, the unhappiness is equal to the population of the                city divided by the distance of the plant from the city.                 The average unhappiness equals:                                 Avg. Unhappiness = Sum of the unhappiness of 4 cities /                                       The total state's population.  Your program should select the site at which the average unhappiness is smallest.   INPUT: ^^^^^^ The user should be prompted to enter the x and y coordinates and the  population for each of the four cities from the keyboard. Each coordinate should be checked to ensure it is between 1 and 25. If not, the user should be prompted to enter a value which is in the correct range. The population should be entered in thousands of people. For example, if the population is 10,000, the user should input 10.   OUTPUT: ^^^^^^^ The program should print a message indication the coordinates where the plant should be located. Following that message, the user should be prompted to enter a 1 to view a map of the scenario or a 0 to exit the program.  An Example Scenario:  Enter the x and y for City 1   : 1 1 Enter the Population for City 1: 10 Enter the x and y for City 2   : 1 25 Enter the Population for City 2: 10 Enter the x and y for City 3   : 25 1 Enter the Population for City 3: 10 Enter the x and y for City 4   : 25 25 Enter the Population for City 4: 10  **********  Locate the Plant At: 13 13  Enter 1 to view a Map of the scenario, or 0 to exit: 1           MAP OF SCENARIO             ---------------      C2<><><><><><><><><><><><><><><><><><><><><><><>C4 <><><><><><><><><><><><><><><><><><><><><><><><><> <><><><><><><><><><><><><><><><><><><><><><><><><> <><><><><><><><><><><><><><><><><><><><><><><><><> <><><><><><><><><><><><><><><><><><><><><><><><><> <><><><><><><><><><><><><><><><><><><><><><><><><> <><><><><><><><><><><><><><><><><><><><><><><><><> <><><><><><><><><><><><><><><><><><><><><><><><><> <><><><><><><><><><><><><><><><><><><><><><><><><> <><><><><><><><><><><><><><><><><><><><><><><><><> <><><><><><><><><><><><><><><><><><><><><><><><><> <><><><><><><><><><><><><><><><><><><><><><><><><> <><><><><><><><><><><><>PP<><><><><><><><><><><><> <><><><><><><><><><><><><><><><><><><><><><><><><> <><><><><><><><><><><><><><><><><><><><><><><><><> <><><><><><><><><><><><><><><><><><><><><><><><><> <><><><><><><><><><><><><><><><><><><><><><><><><> <><><><><><><><><><><><><><><><><><><><><><><><><> <><><><><><><><><><><><><><><><><><><><><><><><><> <><><><><><><><><><><><><><><><><><><><><><><><><> <><><><><><><><><><><><><><><><><><><><><><><><><> <><><><><><><><><><><><><><><><><><><><><><><><><> <><><><><><><><><><><><><><><><><><><><><><><><><> <><><><><><><><><><><><><><><><><><><><><><><><><> C1<><><><><><><><><><><><><><><><><><><><><><><>C3  Note: The previous scenario is just an example, you need to create your own test cases to make sure that your program produces the correct results.  Required Implementation: ^^^^^^^^^^^^^^^^^^^^^^^   Implement a State.java file that contains the State class. The state class  contains all the getters and setters for the data. It also contains the methods that reads in the input calculates the location of the plant, and  displays the output. Another file is required is StatePlant.java which contains your main program and all it does is instantiate the state, read the input  for the state, calculate the location, print the location, and based on the  user's input, print the map.  So the main should be simple and just contains:  State s = new State();  // you can have your own constructor if you'd like  s.read_input();         // this should populate the values. or you can read                          // them in main an pass them here, it's up to you. s.calc_plant();         // print the location of the plant after you calculate  // ask the user if he/she wants to display the map if so    s.display_map(); 

Explanation / Answer

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

class State {
  
       private int citi1x,citi1y;
       private int pop1;
       private int citi2x,citi2y;
       private int pop2;
       private int citi3x,citi3y;
       private int pop3;
       private int citi4x,citi4y;
       private int pop4;
       private int plantx,planty;

       public int getCity1X(){
            return citi1x;
       }
       public int getCity1Y(){
            return citi1y;
       }
       public int getCity2X(){
            return citi2x;
       }
       public int getCity2Y(){
            return citi2y;
       }
       public int getCity3X(){
            return citi3x;
       }
       public int getCity3Y(){
            return citi3y;
       }
       public int getCity4X(){
            return citi4x;
       }
       public int getCity4Y(){
            return citi4y;
       }
       public void setCity1(int a, int b){
            citi1x = a;
            citi1y = b;
       }
       public void setCity2(int a, int b){
            citi2x = a;
            citi2y = b;
       }
       public void setCity3(int a, int b){
            citi3x = a;
            citi3y = b;
       }
       public void setCity4(int a, int b){
            citi4x = a;
            citi4y = b;
       }
       public void setPop1(int a){
            pop1 = a;
       }
       public void setPop2(int a){
            pop2 = a;
       }
       public void setPop3(int a){
            pop3 = a;
       }
       public void setPop4(int a){
            pop4 = a;
       }
       public int getPop1(int a){
            return pop1;
       }
       public int getPop2(int a){
            return pop2;
       }
       public int getPop3(int a){
            return pop3;
       }
       public int getPop4(int a){
            return pop4;
       }
       public int getPlantX(){
            return plantx;
       }
       public int getPlantY(){
            return planty;
       }
       public void setPlantX(int a){
            plantx = a;
       }
       public void setPlantY(int a){
            planty = a;
       }

       public void read_input(){
             int x,y;
             int pop;
             Scanner sc = new Scanner(System.in);
             do {
                  System.out.print("Enter X and Y for city 1: ");
                  x = sc.nextInt();
                  y = sc.nextInt();
             } while ((x < 1 || x > 25) && (y < 1 && y > 25));
             setCity1(x,y);
             System.out.print("Enter population for city 1: ");
             pop = sc.nextInt();
             setPop1(pop*1000);

             do {
                  System.out.print("Enter X and Y for city 2: ");
                  x = sc.nextInt();
                  y = sc.nextInt();
             } while ((x < 1 || x > 25) && (y < 1 && y > 25));
             setCity2(x,y);
             System.out.print("Enter population for city 2: ");
             pop = sc.nextInt();
             setPop2(pop*1000);


             do {
                  System.out.print("Enter X and Y for city 3: ");
                  x = sc.nextInt();
                  y = sc.nextInt();
             } while ((x < 1 || x > 25) && (y < 1 && y > 25));
             setCity3(x,y);
             System.out.print("Enter population for city 3: ");
             pop = sc.nextInt();
             setPop3(pop*1000);

             do {
                  System.out.print("Enter X and Y for city 4: ");
                  x = sc.nextInt();
                  y = sc.nextInt();
             } while ((x < 1 || x > 25) && (y < 1 && y > 25));
             setCity4(x,y);
             System.out.print("Enter population for city 4: ");
             pop = sc.nextInt();
             setPop4(pop*1000);
            
       }
     
       public void calc_plant(){
         
          double min = 100000;
          for (int i = 1; i<=25; i++){
          
            for (int j = 1; j<=25; j++){
            
             double unhappy1 = 0;
             double unhappy2 = 0;
             double unhappy3 = 0;
             double unhappy4 = 0;
            
             if ((i == citi1x && j == citi1y) || (i == citi2x && j == citi2y) || (i == citi3x && j == citi3y) || (i == citi4x && j == citi4y))
                continue;
             double dist = Math.sqrt((i-citi1x)*(i-citi1x) + (j-citi1y)*(j-citi1y));
             if (dist <= 2){
                  unhappy1 = 10000000;
             }
             else {
                 unhappy1 = pop1/dist;
             }
             dist = Math.sqrt((i-citi2x)*(i-citi2x) + (j-citi2y)*(j-citi2y));
             if (dist <= 2){
                 unhappy2 = 1000000;
             }
             else {
                 unhappy2 = pop2/dist;
             }
             dist = Math.sqrt((i-citi3x)*(i-citi3x) + (j-citi3y)*(j-citi3y));
             if (dist <= 2){
                 unhappy3 = 1000000;
             }
             else {
                 unhappy3 = pop3/dist;
             }
             dist = Math.sqrt((i-citi4x)*(i-citi4x) + (j-citi4y)*(j-citi4y));
             if (dist <= 2){
                 unhappy4 = 1000000;
             }
             else {
                 unhappy4 = pop4/dist;
             }
             double unhappy = (unhappy1 + unhappy2 + unhappy3 + unhappy4)/(pop1+pop2+pop3+pop4);
            
             if (unhappy < min){
                  min = unhappy;
                  plantx = i;
                  planty = j;
                 
             }
            }
          }
          
       }
      
       public void display_map(){
           for (int i = 1; i <=25; i++){
              for (int j = 1; j <=25; j++){
                 if (i == citi1x && j == citi1y)
                    System.out.print("C1");
                 else if (i == citi2x && j == citi2y)
                    System.out.print("C2");
                 else if (i == citi3x && j == citi3y)
                    System.out.print("C3");
                 else if (i == citi4x && j == citi4y)
                    System.out.print("C4");
                 else if (i == plantx && j == planty)
                    System.out.print("PP");
                 else
                    System.out.print("<>");
              }
             System.out.println();
           }
           System.out.println();
       }
     
   
}


public class StatePlant{

public static void main(String[] args){
    
     Scanner sc = new Scanner(System.in);
     State s = new State();
     s.read_input();
     s.calc_plant();
     System.out.println("Locate the plan at : " + s.getPlantX() + " " +s.getPlantY() );
     System.out.println("Enter 1 to view a Map of the scenario, or 0 to exit:");
     String inp = sc.nextLine();
     if (inp.charAt(0) == '1'){
        s.display_map();
     }
    
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote